Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Cocos2D-Android-1之源碼詳解:7.CocosNodeTest

Cocos2D-Android-1之源碼詳解:7.CocosNodeTest

編輯:關於Android編程

package org.cocos2d.tests;   import org.cocos2d.actions.UpdateCallback; import org.cocos2d.actions.base.CCAction; import org.cocos2d.actions.base.CCRepeatForever; import org.cocos2d.actions.camera.CCOrbitCamera; import org.cocos2d.actions.ease.CCEaseInOut; import org.cocos2d.actions.instant.CCCallFuncN; import org.cocos2d.actions.interval.CCIntervalAction; import org.cocos2d.actions.interval.CCMoveBy; import org.cocos2d.actions.interval.CCRotateBy; import org.cocos2d.actions.interval.CCScaleBy; import org.cocos2d.actions.interval.CCSequence; import org.cocos2d.config.ccMacros; import org.cocos2d.layers.CCLayer; import org.cocos2d.layers.CCScene; import org.cocos2d.menus.CCMenu; import org.cocos2d.menus.CCMenuItem; 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.CCCamera; import org.cocos2d.opengl.CCGLSurfaceView; import org.cocos2d.particlesystem.CCParticleFire; import org.cocos2d.particlesystem.CCParticleSun; import org.cocos2d.types.CGPoint; import org.cocos2d.types.CGRect; import org.cocos2d.types.CGSize; import org.cocos2d.types.ccColor3B;   import android.app.Activity; import android.os.Bundle; import android.view.Window; import android.view.WindowManager;   // // cocos node tests // a cocos2d example // http://www.cocos2d-iphone.org // public class CocosNodeTest extends Activity {//同樣是個activity // private static final String LOG_TAG = CocosNodeTest.class.getSimpleName(); private CCGLSurfaceView mGLSurfaceView;//創建一個view字段     @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE);//老3種開頭設置 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);   mGLSurfaceView = new CCGLSurfaceView(this);//必要的生成view新對象,並獲得this setContentView(mGLSurfaceView);//把這個view映射到activity上     // attach the OpenGL view to a window CCDirector.sharedDirector().attachInView(mGLSurfaceView);//把這個view的控制權交給導演..   // set landscape mode CCDirector.sharedDirector().setLandscape(false);//還是這幾個常見屬性不贅述了   // show FPS CCDirector.sharedDirector().setDisplayFPS(true);   // frames per second CCDirector.sharedDirector().setAnimationInterval(1.0f / 30);   CCScene scene = CCScene.node();//創建一個場景 scene.addChild(nextAction());//給場景添加一個圖層   // Make the Scene active CCDirector.sharedDirector().runWithScene(scene); }   @Override public void onStart() {//還是這幾個方法 super.onStart();   }   @Override public void onPause() { super.onPause();   CCDirector.sharedDirector().onPause(); }   @Override public void onResume() { super.onResume();   CCDirector.sharedDirector().onResume(); }   @Override public void onDestroy() { super.onDestroy();   CCDirector.sharedDirector().end(); }   public static final int kTagSprite1 = 1; public static final int kTagSprite2 = 2; public static final int kTagSprite3 = 3; public static final int kTagSlider  = 4;   static int sceneIdx = -1;//一個class的集合 static Classtransitions[] = { Test2.class, Test4.class, Test5.class, Test6.class, StressTest1.class, StressTest2.class, NodeToWorld.class, SchedulerTest1.class, CameraOrbitTest.class, CameraZoomTest.class, CameraCenterTest.class, };   static CCLayer nextAction() {//還是這3個方法   sceneIdx++; sceneIdx = sceneIdx % transitions.length;   return restartAction(); }   static CCLayer backAction() { sceneIdx--; int total = transitions.length; if (sceneIdx < 0) sceneIdx += total; return restartAction(); }   static CCLayer restartAction() { Classc = transitions[sceneIdx]; try { return (CCLayer) c.newInstance(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InstantiationException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; }     static abstract class TestDemo extends CCLayer {//進入正式的圖層界面 public TestDemo() { super();   CGSize s = CCDirector.sharedDirector().winSize();//得到屏幕大小   CCLabel label = CCLabel.makeLabel(title(), "DroidSans", 32);//一個label的新建 addChild(label);//添加進去 label.setPosition(CGPoint.ccp(s.width/2, s.height-50));//設置位置   String subtitle = subtitle();//得到字符串 if( subtitle != null ) { 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"); CCMenuItemImage item2 = CCMenuItemImage.item("r1.png", "r2.png", this, "restartCallback"); CCMenuItemImage item3 = CCMenuItemImage.item("f1.png", "f2.png", this, "nextCallback");//3個按鈕   CCMenu menu = CCMenu.menu(item1, item2, item3);//按鈕添加到菜單中 menu.setPosition(CGPoint.zero()); item1.setPosition(CGPoint.make(s.width / 2 - 100, 30)); item2.setPosition(CGPoint.make(s.width / 2, 30)); item3.setPosition(CGPoint.make(s.width / 2 + 100, 30)); addChild(menu, -1);//添加到場景 }   public void restartCallback(Object sender) {//3個方法 CCScene s = CCScene.node(); s.addChild(restartAction()); CCDirector.sharedDirector().replaceScene(s); }   public void nextCallback(Object sender) { CCScene s = CCScene.node(); s.addChild(nextAction()); CCDirector.sharedDirector().replaceScene(s); }   public void backCallback(Object sender) { CCScene s = CCScene.node(); s.addChild(backAction()); CCDirector.sharedDirector().replaceScene(s); }   public String title() { return "No title"; }   public String subtitle() { return null; } }   static class Test2 extends TestDemo {//進入正式的圖層   public void onEnter() { super.onEnter();   CGSize s = CCDirector.sharedDirector().winSize();   CCSprite sp1 = CCSprite.sprite("grossinis_sister1.png");//建立4個精靈 CCSprite sp2 = CCSprite.sprite("grossinis_sister2.png"); CCSprite sp3 = CCSprite.sprite("grossinis_sister1.png"); CCSprite sp4 = CCSprite.sprite("grossinis_sister2.png");   sp1.setPosition(CGPoint.make(100, s.height / 2));//設好位置 sp2.setPosition(CGPoint.make(380, s.height / 2)); addChild(sp1); addChild(sp2);   sp3.setScale(0.25f);//設置縮放比例 sp4.setScale(0.25f);   sp1.addChild(sp3); sp2.addChild(sp4);   CCIntervalAction a1 = CCRotateBy.action(2, 360);//延遲動作2秒走360度 CCIntervalAction a2 = CCScaleBy.action(2, 2);//延遲動作縮放   CCAction action1 = CCRepeatForever.action(CCSequence.actions(a1, a2, a2.reverse()));//永久動作重復剛才的行為 CCAction action2 = CCRepeatForever.action(CCSequence.actions(a1.copy(), a2.copy(), a2.reverse()));//2個動作拷貝,1個動作相反   sp2.setAnchorPoint(CGPoint.zero());//設置錨點   sp1.runAction(action1);//精靈執行動作 sp2.runAction(action2); }   public String title() { return "anchorPoint and children"; } }   static class Test4 extends TestDemo {   public Test4() { super();   CCSprite sp1 = CCSprite.sprite("grossinis_sister1.png");//創建精靈 CCSprite sp2 = CCSprite.sprite("grossinis_sister2.png");   sp1.setPosition(CGPoint.make(100, 160));//設置頂點 sp2.setPosition(CGPoint.make(380, 160));   addChild(sp1, 0, 2);//添加子類 addChild(sp2, 0, 3);   schedule("delay2", 2.0f);//時間表,每2秒執行一次延遲2 schedule("delay4", 4.0f); }   public void delay2(float dt) {//執行的時候會返回實時的間隔值 CCNode node = getChildByTag(2);//得到2號節點的對象 CCIntervalAction action1 = CCRotateBy.action(1, 360);//一個延時動作 node.runAction(action1);//讓這個節點執行動作 }   public void delay4(float dt) {//延時動作4 unschedule("delay4");//只執行1次 removeChildByTag(3, false);//不要這個子類但保留動作 }   public String title() { return "Tags"; } }   static class Test5 extends TestDemo {//常見的生成類   public Test5() { super();   CCSprite sp1 = CCSprite.sprite("grossinis_sister1.png"); CCSprite sp2 = CCSprite.sprite("grossinis_sister2.png");   sp1.setPosition(CGPoint.make(100, 160)); sp2.setPosition(CGPoint.make(380, 160));   CCIntervalAction rot = CCRotateBy.action(2, 360); CCIntervalAction rot_back = rot.reverse(); CCAction forever = CCRepeatForever.action( CCSequence.actions(rot, rot_back)); CCAction forever2 = forever.copy();   forever.setTag(101); forever2.setTag(102);   addChild(sp1, 0, kTagSprite1); addChild(sp2, 0, kTagSprite2);   sp1.runAction(forever); sp2.runAction(forever2);   schedule("addAndRemove", 2.0f); }   public void addAndRemove(float dt) {//先移除然第一個保留動作,然後重新添加 CCNode sp1 = getChildByTag(kTagSprite1); CCNode sp2 = getChildByTag(kTagSprite2);   removeChild(sp1, false); removeChild(sp2, true);   addChild(sp1, 0, kTagSprite1); addChild(sp2, 0, kTagSprite2); }   public String title() { return "remove and cleanup"; } }   static class Test6 extends TestDemo {//常見的創建類   public Test6() { super();   CCSprite sp1 = CCSprite.sprite("grossinis_sister1.png"); CCSprite sp11 = CCSprite.sprite("grossinis_sister1.png");   CCSprite sp2 = CCSprite.sprite("grossinis_sister2.png"); CCSprite sp21 = CCSprite.sprite("grossinis_sister2.png");   sp1.setPosition(CGPoint.make(100, 160)); sp2.setPosition(CGPoint.make(380, 160));     CCIntervalAction rot = CCRotateBy.action(2, 360);//旋轉 CCIntervalAction rot_back = rot.reverse(); CCAction forever1 = CCRepeatForever.action(CCSequence.actions(rot, rot_back));//永久動作 CCAction forever11 = forever1.copy();//拷貝   CCAction forever2 = forever1.copy(); CCAction forever21 = forever1.copy();   addChild(sp1, 0, kTagSprite1); sp1.addChild(sp11); addChild(sp2, 0, kTagSprite2); sp2.addChild(sp21);   sp1.runAction(forever1); sp11.runAction(forever11); sp2.runAction(forever2); sp21.runAction(forever21);   schedule("addAndRemove", 2.0f);//每2秒執行一次 }   public void addAndRemove(float dt) {//同上一個類中的方法 CCNode sp1 = getChildByTag(kTagSprite1); CCNode sp2 = getChildByTag(kTagSprite2);   removeChild(sp1, false); removeChild(sp2, true);   addChild(sp1, 0, kTagSprite1); addChild(sp2, 0, kTagSprite2); }     public String title() { return "remove/cleanup with children"; } }   static class StressTest1 extends TestDemo {//又一個常見類 public StressTest1() { super();   CGSize s = CCDirector.sharedDirector().winSize();   CCSprite sp1 = CCSprite.sprite("grossinis_sister1.png"); addChild(sp1, 0, kTagSprite1);   sp1.setPosition(CGPoint.ccp(s.width/2, s.height/2));   schedule("shouldNotCrash", 1.0f);//每一秒執行一次 }   public void shouldNotCrash(float delta) { unschedule("shouldNotCrash");//不再執行   CGSize s = CCDirector.sharedDirector().winSize();   // if the node has timers, it crashes CCNode explosion = CCParticleSun.node();//得到一個太陽離子效果   // if it doesn't, it works Ok. //     CocosNode *explosion = [Sprite spriteWithFile:@"grossinis_sister2.png"];   explosion.setPosition(CGPoint.ccp(s.width/2, s.height/2));//設置位置   runAction(CCSequence.actions(//執行動作 CCRotateBy.action(2.0f, 360), CCCallFuncN.action(this, "removeMe")//執行方法 ));   addChild(explosion);//添加這個效果節點 }   // remove public void removeMe(Object node) { this.getParent().removeChild((CCNode)node, true);//移除並且停止動畫 nextCallback(this); }   public String title(){ return "stress test #1: no crashes"; } }   static class StressTest2 extends TestDemo {//測試2 public StressTest2() { // // Purpose of this test: // Objects should be released when a layer is removed //   super();   CGSize s = CCDirector.sharedDirector().winSize();//同理不再贅述   CCLayer sublayer = CCLayer.node();   CCSprite sp1 = CCSprite.sprite("grossinis_sister1.png"); sp1.setPosition(CGPoint.ccp(80, s.height/2));   CCIntervalAction move = CCMoveBy.action(3.0f, CGPoint.ccp(350,0)); CCIntervalAction move_ease_inout3 = CCEaseInOut.action(move.copy(), 2.0f); CCIntervalAction move_ease_inout_back3 = move_ease_inout3.reverse(); CCIntervalAction seq3 = CCSequence.actions(move_ease_inout3, move_ease_inout_back3); sp1.runAction(CCRepeatForever.action(seq3)); sublayer.addChild(sp1, 1);   CCParticleFire fire = CCParticleFire.node();//著火了效果.. fire.setPosition(CGPoint.ccp(80, s.height/2-50)); CCIntervalAction copy_seq3 = seq3.copy();//一個延時動作 fire.runAction(CCRepeatForever.action(copy_seq3)); sublayer.addChild(fire, 2);   schedule("shouldNotLeak", 6.0f);//執行方法6秒後   addChild(sublayer, 0, kTagSprite1); }   public void shouldNotLeak(float dt) { unschedule("shouldNotLeak"); CCNode sublayer = getChildByTag(kTagSprite1); sublayer.removeAllChildren(true);//移除所有 }   public String title() { return "stress test #2: no leaks"; } }   static class CustomNode extends CCNode { public static CustomNode node() { return new CustomNode(); }   public void doSomething(float dt) { ccMacros.CCLOG("CustomNode", "do something..."); } }   static class SchedulerTest1 extends TestDemo { public SchedulerTest1() { // // Purpose of this test: // Scheduler should be released //             super();               CCNode layer =  CustomNode.node();             // ccMacros.CCLog(@"retain count after init is %d", [layer retainCount]);                // 1               addChild(layer, 0);             // ccMacros.CCLog(@"retain count after addChild is %d", [layer retainCount]);      // 2               layer.schedule("doSomething");             // ccMacros.CCLog(@"retain count after schedule is %d", [layer retainCount]);      // 3               layer.unschedule("doSomething");             // ccMacros.CCLog(@"retain count after unschedule is %d", [layer retainCount]); // STILL 3! }   public String title() { return "cocosnode scheduler test #1"; } }   static class NodeToWorld extends TestDemo {//又一個 public NodeToWorld() {             super();               //             // This code tests that nodeToParent works OK:             //  - It tests different anchor Points             //  - It tests different children anchor points               CCSprite back = CCSprite.sprite("background3.png");//背景圖片3             addChild(back, -10);//把背景放在老低下             back.setAnchorPoint(CGPoint.ccp(0,0));             CGSize backSize = back.getContentSize(); //同理的按鈕菜單             CCMenuItem item = CCMenuItemImage.item("btn-play-normal.png", "btn-play-selected.png");             CCMenu menu = CCMenu.menu(item);             menu.alignItemsVertically();             menu.setPosition(CGPoint.ccp(backSize.width/2, backSize.height/2));             back.addChild(menu); //瞬時動作             CCIntervalAction rot = CCRotateBy.action(5, 360);             CCRepeatForever fe = CCRepeatForever.action(rot);//永久動作             item.runAction(fe);               CCIntervalAction move = CCMoveBy.action(3.0f, CGPoint.ccp(200,0));             CCIntervalAction move_back = move.reverse();             CCSequence seq = CCSequence.actions(move, move_back);             CCRepeatForever fe2 = CCRepeatForever.action(seq);             back.runAction(fe2);//執行動作         }   public String title() { return "nodeToParent transform"; } }   static class CameraOrbitTest extends TestDemo {//一個重要的例子,背景可以翻動,精靈也可以.總之看起來就像立體一樣,但是是張紙一樣的立體 public void onEnter() {             super.onEnter(); CCDirector.sharedDirector().setProjection(CCDirector.kCCDirectorProjection3D);//設置為參數組2 }   public void onExit() { CCDirector.sharedDirector().setProjection(CCDirector.kCCDirectorProjection2D);//退出時還原參數             super.onExit(); }           public CameraOrbitTest() {             super();               CGSize s = CCDirector.sharedDirector().winSize();//得到屏幕大小               CCSprite p = CCSprite.sprite("background3.png");//依然是這個背景             addChild(p, 0);//添加進去             p.setPosition(CGPoint.ccp(s.width/2, s.height/2));//設置位置             p.setOpacity(128);//暧昧度...也就是不透明度               CCSprite sprite = null;//精靈             CCOrbitCamera orbit = null;//攝像頭               // LEFT             sprite = CCSprite.sprite("grossini.png");//得到精靈             sprite.setScale(0.5f);             p.addChild(sprite, 0);             sprite.setPosition(CGPoint.ccp(s.width/4*1, s.height/2));             orbit = CCOrbitCamera.action(2.0f, 1, 0, 0, 360, 0, 0);//重要的攝像頭變換類,但是其實是精靈在變換,在2秒內從1,0,0的角度,變換0,360,0             sprite.runAction(CCRepeatForever.action(orbit));//永久重復運動               // CENTER             sprite = CCSprite.sprite("grossini.png");//以下兩個同理             sprite.setScale(1.0f);             p.addChild(sprite, 0);             sprite.setPosition(CGPoint.ccp(s.width/4*2, s.height/2));             orbit = CCOrbitCamera.action(2.0f, 1, 0, 0, 360, 45, 0);             sprite.runAction(CCRepeatForever.action(orbit));               // RIGHT             sprite = CCSprite.sprite("grossini.png");             sprite.setScale(2.0f);             p.addChild(sprite, 0);             sprite.setPosition(CGPoint.ccp(s.width/4*3, s.height/2));             orbit = CCOrbitCamera.action(2.0f, 1, 0, 0, 360, 90, -45);             sprite.runAction(CCRepeatForever.action(orbit));               // PARENT             orbit = CCOrbitCamera.action(10.0f, 1, 0, 0, 360, 0, 90);//這是讓場景也變換             p.runAction(CCRepeatForever.action(orbit));               this.setScale(1.0f);//設置縮放比例1         }   public String title() { return "Camera Orbit test"; } }   static class CameraZoomTest extends TestDemo {//變焦距 public void onEnter() { super.onEnter(); CCDirector.sharedDirector().setProjection(CCDirector.kCCDirectorProjection3D);//當得到焦點時轉換為3d狀態。也只有顯示的時候才會得到焦點 }   public void onExit() { CCDirector.sharedDirector().setProjection(CCDirector.kCCDirectorProjection2D); super.onExit(); }//失去焦點,或者關閉時           public CameraZoomTest() {             super();               CGSize s = CCDirector.sharedDirector().winSize();//得到屏幕大小               CCSprite sprite = null;             CCCamera cam = null;               // LEFT             sprite = CCSprite.sprite("grossini.png");//精靈             addChild(sprite, 0);             sprite.setPosition(CGPoint.ccp(s.width/4*1, s.height/2));             cam = sprite.getCamera();             cam.setEye(0, 0, 415);//給相機設置看的點               // CENTER             sprite = CCSprite.sprite("grossini.png");             addChild(sprite, 0, 40);             sprite.setPosition(CGPoint.ccp(s.width/4*2, s.height/2));             //     cam = [sprite camera];             //     [cam setEyeX:0 eyeY:0 eyeZ:415/2];               // RIGHT             sprite = CCSprite.sprite("grossini.png");             addChild(sprite, 0, 20);             sprite.setPosition(CGPoint.ccp(s.width/4*3, s.height/2));             //     cam = [sprite camera];             //     [cam setEyeX:0 eyeY:0 eyeZ:-485];             //     [cam setCenterX:0 centerY:0 centerZ:0];                 schedule(new UpdateCallback() { @Override public void update(float d) { updateEye(d);//更新眼睛 } });         }           static float z = 0;   public void updateEye(float dt) { CCNode sprite = null; CCCamera cam = null;   z += dt * 100;   sprite = getChildByTag(20); cam = sprite.getCamera(); cam.setEye(0, 0, z);   sprite = getChildByTag(40); cam = sprite.getCamera(); cam.setEye(0, 0, z); }   public String title() { return "Camera Zoom test"; } }   static class CameraCenterTest extends TestDemo {//相機在中心測試           public CameraCenterTest() {             super();               CGSize s = CCDirector.sharedDirector().winSize();               CCSprite sprite = null;             CCOrbitCamera orbit = null;               // LEFT-TOP             sprite = CCSprite.sprite("grossini.png");             addChild(sprite, 0);             sprite.setPosition(CGPoint.ccp(s.width/5*1, s.height/5*1));             sprite.setColor(ccColor3B.ccRED);             sprite.setTextureRect(CGRect.make(0, 0, 120, 50));             orbit = CCOrbitCamera.action(10, 1, 0, 0, 360, 0, 0);//相機動作             sprite.runAction(CCRepeatForever.action(orbit));               // LEFT-BOTTOM             sprite = CCSprite.sprite("grossinis_sister1.png");             addChild(sprite, 0, 40);             sprite.setPosition(CGPoint.ccp(s.width/5*1, s.height/5*4));             sprite.setColor(ccColor3B.ccBLUE);             sprite.setTextureRect(CGRect.make(0, 0, 120, 50));             orbit = CCOrbitCamera.action(10.0f, 1, 0, 0, 360, 0, 0);//同理             sprite.runAction(CCRepeatForever.action(orbit));               // RIGHT-TOP             sprite = CCSprite.sprite("grossinis_sister2.png");             addChild(sprite, 0);             sprite.setPosition(CGPoint.ccp(s.width/5*4, s.height/5*1));             sprite.setColor(ccColor3B.ccYELLOW);             sprite.setTextureRect(CGRect.make(0, 0, 120, 50));             orbit = CCOrbitCamera.action(10.0f, 1, 0, 0, 360, 0, 0);//同理             sprite.runAction(CCRepeatForever.action(orbit));               // RIGHT-BOTTOM             sprite = CCSprite.sprite("smoke.png");             addChild(sprite, 0, 40);             sprite.setPosition(CGPoint.ccp(s.width/5*4, s.height/5*4));             sprite.setColor(ccColor3B.ccGREEN);             sprite.setTextureRect(CGRect.make(0, 0, 120, 50));             orbit = CCOrbitCamera.action(10.0f, 1, 0, 0, 360, 0, 0);//同理             sprite.runAction(CCRepeatForever.action(orbit));               // CENTER             sprite = CCSprite.sprite("snow.png");             addChild(sprite, 0, 40);             sprite.setPosition(CGPoint.ccp(s.width/2, s.height/2));             sprite.setColor(ccColor3B.ccWHITE);             sprite.setTextureRect(CGRect.make(0, 0, 120, 50));             orbit = CCOrbitCamera.action(10.0f, 1, 0, 0, 360, 0, 0);//同理             sprite.runAction(CCRepeatForever.action(orbit));         }   public String title() { return "Camera Center test"; }   public String subtitle() { return "Sprites should rotate at the same speed"; } } } //粒子效果:CCParticleFire 火焰效果 //CCParticleSun.node();太陽離子

上一篇:/kf/201512/453002.html
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved