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: