1. MaInActivity
Package com. njupt. actioncc; 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. actioncc; import org. cocos2d. actions. instant. CCFlipX; import org. cocos2d. actions. instant. CCHide; import org. cocos2d. actions. interval. CCJumpTo; import org. cocos2d. actions. interval. CCMoveTo; import org. cocos2d. actions. interval. CCRotateTo; import org. cocos2d. layers. CCLayer; import org. cocos2d. nodes. CCSprite; import org. cocos2d. types. CGPoint; public class GameLayer extends CCLayer {// declare a genie object CCSprite player; public GameLayer () {// declare the file player = CCSprite. sprite ("player.png"); this. addChild (player); // sets the location of the genie object player. setPosition (100,100);/*** the following two sets of instantaneous actions: * CCFlipX * CCHide * // generate the action object // CCFlipX flipX = CCFlipX. action (true); // executes the action object // player. runAction (flipX); // CCHide hide = CCHide. action (); // player. runAction (hide);/*** the following two are delayed actions: * CCMoveTo * CCRotateTo * // *** CCMoveTo. action (3, pos): the first parameter indicates the duration of the action, and the second parameter indicates the end * // CGPoint pos = CGPoint. ccp (200,200); // CCMoveTo moveTo = CCMoveTo. action (3, pos); // player. runAction (moveTo);/*** CCRotateTo. action (3,-180): the first parameter indicates the duration of the action, and the second parameter indicates the rotation angle. * a negative number indicates clockwise rotation. A positive number indicates clockwise rotation... */CCRotateTo rotateTo = CCRotateTo. action (3,-180); player. runAction (rotateTo );}}