This chapter is directly on the source code. The content is not difficult, mainly is
HelloWorldScene.h file:
1 #ifndef __helloworld_scene_h__2 #define__helloworld_scene_h__3 4#include"cocos2d.h"5 6 classHelloWorld: PublicCocos2d::layer7 {8 protected:9 float_angle;Ten cocos2d::vec2 _vec; One Public: A Staticcocos2d::scene*Createscene (); - - Virtual BOOLinit (); the - //a selector callback - voidMenuclosecallback (cocos2d::ref*psender); - + //implement the "Static Create ()" Method manually - Create_func (HelloWorld); + A Virtual voidUpdatefloatDT); at Private: - //Get the screen visual range - floatwidth_l; - floatWidth_r; -Cocos2d::D rawnode*Ball ; - }; in - #endif //__helloworld_scene_h__
HelloWorldScene.cpp file:
1 //On "Init" need to initialize your instance2 BOOLHelloworld::init ()3 {4 //////////////////////////////5 //1. Super init First6 if( !layer::init ())7 {8 return false;9 }Ten OneSize visiblesize = Director::getinstance ()getvisiblesize (); AVEC2 origin = Director::getinstance ()Getvisibleorigin (); -width_l =origin.x; -Width_r = origin.x +Visiblesize.width; the -Ball =drawnode::create (); -Ball--Drawdot (VEC2 (0,0),4, color4f (1.0f,1.0f,1.0f,1.0f)); - + addChild (ball); -Ball--SetPosition (origin.x + visiblesize.width/2, ORIGIN.Y + visiblesize.height/2); + A //action-related motion we are generally not used to do game games, generally used to make the game change effect. Because the action is not very good to show the effect of the game at //dot-runaction (repeatforever::create (Moveby::create (0.2, _vec*100)); - // - scheduleupdate (); - return true; - } - in voidHelloworld::update (floatDT) { -Ball--Setpositionx (Ball->getpositionx () +5); to if(Ball->getpositionx () <width_l-ball->getcontentsize (). width/2 +|| Ball->getpositionx () >width_r+ball->getcontentsize (). width/2) { -Ball->setpositionx (-ball->getcontentsize (). width/2); the } *}
Then achieve the effect:
Cocos2d Getting started--3--ball sport