Detailed explanation of the source code of Cocos2D-Android-1: 2. ActionManagerTest

Source: Internet
Author: User

Detailed explanation of the source code of Cocos2D-Android-1: 2. ActionManagerTest

// Now let's start commenting on his first activitypackage org. cocos2d. tests; import org. cocos2d. actions. CCActionManager; import org. cocos2d. actions. base. CCAction; import org. cocos2d. actions. instant. CCCallFunc; import org. cocos2d. actions. instant. CCCallFuncN; import org. cocos2d. actions. interval. CCDelayTime; import org. cocos2d. actions. interval. CCFadeOut; import org. cocos2d. actions. interval. CCMoveBy; import org. cocos2d. actions. interval. CCRotateBy; import org. cocos2d. actions. interval. CCScaleBy; import org. cocos2d. actions. interval. CCScaleTo; import org. cocos2d. actions. interval. CCSequence; import org. cocos2d. layers. CCLayer; import org. cocos2d. layers. CCScene; import org. cocos2d. menus. CCMenu; import org. cocos2d. menus. CCMenuItemImage; import org. cocos2d. nodes. CCDirector; import org. cocos2d. nodes. CCLabel; import org. cocos2d. nodes. CCNode; import org. cocos2d. nodes. CCSprite; import org. cocos2d. opengl. CCGLSurfaceView; import org. cocos2d. opengl. CCTextureAtlas; import org. cocos2d. types. CGPoint; import org. cocos2d. types. CGSize; import android. app. activity; import android. OS. bundle; import android. view. window; import android. view. windowManager; // There is a downloadable demo on this test // http://code.google.com/p/cocos2d-android-1/downloads/detail?name=ActionManagerTest.3gp&can=2&q=#makechanges// The above is public class ActionManagerTest extends Activity {// is a common activity // private static final String LOG_TAG = ActionManagerTest. class. getSimpleName (); // The above annotation was originally intended to obtain the package name public static final int kTagNode = 0; // public static final int kTagGrossini = 1; public static final int kTagSister = 2; public static final int kTagSlider = 3; public static final int kTagSequence = 4; private CCGLSurfaceView mGLSurfaceView; // This c Cg's view is a class in opengl, and ultimately inherits sufaceview. Do not explain it in detail @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); requestWindowFeature (Window. FEATURE_NO_TITLE); // No title getWindow (). setFlags (WindowManager. layoutParams. FLAG_FULLSCREEN, WindowManager. layoutParams. FLAG_FULLSCREEN); // full screen getWindow (). setFlags (WindowManager. layoutParams. FLAG_KEEP_SCREEN_ON, // do not close WindowManager if it is not kept dark. layou TParams. FLAG_KEEP_SCREEN_ON); mGLSurfaceView = new CCGLSurfaceView (this); // generate surfaceviewCCDirector dire= CCDirector under this avtivity. shareddire(); // gets the "director" director. attachInView (mGLSurfaceView); // give the background to the director. The director will draw the data to ctor later. setDeviceOrientation (CCDirector. kCCDeviceOrientationLandscapeLeft); // setContentView (mGLSurfaceView) on the horizontal screen; // display this view // show FPSCCDirector. shareddire (). setDisplayFPS (true); // display Fps resolution // frames per secondCCDirector. shareddire (). setAnimationInterval (1.0f/30); // sets the number of seconds for an animation to brush a frame CCScene scene = CCScene. node (); // obtain a scenario scene. addChild (nextAction (); // Add a subclass to the scenario // Make the Scene activeCCDirector. shareddire (). runWithScene (scene); // The director starts to show.} static int sceneIdx =-1; static Class <?> Transitions [] = {CrashTest. class, LogicTest. class, PauseTest. class, RemoveTest. class, Issue835.class,}; @ Overridepublic void onStart () {// The following several methods can be understood by name .. super. onStart () ;}@ Overridepublic void onPause () {super. onPause (); CCDirector. shareddire (). onPause () ;}@ Overridepublic void onResume () {super. onResume (); CCDirector. shareddire (). onResume () ;}@ Overridepublic void onDestroy () {super. onDestroy (); CCDire Ctor. shareddire (). end (); // CCTextureCache. sharedTextureCache (). removeAllTextures ();} // Actions here // static CCLayer nextAction () {// implemented layer sceneIdx ++; // animated frame sceneIdx = sceneIdx % transitions. length; // keep the animation layer frames in a loop, for example, 0-> 1-> 2-> 3-> 4 return restartAction (); // return the attributes of another class, the above variable is a global static variable} static CCLayer backAction () {// similarly, a layer sceneIdx --; int total = transitions is rolled back. length; if (sceneIdx <0) sceneIdx + = total; retu Rn restartAction ();} static CCLayer restartAction () {try {Class <?> C = transitions [sceneIdx]; // return (CCLayer) c. newInstance (); // returned display layer} catch (Exception e) {return null ;}} static abstract class ActionManagerDemo extends CCLayer {CCTextureAtlas; // define a large image public ActionManagerDemo () {// constructor CGSize s = CCDirector. shareddire (). winSize (); // obtain the width and height of CCLabel label = CCLabel from the director. makeLabel (title (), "DroidSans", 32); // draw a label with the font "DroidSans" and a 32-bit addChild (la Bel, 1); // Add the object to the layer label. setPosition (CGPoint. make (s. width/2, s. height/2-50); set the label position String subtitle = subtitle (); // change the subtitle to null if (subtitle! = Null ){//... this command does not run CCLabel l = CCLabel. makeLabel (subtitle, "DroidSerif", 16); addChild (l, 1); l. setPosition (CGPoint. ccp (s. width/2, s. height-80);} CCMenuItemImage item1 = CCMenuItemImage. item ("b1.png", "b2.png", this, "backCallback"); // menu item 1. Click the backCallback method CCMenuItemImage item2 = CCMenuItemImage. item ("r1.png", "r2.png", this, "restartCallback"); // Similarly, CCMenuItemImage item3 = CCMenuItemImage. item ("f 1. png "," f2.png ", this," nextCallback "); CCMenu menu = CCMenu. menu (item1, item2, item3); // Add three items to menumenu. setPosition (CGPoint. make (0, 0); // set the menu position item1.setPosition (CGPoint. make (s. width/2-100, 30); // The position item2.setPosition (CGPoint. make (s. width/2, 30); item3.setPosition (CGPoint. make (s. width/2 + 100, 30); addChild (menu, 1); // set the order,} public void restartCallback (Object sender) {// re-initiate C CScene s = CCScene. node (); s. addChild (restartAction (); CCDirector. shareddire (). replaceScene (s);} public void nextCallback (Object sender) {// The next animation CCScene s = CCScene. node (); // create a scenario s. addChild (nextAction (); // Add the next animation layer CCDirector in the scene. shareddire (). replaceScene (s); // Replace the current animation layer, which is actually placed at the top of the layer stack} public void backCallback (Object sender) {// A CCScene s = CCScene. node (); s. addChild (backAction (); CCDirector. Shareddire(). replaceScene (s);} public String title () {// return No question return "No Title! ";}Public String subtitle () {// return null return;} static class CrashTest extends ActionManagerDemo {// inherits the general class public CrashTest () just created () {// construct super (); // The Inheritance Method CCSprite child = CCSprite. sprite ("grossini.png"); // use this figure to create an elf child. setPosition (CGPoint. ccp (200,200); // set the location of the genie addChild (child, 1 ); // Add this layer to the layer // Sum of all action's duration is 1.5 second. child. runAction (CCRotateBy. action (1.5f, 90); // Let this image perform a rotation action of 1.5 seconds, 90 degrees, immediate execution, is rotating, not rotating to child. runAction (CCSequence. actions (// executes a queue and runs one by one. It is an iteration class CCDelayTime. action (1.4f), // The first in the queue is the 1.4 seconds delay CCFadeOut. action (1.1f); // fade out in 1.1 seconds // After 1.5 second, self will be removed. this. runAction (CCSequence. actions (CCDelayTime. action (1.4f), // latency: 1.4 seconds CCCallFunc. action (this, "removeThis") // execution method, the removeThis method in this class);} public void removeThis () {this. getParent (). removeChild (this, true); // remove it from its parent class, and this will not be found in the parent class or connected. nextCallback (null); // play the next parameter. It is useless in brackets ..} @ Overridepublic String title () {return "Test 1. shocould not crash "; // Test 1 is returned. shocould not crash} static class LogicTest extends ActionManagerDemo {// Similarly, public LogicTest () {super (); CCSprite grossini = CCSprite. sprite ("grossini.png"); // create an genie. Similarly, addChild (grossini); grossini. setPosition (CGPoint. ccp (200,200); grossini. runAction (CCSequence. actions (// execute the action CCMoveBy. action (1.0f, CGPoint. ccp (), // relatively move CCCallFuncN. action (this, "bugMe"); // execute the bugMe method} public void bugMe (CCNode node) {node. stopAllActions (); // After this stop next action not working, if remove this stop everything is workingnode. runAction (CCScaleTo. action (2.0f, 2.0f); // proportional amplification} public String title () {return "Logic test" ;}} static class PauseTest extends ActionManagerDemo {public PauseTest () {super ();} public void onEnter () {// This test MUST be done in 'onenter' and not on 'init '// otherwise the paused action will be resumed at 'onenter' time // super. onEnter (); // Also, this test MUST be done, after [super onEnter] // CCSprite grossini = CCSprite. sprite ("grossini.png"); // Similarly, create the sprite addChild (grossini, 0, kTagGrossini); grossini. setPosition (CGPoint. ccp (200,200); CCAction action = CCMoveBy. action (1.0f, CGPoint. ccp (, 0); // The action is relatively moving. CCMoveBy is a static class, but its action method creates its instance internally, CCMoveBy converts the value of moveto ccdelta to its own posCCActionManager. sharedManager (). addAction (action, grossini, true); // get the action controller. Use this action controller to put the action into the grossini node. The action controller is created at zero time, but his own reference is a static variable, so there is only one this. schedule ("unpause", 3); // set a timetable at intervals of 3} public void unpause (float dt) {unschedule ("unpause "); // This will stop .. CCNode node = getChildByTag (kTagGrossini); // obtain the CCActionManager node. sharedManager (). resume (node); // restore the node, that is, set the Pause status to false;} @ Overridepublic String title () {return "Pause Test";} public String subtitle () {return "After 3 seconds grossini shocould move";} static class RemoveTest extends ActionManagerDemo {// Similarly, public RemoveTest () {super (); CCMoveBy move = CCMoveBy. action (2, CGPoint. ccp (); // move CCCallFunc callback = CCCallFunc. action (this, "stopAction"); // obtain a function CCSequence sequence = CCSequence. actions (move, callback); sequence. setTag (kTagSequence); CCSprite child = CCSprite. sprite ("grossini.png"); child. setPosition (CGPoint. ccp (200,200); this. addChild (child, 1, kTagGrossini); child. runAction (sequence);} public void stopAction (Object sender) {// The same is true for the following... CCNode sprite = getChildByTag (kTagGrossini); sprite. stopAction (kTagSequence);} public String title () {return "Remove Test";} public String subtitle () {return "shocould not crash. testing issue #841 "; // The same is true for the following ...}} static class Issue835 extends ActionManagerDemo {// Similarly, public Issue835 () {super ();} public void onEnter () {super. onEnter (); CGSize s = CCDirector. shareddire (). winSize (); // still get the window size CCSprite grossini = CCSprite. sprite ("grossini.png"); // The following are all the same... addChild (grossini, 0, kTagGrossini); grossini. setPosition (CGPoint. ccp (s. width/2, s. height/2); // An action shocould be scheduled before calling pause, otherwise pause won't pause a non-existang targetgrossini. runAction (CCScaleBy. action (2.0f, 2.0f); CCActionManager. sharedManager (). pause (grossini); // The same is true for the following... grossini. runAction (CCRotateBy. action (2.0f, 360); this. schedule ("resumegrosini", 3);} public String title () {return "Issue 835";} public String subtitle () {return "Grossini only rotate/scale in 3 seconds";} public void resumegrosini (float dt) {this. unschedule ("resumegrosini"); // The same is true for the following... CCNode grossini = this. getChildByTag (kTagGrossini); CCActionManager. sharedManager (). resume (grossini );}}}

 

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.