Cocos2d-x genie action not executed work und

Source: Internet
Author: User

First, let's look at the Code:


[Cpp]
Void GameEnemyBullet: setvisable ()
{
This-> setVisible (true); // sets the sprite visibility.
Isvisable = true;
This-> runAction (CCJumpTo: create (3, CCPointMake (500,500), 30, 5); // let the genie perform the Skip action
CCMessageBox ("over", "over"); // pop up a small window to verify whether the operation is performed here
}

Void GameEnemyBullet: setvisable ()
{
This-> setVisible (true); // sets the sprite visibility.
Isvisable = true;
This-> runAction (CCJumpTo: create (3, CCPointMake (500,500), 30, 5); // let the genie perform the Skip action
CCMessageBox ("over", "over"); // pop up a small window to verify whether the operation is performed here
}
The Code is also very simple, but when I execute this method, I can't always execute the Skip action, but it is true that the function is executed because the window has been popped up and a static genie has also appeared, but why is it not moving. This is confusing. Why is runAction not executed?

Then I read it.

[Cpp]
Bool GameEnemyBullet: init ()
{
CCSprite * bulletmainbody = CCSprite: create ("DrDogZD.png ");
Bulletmainbody-> runAction (CCRepeatForever: create (CCRotateBy: create (0.5, 350 )));
Isvisable = false;
This-> setVisible (false );
AddChild (bulletmainbody, 1 );
Return true;
}

Bool GameEnemyBullet: init ()
{
CCSprite * bulletmainbody = CCSprite: create ("DrDogZD.png ");
Bulletmainbody-> runAction (CCRepeatForever: create (CCRotateBy: create (0.5, 350 )));
Isvisable = false;
This-> setVisible (false );
AddChild (bulletmainbody, 1 );
Return true;
}
GameEnemyBullet class inherits from CCNode. When creating an object, it uses the create method and overwrites the onEnter method. The Code is as follows:


[Cpp]
Void GameEnemyBullet: onEnter ()
{
CCNode: onEnter ();
}

Void GameEnemyBullet: onEnter ()
{
CCNode: onEnter ();
}
Later, with the help of CCLog (), I found that the rewritten onEnter () was not called at all, and more often, we simply forgot to call CCNode :: onEnter ();
In this way, the final result is that the image appears, and when no action is performed, the image is quietly there.

However, this is not true. In theory, when creating a GameEnemyBullet object through create, the init () method should be called first, and then the onEnter () method should be called, but it is not called now.

So I read the class file for creating the GameEnemyBullet object, which inherits from the CCLayer and also overrides the onEnter () method. However, I found that in this onEnter, I didn't call the method "CCLayer: onEnter ();" for the parent class ();". That is to say, I made the most common mistake for a newbie. Do not forget to call the parent class method in the override onEnter method.

Therefore, after CCLayer: onEnter (); is added to this file, the static image can finally be moved, and the onEnter method in GameenemyBullet is also called !!!

Finally, optimize the ugly and inefficient code:


[Cpp]
Void GameEnemyBullet: onEnter ()
{
CCNode: onEnter ();
This-> setContentSize (CCSizeMake (21, 62 ));
// CCLog ("gameenemybullet onenter ");
}
Bool GameEnemyBullet: init ()
{
CCSpriteBatchNode * batchnode = CCSpriteBatchNode: create ("DrDogZD.png ");
AddChild (batchnode, 1, 3 );
CCSprite * bulletmainbody = CCSprite: createWithTexture (batchnode-> getTexture ());
Bulletmainbody-> runAction (CCRepeatForever: create (CCRotateBy: create (0.5, 350 )));
Isvisable = false;
This-> setVisible (false );
Batchnode-> addChild (bulletmainbody, 1 );
// CCLog ("gameenemybullet init ");
Return true;
}

Void GameEnemyBullet: onEnter ()
{
CCNode: onEnter ();
This-> setContentSize (CCSizeMake (21, 62 ));
// CCLog ("gameenemybullet onenter ");
}
Bool GameEnemyBullet: init ()
{
CCSpriteBatchNode * batchnode = CCSpriteBatchNode: create ("DrDogZD.png ");
AddChild (batchnode, 1, 3 );
CCSprite * bulletmainbody = CCSprite: createWithTexture (batchnode-> getTexture ());
Bulletmainbody-> runAction (CCRepeatForever: create (CCRotateBy: create (0.5, 350 )));
Isvisable = false;
This-> setVisible (false );
Batchnode-> addChild (bulletmainbody, 1 );
// CCLog ("gameenemybullet init ");
Return true;
}
To sum up, I overwrote the onEnter method of NewGame in NewGame that instantiated the GameEnemyBullet object, but did not call the CCLayer: onEnter (); method of the base class, therefore, when the GameEnemyBullet object is created, the onEnter method of the GameEnemyBullet is not called. In this way, the added genie does not have any action effect.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.