RPG Slots Progress – Cocos2d Review

rpgslots

I started developing a slots game using the Cocos2d C++ SDK. Cocos2d is an opensource game development framework that support C++, Objective-C, and JavaScript. Eventually, this game will evolve into a slots RPG, like King Cashing and King Cashing 2. For now it uses a pretty generic reels and a match 3 mechanic that matches on the same row as well as cells on adjacent rows. For score, it keeps experience points, since this will transition to the RPG slots.

Source for the project can be found here. As of this writing, it’s in early development.

Like many game frameworks, Cocos2d has many helper functions that allow for quick game prototyping. Scenes are easy to construct, and assets, sprites, and audio can be added using built in Cocos2d objects. It even supports the ability to add custom shaders.

Extending Sprites

I found quickly that I needed to create custom objects that extended sprites. In this project there two classes that extend cocos2d::sprite ; the reel, and the HUD.

Grouping elements within sprites helped with organizing code and separation of concern. I did run into strange memory errors when trying to add certain objects, such as cocos2d::Label , directly to the scene while also having a pointer to it in the scene.

Autorelease

The Cocos2d C++ framework uses a smart-pointer technique to automatically destroy dynamically allocated objects when its internal reference count is 0. This relieves pressure in remembering to destroy objects and worrying about pointer ownership. Though cyclical dependencies still need to be avoided.

The built-in Cocos2d objects are automatically added to the autorelease pool, so there is no need to use the new  keyword. In my project, I have an object that extends the Cocos2d sprite. So there’s some boilerplate code that I needed to add so my object would be added to the autorelease pool.

ReelSprite::create  is a static method that follows the Cocos2d convention of constructing an object and adding it to the autorelease pool. mainSprite->autorelease()  is the line that actually adds the object to the autorelease pool, so that it does not have to be manually destroyed.

Reel Animation

To get the reels to function properly in regards to animation and properly stopping, there were several things that had to happen. The reels needed to be animated so they moved down vertically and timing needed to be involved so that they stopped within a given time and at a given cell.

Spin Management

In this implementation the start of each reel is staggered using the scheduler. This is something that will probably change. The ReelSprite::startSpin  method is called by the scheduler.

In ReelSprite::startSpin, there’s another scheduled callback function that handles the stop sequence. I’ve broken this process down below. It starts with defining the expected position, which is challenging because there’s two sprites that alternate top-bottom order.

If the desired cell is not at the right position, cocos2d actions are used to move the reels to the right position while maintaining the spin animation.

The tricky thing I ran into was I needed to animate the two sprites contained in the reel at the same time. This was accomplished using cocos2d::Sequence. The challenge here is two fold; binding the sprite in question, and handling a race condition.

It’s possible the callback is called twice. Luckily cocos2d::Sequence::isDone exists to detect if the sequence is indeed finished. From there, other flags are set to indicate the reel has completely stopped spinning.

Spinning Animation

In order to accomplish a fluid animation, each reel column needed two instances of the reel image to fill in a gap left when the top of one passed the top of the view window.

Moving Forward

Though my use of Cocos2d has been somewhat limited, so far I really like Cocos2d. It is well suited for 2d games, and I will use it more to see how well it compares to other options, such as Unity.

There is also some refactoring that will need to be done, for example with lambdas and matching.

Lambdas

I used lambda functions in my code because it was convenient for prototyping. Unfortunately, the use of lambdas make it more difficult to unit test.

For example, crucial logic that handles the stopping sequence is embedded in ReelSprite::startSpin . Ideally this should be broken out so that the stop sequence can be tested independently.

Matching

At the moment, matching is pretty flawed because it will match cells across the same column. I intend on covering this in another post that will involve the recursive function that does the matching, as well as unit tests that assert the proper matches are being returned.

 

Questions or Comments? Leave a Reply!

This site uses Akismet to reduce spam. Learn how your comment data is processed.