Cocos2D-Android-1之源码详解:2.ActionManagerTest
//现在我们开始注释,他的第一个activity
平陆网站制作公司哪家好,找创新互联公司!从网页设计、网站建设、微信开发、APP开发、成都响应式网站建设公司等网站项目制作,到程序开发,运营维护。创新互联公司自2013年起到现在10年的时间,我们拥有了丰富的建站经验和运维经验,来保证我们的工作的顺利进行。专注于网站建设就选创新互联公司。
package 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
//上面是下载地址
public class ActionManagerTest extends Activity {//是个普通的activity
// private static final String LOG_TAG = ActionManagerTest.class.getSimpleName();
//上面注释本来想获取包名
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;//这个ccg的view是opengl中的类,最终还是继承了sufaceview,先不做详细解释
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//没有标题
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);//产生这个avtivity下的surfaceview
CCDirector director = CCDirector.sharedDirector();//获得“导演”
director.attachInView(mGLSurfaceView);//把背景给导演,导演一会把数据画上去
director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);//横屏显示
setContentView(mGLSurfaceView);//展示出这个view
// show FPS
CCDirector.sharedDirector().setDisplayFPS(true);//显示fps分辨率
// frames per second
CCDirector.sharedDirector().setAnimationInterval(1.0f / 30);//设置动画多少秒刷一帧
CCScene scene = CCScene.node();//获得一个场景
scene.addChild(nextAction());//给场景添加一个子类
// Make the Scene active
CCDirector.sharedDirector().runWithScene(scene);//导演开始放映
}
static int sceneIdx = -1;
static Class> transitions[] = {
CrashTest.class,
LogicTest.class,
PauseTest.class,
RemoveTest.class,
Issue835.class,
};
@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();
// CCTextureCache.sharedTextureCache().removeAllTextures();
}
//
// Actions here
//
static CCLayer nextAction() {//实现的图层
sceneIdx++;//动画帧
sceneIdx = sceneIdx % transitions.length;//让动画的图层帧始终循环,比如0-》1-》2-》3-》4
return restartAction();//返回的是另一个类的属性,上述变量是全局静态变量
}
static CCLayer backAction() {//同理回退一个图层
sceneIdx--;
int total = transitions.length;
if (sceneIdx < 0)
sceneIdx += total;
return restartAction();
}
static CCLayer restartAction() {
try {
Class> c = transitions[sceneIdx];//按照数值计算显示的图层
return (CCLayer) c.newInstance();//返回显示的图层
} catch (Exception e) {
return null;
}
}
static abstract class ActionManagerDemo extends CCLayer {
CCTextureAtlas atlas;//定义一个大图
public ActionManagerDemo() {//构造方法
CGSize s = CCDirector.sharedDirector().winSize();//从导演那里得到宽高
CCLabel label = CCLabel.makeLabel(title(), "DroidSans", 32);//画一个label,字体是“DroidSans”,32的大小
addChild(label, 1);//把对象添加到图层
label.setPosition(CGPoint.make(s.width / 2, s.height / 2 - 50));设置label的位置
String subtitle = 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");
//菜单项目1,最后点击backCallback方法
CCMenuItemImage item2 = CCMenuItemImage.item("r1.png", "r2.png", this, "restartCallback");
//同理
CCMenuItemImage item3 = CCMenuItemImage.item("f1.png", "f2.png", this, "nextCallback");
CCMenu menu = CCMenu.menu(item1, item2, item3);//把3个项目添加进menu
menu.setPosition(CGPoint.make(0, 0));//设置菜单的位置
item1.setPosition(CGPoint.make(s.width / 2 - 100, 30));//屏幕的一半-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) {//重来一遍
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 CrashTest extends ActionManagerDemo {//继承了刚才建的通用类
public CrashTest() {//构造
super();//继承方法
CCSprite child = CCSprite.sprite("grossini.png");//用这张图建立一个精灵
child.setPosition(CGPoint.ccp(200,200));//设置这个精灵的地点
addChild(child, 1);//把这个图层添加到图层中
//Sum of all action's duration is 1.5 second.
child.runAction(CCRotateBy.action(1.5f, 90));//让这个图像执行一个旋转动作1.5秒,90度,立即执行,是旋转,不是旋转到
child.runAction(CCSequence.actions(//执行一个队列,挨个运行,是个迭代类
CCDelayTime.action(1.4f),//队列中有第一个是延时1.4秒
CCFadeOut.action(1.1f) ));//淡出在1.1秒
//After 1.5 second, self will be removed.
this.runAction(CCSequence.actions(
CCDelayTime.action(1.4f),//延迟1.4秒
CCCallFunc.action(this, "removeThis")//执行方法,这个类中的removeThis方法
));
}
public void removeThis() {
this.getParent().removeChild(this, true);//把他从他父类立去除,以后在父类里就找不到也连接不到他了
this.nextCallback(null);//播下一个,括号里本来就是个没用的参数..
}
@Override
public String title(){
return "Test 1. Should not crash";//返回Test 1. Should not crash
}
}
static class LogicTest extends ActionManagerDemo {//同理
public LogicTest() {
super();
CCSprite grossini = CCSprite.sprite("grossini.png");//创建精灵,同理
addChild(grossini);
grossini.setPosition(CGPoint.ccp(200,200));
grossini.runAction(CCSequence.actions( //执行动作
CCMoveBy.action(1.0f, CGPoint.ccp(150,0)),//相对移动
CCCallFuncN.action(this, "bugMe")));//执行bugMe方法
}
public void bugMe(CCNode node) {
node.stopAllActions(); //After this stop next action not working, if remove this stop everything is working
node.runAction(CCScaleTo.action(2.0f, 2.0f));//比例放大
}
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");//同理创建精灵
addChild(grossini, 0, kTagGrossini);
grossini.setPosition(CGPoint.ccp(200,200));
CCAction action = CCMoveBy.action(1.0f, CGPoint.ccp(150,0));//动作相对移动,CCMoveBy是个静态类,但是他的action方法在内部创建了他的实例,CCMoveBy就是把moveto的ccdelta值变成了自己的pos
CCActionManager.sharedManager().addAction(action, grossini, true);//得到动作控制器,用这个动作控制器,把action放进grossini节点中,动作控制器是零时建的,但是他自己的引用是个静态变量,所以只有一个
this.schedule("unpause", 3);//设置一个时间表,间隔3
}
public void unpause(float dt) {
unschedule("unpause");//这就停止了..
CCNode node = getChildByTag(kTagGrossini);//得到这样一个节点
CCActionManager.sharedManager().resume(node);//恢复节点,就是让暂停状态为false;
}
@Override
public String title() {
return "Pause Test";
}
public String subtitle() {
return "After 3 seconds grossini should move";
}
}
static class RemoveTest extends ActionManagerDemo {//同理
public RemoveTest() {
super();
CCMoveBy move = CCMoveBy.action(2,CGPoint.ccp(200,0));//移动
CCCallFunc callback = CCCallFunc.action(this, "stopAction");//得到某个函数
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) {//以下均同理...
CCNode sprite = getChildByTag(kTagGrossini);
sprite.stopAction(kTagSequence);
}
public String title() {
return "Remove Test";
}
public String subtitle() {
return "Should not crash. Testing issue #841";//以下均同理...
}
}
static class Issue835 extends ActionManagerDemo {//同理
public Issue835() {
super();
}
public void onEnter() {
super.onEnter();
CGSize s = CCDirector.sharedDirector().winSize();//仍然是获得窗口大小
CCSprite grossini = CCSprite.sprite("grossini.png");//以下均同理...
addChild(grossini, 0, kTagGrossini);
grossini.setPosition(CGPoint.ccp(s.width/2, s.height/2));
// An action should be scheduled before calling pause, otherwise pause won't pause a non-existang target
grossini.runAction(CCScaleBy.action(2.0f, 2.0f));
CCActionManager.sharedManager().pause(grossini);//以下均同理...
grossini.runAction(CCRotateBy.action(2.0f, 360));
this.schedule("resumeGrossini", 3);
}
public String title() {
return "Issue 835";
}
public String subtitle() {
return "Grossini only rotate/scale in 3 seconds";
}
public void resumeGrossini(float dt) {
this.unschedule("resumeGrossini");//以下均同理...
CCNode grossini = this.getChildByTag(kTagGrossini);
CCActionManager.sharedManager().resume(grossini);
}
}
}
分享标题:Cocos2D-Android-1之源码详解:2.ActionManagerTest
链接URL:http://azwzsj.com/article/jieiio.html