Cocos2D-Android-1之源码详解:7.CocosNodeTest

前端之家收集整理的这篇文章主要介绍了Cocos2D-Android-1之源码详解:7.CocosNodeTest前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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 Class<?> transitions[] = {

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() {

Class<?> c = 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","restartCallback");

CCMenuItemImage item3 = CCMenuItemImage.item("f1.png","f2.png","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,2);//添加子类

addChild(sp2,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,kTagSprite1);

addChild(sp2,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,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));



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,kTagSprite1);

sp1.addChild(sp11);

addChild(sp2,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,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,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,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,360,0);//重要的摄像头变换类,但是其实是精灵在变换,在2秒内从1,0的角度,变换0,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,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,90,-45);

sprite.runAction(CCRepeatForever.action(orbit));


// PARENT

orbit = CCOrbitCamera.action(10.0f,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,s.height/2));

cam = sprite.getCamera();

cam.setEye(0,415);//给相机设置看的点


// CENTER

sprite = CCSprite.sprite("grossini.png");

addChild(sprite,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,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,z);


sprite = getChildByTag(40);

cam = sprite.getCamera();

cam.setEye(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,120,50));

orbit = CCOrbitCamera.action(10,0);//相机动作

sprite.runAction(CCRepeatForever.action(orbit));


// LEFT-BOTTOM

sprite = CCSprite.sprite("grossinis_sister1.png");

addChild(sprite,40);

sprite.setPosition(CGPoint.ccp(s.width/5*1,s.height/5*4));

sprite.setColor(ccColor3B.ccBLUE);

sprite.setTextureRect(CGRect.make(0,50));

orbit = CCOrbitCamera.action(10.0f,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);//同理

sprite.runAction(CCRepeatForever.action(orbit));


// RIGHT-BOTTOM

sprite = CCSprite.sprite("smoke.png");

addChild(sprite,40);

sprite.setPosition(CGPoint.ccp(s.width/5*4,s.height/5*4));

sprite.setColor(ccColor3B.ccGREEN);

sprite.setTextureRect(CGRect.make(0,0);//同理

sprite.runAction(CCRepeatForever.action(orbit));


// CENTER

sprite = CCSprite.sprite("snow.png");

addChild(sprite,40);

sprite.setPosition(CGPoint.ccp(s.width/2,s.height/2));

sprite.setColor(ccColor3B.ccWHITE);

sprite.setTextureRect(CGRect.make(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();太阳离子

猜你在找的Cocos2d-x相关文章