Cocos2dx genie Movement

Source: Internet
Author: User

Objective: to move the genie randomly. Process: A white background is required. Modify the inheritance of HelloWorld to inherit CCLayerColor, and then modify the init method. As follows: [objc] CCLayerColor: initWithColor (ccc4 (255,255,255,255) after adding image resources, you can start coding. The comments must be written in the code ,, paste the code: HelloWorld. h declaration method: [objc] void addTarget (); void spriteMoveFinished (CCNode * sender); void gameLogic (cocos2d: CCTime dt); HelloWorld. the init () method for rewriting m Files: [objc] # include "HelloWorldScene. h "# include" SimpleAudioEngine. h "using namespace cocos2d; using namespace CocosDenshion; CCScene * HelloWorld: scene () {// 'Scene 'is an autorelease object CCScene * scene = CCScene: create (); // 'player' is an autorelisted object HelloWorld * layer = HelloWorld: create (); // add layer as a child to scene-> addChild (layer ); // return the scene return scene;} // on "init" you need to initialize your instance bool HelloWorld: init () {// 1. super init first if (CCLayerColor: initWithColor (ccc4 (255,255,255,255) {CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); // obtain the screen size. float sprite_scale = 2.0; CCSprite * Player = CCSprite: create ("Player.png"); Player-> setScale (sprite_scale ); player-> setPosition (ccp (Player-> getContentSize (). width * sprite_scale/2.0, winSize. height/2.0); this-> addChild (Player); this-> schedule (schedule_selector (HelloWorld: gameLogic), 1.0); return true ;} else {return false ;}} void HelloWorld: gameLogic (cocos2d: CCTime dt) {this-> addTarget ();} void HelloWorld: addTarget () {CCSize winSize = CCDirector: sharedDirector ()-> getWinSize (); CCSprite * target = CCSprite: create ("Target.png "); // random position int minY = target-> getContentSize (). height/2.0; int maxY = winSize. height-target-> getContentSize (). height/2.0; int rangeY = maxY-minY; int actualY = rand () % rangeY + minY; target-> setPosition (ccp (winSize. width-target-> getContentSize (). width/2.0, actualY); this-> addChild (target); // random speed float minDuration = 2.0; float maxDuration = 4.0; int rangeDuration = maxDuration-minDuration; float actualDuration = rand () % rangeDuration + minDuration; CCFiniteTimeAction * actionMove = CCMoveTo: create (actualDuration, ccp (0-target-> getContentSize (). width/2.0, actualY); // 0 indicates that the screen is out. This indicates that the callback function is added from the initial position to the screen within 3 seconds, reclaim the CCFiniteTimeAction * actionMoveDone = CCCallFuncN: create (this, callfuncN_selector (HelloWorld: Success); target-> runAction (CCSequence: create (actionMove, actionMoveDone, NULL);} void HelloWorld: spriteMoveFinished (cocos2d: CCNode * sender) {CCSprite * sprite = (CCSprite *) sender; this-> removeChild (sprite, true );} void HelloWorld: menuCloseCallback (CCObject * pSender) {ccctor ctor: shareddire()-> end (); # if (CC_TARGET_PLATFORM = CC_PLATFORM_IOS) exit (0 ); # endif:

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.