• Register

A game full of action, adventure and puzzle where Masato needs to protect his village from one of the most powerful rogue ninja he as ever faced

Post tutorial Report RSS Using delegate controls in cocos2d-x

In my previous article “Implementing MVC pattern for cocos2d-x projects” I had mentioned about a “Model” sending messages back to the “Controller” using delegate control. While the source code is located in the “GitHub”, I had few request regarding a bit more explanation on the usage of the delegate control mentioned in that article.

Posted by on - Advanced Design/Concepts

Using delegate controls in cocos2d-x

In my previous article "Implementing MVC pattern for cocos2d-x projects" I had mentioned about a "Model" sending messages back to the "Controller" using delegate control. While the source code is located in the "GitHub", I had few request regarding a bit more explanation on the usage of the delegate control mentioned in that article.

cocos2d-x delegate control

I will refer to the same source code as explained in the "MVC" article so that you can relate to this easier. We have a ball class and a Game Layer class. Game Layer sends command to the ball class to change its state (animation) and in return ball class returns a message back to the Game layer class using a delegate control. So how do we do this?

We will create a C++ header class called "GameplayLayerDelegate.h". This class will have a virtual method. In our example, we want to send a message back to the Gameplay layer class when the ball touches the ground, therefore we will call our virtual method as "touchGround" and this method will accept the position in the screen where the ball touches the ground (since this can be anywhere in the screen).


cpp code:
class GameplayLayerDelegate
{
public:
virtual void touchGround(CCPoint startPosition) = 0;

};


The GamePlayLayer class will inherit from this delegate header class as mentioned above:

cpp code:
class GamePlayLayer: public GameplayLayerDelegate {

public:
   
    GamePlayLayer();
    virtual ~GamePlayLayer();
    virtual void touchGround(CCPoint startPosition);
    //and so on...
};
 


As you can see that in the virtual method we have "= 0" in the end. This means that we force the inherited class to implement this virtual method otherwise your code will not build successfully. So we implement this virtual method in the GamePlayLayer class.

cpp code:
void GamePlayLayer::touchGround(CCPoint startPosition){
    //we know that ball has touched the ground, we need to do something here.
}
 


The ball class will declare the GamePlayLayerDelegate as a public variable:

cpp code:
class Ball: public CCSprite{
pubic:
   CC_SYNTHESIZE(GameplayLayerDelegate*, _delegate, Delegate);
};
 


In the GameplayLayer class, when we initialise the Ball sprite, we also set its delegate:

cpp code:
Ball* ball = Ball::create();
//set the delegate of the ball class.
ball->setDelegate(this);
ball->initWithSpriteFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("ball_1.png"));
ball->setPosition(spawnLocation);
ball->setInitialPosition(spawnLocation);
ball->changeState(kStateBouncing);
_sceneSpriteBatchNode->addChild(ball, ZValue, kBallTagValue);
 


Now from the Ball class we can send a message back to the GamePlayLayer class using the delegate control and accessing the touchGround method implemented in the GamePlayLayer class:

cpp code:
//call this when the ball touches the ground.
void Ball::bounceMe(CCNode *node){
    this->getDelegate()->touchGround(this->getPosition());
    this->changeState(kStateBouncing);
}
 


That's it folks. Again the source code can be found in the "GitHub". Please feel free to comment and provide feedback. Thanks for visiting cobagames.

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: