Cocos2d Article 2

Source: Internet
Author: User

1. Menu usage:

CCMenuItemimage * image = CCMenuItemImage * create ("xxx.png ",

"Xxx, png ",

"Xxx.png ",

This,

Menu_selector (HelloWorld: selfdefinefunc); // The first parameter is used when no image is selected, the second parameter is used when the image is selected, and the third parameter is: the image selected at the time of entry. Fourth parameter: on which object, fourth parameter: specific response function.

CCMenu * menu = CCMenu: create (iamge, NULL); // create a menu

Menu-> addChild (menu );


2. Actions executed by the genie:

CCMoveBy * moveby = CCMoveBy: create (2.0f, ccp (800,0 ));

CCCallFuncN * selfaction = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: selfdefine); // CCCallFuncN is also an action, but this action is a callback function (with a parameter ).
CCSequence * action = CCSequence: create (moveby, selfaction, NULL); // combined action
Sprite1-> runAction (action );


3. How to enable the Timer:

Bool HelloWorld: init ()
{

// Omit a lot of code

This-> schedule (schedule_selector (HelloWorld: usecreatesprite), 2); // The first parameter is the response function, and the second parameter is the interval of execution.

Return true;

}

Void HelloWorld: usecreatesprite (float dt ){
Createsprite ();
}


Void HelloWorld: createsprite (){
CCSize visiblesize = CCDirector: sharedDirector ()-> getVisibleSize ();
CCSprite * sprite1 = CCSprite: create ("sprite.png"); // use local variables because many genie are to be created. Class Members are not applicable.
Int y = rand () % (int) (visiblesize. height );
Sprite1-> setPosition (ccp (10, y ));
This-> addChild (sprite1 );
CCMoveBy * moveby = CCMoveBy: create (2.0f, ccp (800,0 ));

CCCallFuncN * selfaction = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: selfdefine ));
CCSequence * action = CCSequence: create (moveby, selfaction, NULL );
Sprite1-> runAction (action );
}

Q: Why is the createsprie function called in usecreatesprite (float dt) instead of being directly enabled when a timer is used?

Because the prototype of the execution function in schedule is fixed, the return value is void type, and the parameter type is float, while the createsprite function does not meet the requirements, the usecreatesprite function is used as a transitional function.

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.