1. MainActivity
Package com. njupt. cocos2dvector; import org. cocos2d. layers. CCScene; import org. cocos2d. nodes. CCDirector; import org. cocos2d. opengl. CCGLSurfaceView; import android. OS. bundle; import android. app. activity; import android. view. menu; public class MainActivity extends Activity {// The cocos2d engine knows that the image is private CCGLSurfaceView view = null on the view object; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); view = new CCGLSurfaceView (this); setContentView (view); CCDirector dire= CCDirector. sharedDirector (); // get the CCDirector object/*** set the properties of the game program */director. attachInView (view); // sets the view object director used in the current game program. setDisplayFPS (true); // sets whether the game program displays the FPS value director. setAnimationInterval (1.0f/60); // set the time required for rendering a frame in the game. // generate a game scenario object CCScene scene = CCScene. node (); // generate the set layer object GameLayer gameLayer = new GameLayer (); // Add the set layer object to scene in the game scene. addChild (gameLayer); // run game scenario ctor. runWithScene (scene) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
2. GameLayer
Package com. njupt. cocos2dvector; import org. cocos2d. actions. interval. CCJumpBy; import org. cocos2d. actions. interval. CCJumpTo; import org. cocos2d. actions. interval. CCMoveBy; import org. cocos2d. layers. CCLayer; import org. cocos2d. nodes. CCSprite; import org. cocos2d. types. CGPoint; public class GameLayer extends CCLayer {// declare a Sprite object CCSprite aSprite; CCSprite bSprite; public GameLayer () {aSprite = CCSprite. sprite ("player.png"); bSprite = CCSprite. sprite ("player.png"); this. addChild (aSprite); this. addChild (bSprite); CGPoint initPoint = CGPoint. ccp (100,100); aSprite. setPosition (initPoint); bSprite. setPosition (initPoint); // CGPoint detaPoint = CGPoint. CPP (0,100); // CGPoint target = CGPoint. ccpAdd (initPoint, detaPoint); // addition of vectors /// bSprite. setPosition (target);/*** CCMoveBy. action (3, pos) * CCMoveBy adds the value of the current position of the genie to the value of pos * // CGPoint pos = CGPoint. ccp (100,100); // CCMoveBy MoveBy = CCMoveBy. action (3, pos); // aSprite. runAction (MoveBy);/*** CCJumpBy. action (3, pos, 200, 3); * four parameters are: action duration, increment, jump height, number of hops */CGPoint pos = CGPoint. CPP (300,400); CCJumpBy jumpBy = CCJumpBy. action (3, pos, 200, 3); aSprite. runAction (jumpBy );}}