Cocos2D-Android-1之源码详解:3.ActionsTest

package org.cocos2d.tests;

import java.io.IOException;

import java.io.InputStream;

import javax.microedition.khronos.opengles.GL10;

import org.cocos2d.actions.base.CCAction;

import org.cocos2d.actions.base.CCFollow;

import org.cocos2d.actions.base.CCRepeatForever;

import org.cocos2d.actions.camera.CCOrbitCamera;

import org.cocos2d.actions.instant.CCCallFunc;

import org.cocos2d.actions.instant.CCCallFuncN;

import org.cocos2d.actions.instant.CCCallFuncND;

import org.cocos2d.actions.instant.CCHide;

import org.cocos2d.actions.instant.CCPlace;

import org.cocos2d.actions.instant.CCShow;

import org.cocos2d.actions.instant.CCToggleVisibility;

import org.cocos2d.actions.interval.CCAnimate;

import org.cocos2d.actions.interval.CCBezierBy;

import org.cocos2d.actions.interval.CCBezierTo;

import org.cocos2d.actions.interval.CCBlink;

import org.cocos2d.actions.interval.CCDelayTime;

import org.cocos2d.actions.interval.CCFadeIn;

import org.cocos2d.actions.interval.CCFadeOut;

import org.cocos2d.actions.interval.CCJumpBy;

import org.cocos2d.actions.interval.CCJumpTo;

import org.cocos2d.actions.interval.CCMoveBy;

import org.cocos2d.actions.interval.CCMoveTo;

import org.cocos2d.actions.interval.CCPropertyAction;

import org.cocos2d.actions.interval.CCRepeat;

import org.cocos2d.actions.interval.CCRotateBy;

import org.cocos2d.actions.interval.CCRotateTo;

import org.cocos2d.actions.interval.CCScaleBy;

import org.cocos2d.actions.interval.CCScaleTo;

import org.cocos2d.actions.interval.CCSequence;

import org.cocos2d.actions.interval.CCSpawn;

import org.cocos2d.actions.interval.CCTintBy;

import org.cocos2d.actions.interval.CCTintTo;

import org.cocos2d.config.ccMacros;

import org.cocos2d.layers.CCLayer;

import org.cocos2d.layers.CCScene;

import org.cocos2d.menus.CCMenu;

import org.cocos2d.menus.CCMenuItemImage;

import org.cocos2d.nodes.CCAnimation;

import org.cocos2d.nodes.CCDirector;

import org.cocos2d.nodes.CCLabel;

import org.cocos2d.nodes.CCNode;

import org.cocos2d.nodes.CCSprite;

import org.cocos2d.opengl.CCDrawingPrimitives;

import org.cocos2d.opengl.CCGLSurfaceView;

import org.cocos2d.opengl.CCTexture2D;

import org.cocos2d.opengl.GLResourceHelper;

import org.cocos2d.opengl.GLResourceHelper.Resource;

import org.cocos2d.types.CCBezierConfig;

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.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.os.Bundle;

import android.view.Window;

import android.view.WindowManager;

// ActionsTest, there is a downloadable demo here:

//  http://code.google.com/p/cocos2d-android-1/downloads/detail?name=ActionsTest.3gp&can=2&q=#makechanges

//

public class ActionsTest extends Activity {//依然是个activity的类

private static final String LOG_TAG = ActionsTest.class.getSimpleName();//得到包名

public static final int kTagAnimationDance = 1;//这个设为1

private CCGLSurfaceView mGLSurfaceView;//得到一个cocose2d的surfaceview

@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);//以自己的上下文建立surface,同时让导演中的上下文变成this

CCDirector director = CCDirector.sharedDirector();//得到导演

director.attachInView(mGLSurfaceView);//把这个view链接到导演

director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);//横屏

setContentView(mGLSurfaceView);//把这个view展示刀屏幕上

// show FPS

CCDirector.sharedDirector().setDisplayFPS(true);//显示帧频率

// frames per second

CCDirector.sharedDirector().setAnimationInterval(1.0f / 30);//多少秒动一下

//导演是个全局的变量集合。

CCScene scene = CCScene.node();//生成一个新场景,因为他不提供new方法所以这样写

scene.addChild(nextAction());//给场景添加一个图层

// Make the Scene active

CCDirector.sharedDirector().runWithScene(scene);//把场景推到栈顶,并且下一个场景设为当前场景

}

static int sceneIdx = -1;

static Class<?> transitions[] = {//一个类列表,方便用来实现不同的窗口

ActionManual.class,

ActionMove.class,

ActionRotate.class,

ActionScale.class,

ActionJump.class,

ActionBezier.class,

ActionBlink.class,

ActionFade.class,

ActionTint.class,

ActionAnimate.class,

ActionSequence.class,

ActionSequence2.class,

ActionSpawn.class,

ActionReverse.class,

ActionDelayTime.class,

ActionRepeat.class,

ActionRepeatForever.class,

ActionRotateToRepeat.class,

ActionRotateJerk.class,

ActionCallFunc.class,

ActionCallFuncND.class,

ActionReverseSequence.class,

ActionReverseSequence2.class,

ActionOrbit.class,

ActionFollow.class,

ActionProperty.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;

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 class ActionDemo extends CCLayer {

CCSprite grossini;

CCSprite tamara;

CCSprite kathia;

public ActionDemo() {

super();

// Example:

// You can create a sprite using a Texture2D

Bitmap bmp = null;

InputStream is;//用流打开一个图片,变成

try {

is = CCDirector.theApp.getAssets().open("grossini.png");

bmp = BitmapFactory.decodeStream(is);

is.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

// Keep copy for reinit texture after Activity pause

final Bitmap bmpCopy = bmp.copy(bmp.getConfig(), false);//得到一个不可变的图像

bmp.recycle();//原图回收

CCTexture2D tex = new CCTexture2D();//新建一个纹理

tex.setLoader(new GLResourceHelper.GLResourceLoader() {

@Override

public void load(Resource res) {

Bitmap bmpForInit = bmpCopy.copy(bmpCopy.getConfig(), false);//依然是得到一个不能改变的拷贝

((CCTexture2D)res).initWithImage(bmpForInit);//以此来作为res的的纹理

}

});

grossini = CCSprite.sprite(tex);//把纹理变为精灵

// or just use://或者这样用

// grossini = CCSprite.sprite(bmp);

// and better not to use this way at all(this is better for manually generated tex)//最好别这样用

// because you have to store bmp copy in memory.

// use for assets:

// CCTexture2D tex = CCTextureCache.sharedTextureCache().addImage("grossini.png");//他会占用内存

// grossini = CCSprite.sprite(tex);

// Example:

// Or you can create an sprite using a filename. PNG, JPEG and BMP files are supported. Probably TIFF too

tamara = CCSprite.sprite("grossinis_sister1.png");//生成2个精灵

kathia = CCSprite.sprite("grossinis_sister2.png");

addChild(grossini, 1);

addChild(tamara, 2);

addChild(kathia, 3);

CGSize s = CCDirector.sharedDirector().winSize();//生成一个大小

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

tamara.setPosition(CGPoint.ccp(s.width/2, 2*s.height/3));

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

CCLabel label = CCLabel.makeLabel(title(), "DroidSans", 24);//生成一个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");

CCMenu menu = CCMenu.menu(item1, item2, item3);//和之前的文章一样,生成菜单

menu.setPosition(CGPoint.zero());

item1.setPosition(CGPoint.ccp( s.width/2 - 100, 30));

item2.setPosition(CGPoint.ccp( s.width/2, 30));

item3.setPosition(CGPoint.ccp( 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 void alignSpritesLeft(int numberOfSprites) {//一个通用的方法用来设定不同的大小

CGSize s = CCDirector.sharedDirector().winSize();

if( numberOfSprites == 1 ) {

tamara.setVisible(false);

kathia.setVisible(false);

grossini.setPosition(CGPoint.ccp(60, s.height/2));

} else if( numberOfSprites == 2 ) {

kathia.setPosition(CGPoint.ccp(60, s.height/3));

tamara.setPosition(CGPoint.ccp(60, 2*s.height/3));

grossini.setVisible(false);

} else if( numberOfSprites == 3 ) {

grossini.setPosition(CGPoint.ccp(60, s.height/2));

tamara.setPosition(CGPoint.ccp(60, 2*s.height/3));

kathia.setPosition(CGPoint.ccp(60, s.height/3));

} else {

ccMacros.CCLOG(LOG_TAG, "ActionsTests: Invalid number of Sprites");

}

}

public void centerSprites(int numberOfSprites) {//同理

CGSize s = CCDirector.sharedDirector().winSize();

if( numberOfSprites == 1 ) {

tamara.setVisible(false);

kathia.setVisible(false);

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

} else if( numberOfSprites == 2 ) {

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

tamara.setPosition(CGPoint.ccp(2*s.width/3, s.height/2));

grossini.setVisible(false);

} else if( numberOfSprites == 3 ) {

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

tamara.setPosition(CGPoint.ccp(2*s.width/3, s.height/2));

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

} else {

ccMacros.CCLOG(LOG_TAG, "ActionsTests: Invalid number of Sprites");

}

}

public String title() {

return "No title";

}

public String subtitle() {

return null;

}

}

static class  ActionManual extends ActionDemo {//第一个类的解析,当执行这个模块时,将会运行如下脚本

public void onEnter() {

super.onEnter();

CGSize s = CCDirector.sharedDirector().winSize();

tamara.setScaleX(2.5f);//设置x的比例

tamara.setScaleY(-1.0f);//设置y的比例

tamara.setPosition(CGPoint.ccp(100,70));

tamara.setOpacity(128);//设置明暗度

grossini.setRotation(120);//设置旋转角度

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

grossini.setColor(ccColor3B.ccc3( 255,0,0));

kathia.setPosition(CGPoint.ccp(s.width-100, s.height/2));

kathia.setColor(ccColor3B.ccBLUE);

}

public String title() {

return "Manual Transformation";

}

}

static class ActionMove extends ActionDemo {//移动

public void onEnter() {

super.onEnter();

centerSprites(3);

CGSize s = CCDirector.sharedDirector().winSize();

CCMoveTo actionTo = CCMoveTo.action(2, CGPoint.ccp(s.width-40, s.height-40));//设置移动动作,2秒

CCMoveBy actionBy = CCMoveBy.action(2, CGPoint.ccp(80,80));//设置相对移动动作

CCMoveBy actionByBack = actionBy.reverse();//反向动作自动生成

tamara.runAction(actionTo);//运行

grossini.runAction(CCSequence.actions(actionBy, actionByBack));//组合连续动作,不同步

kathia.runAction(CCMoveTo.action(1, CGPoint.ccp(40,40)));

}

public String title() {

return "MoveTo / MoveBy";

}

}

static class ActionRotate extends ActionDemo {//旋转

public void onEnter() {

super.onEnter();

centerSprites(3);//3号位置,之前的一个设定位置的方法

CCRotateTo actionTo = CCRotateTo.action(2, 45);//旋转2秒内转到45

CCRotateTo actionTo2 = CCRotateTo.action(2, -45);

CCRotateTo actionTo0 = CCRotateTo.action(2, 0);

tamara.runAction(CCSequence.actions(actionTo, actionTo0));

CCRotateBy actionBy = CCRotateBy.action(2, 360);//顺时针相对转动360

CCRotateBy actionByBack = actionBy.reverse();

grossini.runAction(CCSequence.actions(actionBy, actionByBack));

kathia.runAction(CCSequence.actions(actionTo2, actionTo0.copy()));

}

public String title() {

return "RotateTo / RotateBy";

}

}

static class ActionScale extends ActionDemo {//大小变化

public void onEnter() {

super.onEnter();

centerSprites(3);

CCScaleTo actionTo = CCScaleTo.action(2, 0.5f);//2秒钟变小0.5比例

CCScaleBy actionBy = CCScaleBy.action(2, 2);//2秒变大2倍

CCScaleBy actionBy2 = CCScaleBy.action(2, 0.25f, 4.5f);//2秒,宽变小0.25,长变大4.5

CCScaleBy actionByBack = actionBy.reverse();

tamara.runAction(actionTo);

grossini.runAction(CCSequence.actions(actionBy, actionByBack));

kathia.runAction(CCSequence.actions(actionBy2, actionBy2.reverse()));

}

public String title() {

return "ScaleTo / ScaleBy";

}

}

static class ActionJump extends ActionDemo {//跳跃

public void onEnter() {

super.onEnter();

CCJumpTo actionTo = CCJumpTo.action(2, CGPoint.ccp(300,300), 50, 4);//时间,跳到哪一点,高度,次数

CCJumpBy actionBy = CCJumpBy.action(2, CGPoint.ccp(300,0), 50, 4);

CCJumpBy actionUp = CCJumpBy.action(2, CGPoint.ccp(0,0), 8, 4);

CCJumpBy actionByBack = actionBy.reverse();

tamara.runAction(actionTo);

grossini.runAction(CCSequence.actions(actionBy, actionByBack));

kathia.runAction(CCRepeatForever.action(actionUp));

}

public String title() {

return "JumpTo / JumpBy";

}

}

static class ActionBezier extends ActionDemo {//曲线移动,以下省略

public void onEnter() {

super.onEnter();

CGSize s = CCDirector.sharedDirector().winSize();

//

// startPosition can be any coordinate, but since the movement

// is relative to the Bezier curve, make it (0,0)

//

// sprite 1

CCBezierConfig bezier = new CCBezierConfig();

bezier.controlPoint_1 = CGPoint.ccp(0, s.height/2);

bezier.controlPoint_2 = CGPoint.ccp(300, -s.height/2);

bezier.endPosition = CGPoint.ccp(300,100);

CCBezierBy bezierForward = CCBezierBy.action(3, bezier);

CCBezierBy bezierBack = bezierForward.reverse();

CCSequence seq = CCSequence.actions(bezierForward, bezierBack);

CCRepeatForever rep = CCRepeatForever.action(seq);

// sprite 2

tamara.setPosition(CGPoint.ccp(80,160));

CCBezierConfig bezier2 = new CCBezierConfig();

bezier2.controlPoint_1 = CGPoint.ccp(100, s.height/2);

bezier2.controlPoint_2 = CGPoint.ccp(200, -s.height/2);

bezier2.endPosition = CGPoint.ccp(240,160);

CCBezierTo bezierTo1 = CCBezierTo.action(2, bezier2);

// sprite 3

kathia.setPosition(CGPoint.ccp(400,160));

CCBezierTo bezierTo2 = CCBezierTo.action(2, bezier2);

grossini.runAction(rep);

tamara.runAction(bezierTo1);

kathia.runAction(bezierTo2);

}

public String title() {

return "BezierBy / BezierTo";

}

}

static class ActionBlink extends ActionDemo {//闪烁

public void onEnter() {

super.onEnter();

centerSprites(2);

CCBlink action1 = CCBlink.action(2, 10);

CCBlink action2 = CCBlink.action(2, 5);

tamara.runAction(action1);

kathia.runAction(action2);

}

public String title() {

return "Blink";

}

}

static class ActionFade extends ActionDemo {//显影

public void onEnter() {

super.onEnter();

centerSprites(2);

tamara.setOpacity(0);

CCFadeIn action1 = CCFadeIn.action(1.0f);

CCFadeOut action1Back = action1.reverse();

CCFadeOut action2 = CCFadeOut.action(1.0f);

CCFadeIn action2Back = action2.reverse();

tamara.runAction(CCSequence.actions(action1, action1Back));

kathia.runAction(CCSequence.actions(action2, action2Back));

}

public String title() {

return "FadeIn / FadeOut";

}

}

static class ActionTint extends ActionDemo {//颜色渐变

public void onEnter() {

super.onEnter();

centerSprites(2);

CCTintTo action1 = CCTintTo.action(2, ccColor3B.ccc3(255, 0, 255));

CCTintBy action2 = CCTintBy.action(2, ccColor3B.ccc3(-127, -255, -127));

CCTintBy action2Back = action2.reverse();

tamara.runAction(action1);

kathia.runAction(CCSequence.actions(action2, action2Back));

}

public String title() {

return "TintTo / TintBy";

}

}

static class ActionAnimate extends ActionDemo {//贞贞动画

public void onEnter() {

super.onEnter();

centerSprites(1);

CCAnimation animation = CCAnimation.animation("dance");

for( int i=1;i<15;i++)

animation.addFrame(String.format("grossini_dance_%02d.png", i));

CCAnimate action = CCAnimate.action(3, animation, false);

CCAnimate action_back = action.reverse();

grossini.runAction(CCSequence.actions(action, action_back));

}

public String title() {

return "Animation";

}

}

static class ActionSequence extends ActionDemo {//连续动作

public void onEnter() {

super.onEnter();

alignSpritesLeft(1);

CCSequence action = CCSequence.actions(

CCMoveBy.action(2, CGPoint.ccp(240,0)),

CCRotateBy.action(2, 540)

);

grossini.runAction(action);

}

public String title() {

return "Sequence: Move + Rotate";

}

}

static class ActionSequence2 extends ActionDemo {

public void onEnter() {

super.onEnter();

alignSpritesLeft(1);

grossini.setVisible(false);

CCSequence action = CCSequence.actions(

CCPlace.action(CGPoint.ccp(200,200)),

CCShow.action(),

CCMoveBy.action(1, CGPoint.ccp(100,0)),

CCCallFunc.action(this, "callback1"),

CCCallFuncN.action(this, "callback2"),

CCCallFuncND.action(this, "callback3", Float.valueOf(1.0f))

);

grossini.runAction(action);

}

public void callback1() {

CGSize s = CCDirector.sharedDirector().winSize();

CCLabel label = CCLabel.makeLabel("callback 1 called", "DroidSans", 16);

label.setPosition(CGPoint.ccp( s.width/4*1,s.height/2));

addChild(label);

}

public void callback2(Object sender) {

CGSize s = CCDirector.sharedDirector().winSize();

CCLabel label = CCLabel.makeLabel("callback 2 called", "DroidSans", 16);

label.setPosition(CGPoint.ccp( s.width/4*2,s.height/2));

addChild(label);

}

public void callback3(Object sender, Object data) {

CGSize s = CCDirector.sharedDirector().winSize();

CCLabel label = CCLabel.makeLabel("callback 3 called", "DroidSans", 16);

label.setPosition(CGPoint.ccp( s.width/4*3,s.height/2));

addChild(label);

}

public String title() {

return "Sequence of InstantActions";

}

}

static class ActionSpawn extends ActionDemo {//同步动画

public void onEnter() {

super.onEnter();

alignSpritesLeft(1);

CCAction action = CCSpawn.actions(

CCJumpBy.action(2, CGPoint.ccp(300,0), 50, 4),

CCRotateBy.action(2, 720));

grossini.runAction(action);

}

public String title() {

return "Spawn: Jump + Rotate";

}

}

static class ActionRepeatForever extends ActionDemo {//永久重复动画

public void onEnter() {

super.onEnter();

centerSprites(1);

CCSequence action = CCSequence.actions(

CCDelayTime.action(1),

CCCallFuncN.action(this, "repeatForever")

);

grossini.runAction(action);

}

public void repeatForever(Object sender) {

CCRepeatForever repeat = CCRepeatForever.action(CCRotateBy.action(1.0f, 360));

((CCNode)sender).runAction(repeat);

}

public String title() {

return "CallFuncN + RepeatForever";

}

}

static class ActionRotateToRepeat extends ActionDemo {

public void onEnter() {

super.onEnter();

centerSprites(2);

CCRotateTo act1 = CCRotateTo.action(1, 90);

CCRotateTo act2 = CCRotateTo.action(1, 0);

CCSequence seq = CCSequence.actions(act1, act2);

CCRepeatForever rep1 = CCRepeatForever.action(seq);

CCRepeat rep2 = CCRepeat.action(seq.copy(), 10);

tamara.runAction(rep1);

kathia.runAction(rep2);

}

public String title() {

return "Repeat/RepeatForever + RotateTo";

}

public String subtitle() {

return "You should see smooth movements (no jerks). issue #390";

}

}

static class ActionRotateJerk extends ActionDemo {

public void onEnter() {

super.onEnter();

centerSprites(2);

CCSequence seq = CCSequence.actions(

CCRotateTo.action(0.5f, -20),

CCRotateTo.action(0.5f, 20)

);

CCRepeat rep1 = CCRepeat.action(seq, 10);

CCRepeatForever rep2 = CCRepeatForever.action(seq.copy());

tamara.runAction(rep1);

kathia.runAction(rep2);

}

public String title() {

return "RepeatForever / Repeat + Rotate";

}

public String subtitle() {

return "You should see smooth movements (no jerks). issue #390";

}

}

static class ActionReverse extends ActionDemo {

public void onEnter() {

super.onEnter();

alignSpritesLeft(1);

CCJumpBy jump = CCJumpBy.action(2, CGPoint.ccp(300,0), 50, 4);

CCSequence action = CCSequence.actions(jump, jump.reverse());

grossini.runAction(action);

}

public String title() {

return "Reverse an action";

}

}

static class ActionDelayTime extends ActionDemo {

public void onEnter() {

super.onEnter();

alignSpritesLeft(1);

CCMoveBy move = CCMoveBy.action(1, CGPoint.ccp(150,0));

CCSequence action = CCSequence.actions(move, CCDelayTime.action(2), move);

grossini.runAction(action);

}

public String title() {

return "DelayTime: m + delay + m";

}

}

static class ActionReverseSequence extends ActionDemo {

public void onEnter() {

super.onEnter();

alignSpritesLeft(1);

CCMoveBy move1 = CCMoveBy.action(1, CGPoint.ccp(250,0));

CCMoveBy move2 = CCMoveBy.action(1, CGPoint.ccp(0,50));

CCSequence seq = CCSequence.actions(move1, move2, move1.reverse());

CCSequence action = CCSequence.actions(seq, seq.reverse());

grossini.runAction(action);

}

public String title() {

return "Reverse a sequence";

}

}

static class ActionReverseSequence2 extends ActionDemo {

public void onEnter() {

super.onEnter();

alignSpritesLeft(2);

// Test:

//   Sequence should work both with IntervalAction and InstantActions

CCMoveBy move1 = CCMoveBy.action(1, CGPoint.ccp(250,0));

CCMoveBy move2 = CCMoveBy.action(1, CGPoint.ccp(0,50));

CCToggleVisibility tog1 = CCToggleVisibility.action();

CCToggleVisibility tog2 = CCToggleVisibility.action();

CCSequence seq = CCSequence.actions(move1, tog1, move2, tog2, move1.reverse());

CCRepeat action = CCRepeat.action(

CCSequence.actions(seq, seq.reverse()), 3);

// Test:

//   Also test that the reverse of Hide is Show, and vice-versa

kathia.runAction(action);

CCMoveBy move_tamara = CCMoveBy.action(1, CGPoint.ccp(100,0));

CCMoveBy move_tamara2 = CCMoveBy.action(1, CGPoint.ccp(50,0));

CCHide hide = CCHide.action();

CCSequence seq_tamara = CCSequence.actions(move_tamara, hide, move_tamara2);

CCSequence seq_back = seq_tamara.reverse();

tamara.runAction(CCSequence.actions(seq_tamara, seq_back));

}

public String title() {

return "Reverse sequence 2";

}

}

static class ActionRepeat extends ActionDemo {

public void onEnter() {

super.onEnter();

alignSpritesLeft(2);

CCMoveBy a1 = CCMoveBy.action(1, CGPoint.ccp(150,0));

CCRepeat action1 = CCRepeat.action(

CCSequence.actions(CCPlace.action(CGPoint.ccp(60,60)), a1), 3);

CCRepeatForever action2 = CCRepeatForever.action(

CCSequence.actions( a1.copy(), a1.reverse()));

kathia.runAction(action1);

tamara.runAction(action2);

}

public String title() {

return "Repeat / RepeatForever actions";

}

}

static class ActionCallFunc extends ActionDemo {

public void onEnter() {

super.onEnter();

centerSprites(3);

CCSequence action = CCSequence.actions(

CCMoveBy.action(2, CGPoint.ccp(200,0)),

CCCallFunc.action(this, "callback1")

);

CCSequence action2 = CCSequence.actions(

CCScaleBy.action(2, 2),

CCFadeOut.action(2),

CCCallFuncN.action(this, "callback2")

);

CCSequence action3 = CCSequence.actions(

CCRotateBy.action(3, 360),

CCFadeOut.action(2),

CCCallFuncND.action(this, "callback3", Float.valueOf(1.0f))

);

grossini.runAction(action);

tamara.runAction(action2);

kathia.runAction(action3);

}

public void callback1() {

// ccMacros.CCLOG(LOG_TAG,"callback 1 called");

CGSize s = CCDirector.sharedDirector().winSize();

CCLabel label = CCLabel.makeLabel("callback 1 called", "DroidSans", 16);

label.setPosition(CGPoint.ccp( s.width/4*1,s.height/2));

addChild(label);

}

public void callback2(Object sender) {

// ccMacros.CCLOG(LOG_TAG, "callback 2 called from:" + String.valueOf(sender));

CGSize s = CCDirector.sharedDirector(). winSize();

CCLabel label = CCLabel.makeLabel("callback 2 called", "DroidSans", 16);

label.setPosition(CGPoint.ccp( s.width/4*2,s.height/2));

addChild(label);

}

public void callback3(Object sender, Object data) {

// NSLog(@"callback 3 called from:%@ with data:%x",sender,(int)data);

CGSize s = CCDirector.sharedDirector().winSize();

CCLabel label = CCLabel.makeLabel("callback 3 called", "DroidSans", 16);

label.setPosition(CGPoint.ccp( s.width/4*3,s.height/2));

addChild(label);

}

public String title() {

return "Callbacks: CallFunc and friends";

}

}

static class ActionCallFuncND extends ActionDemo {

public void onEnter() {

super.onEnter();

centerSprites(1);

CCSequence action = CCSequence.actions(

CCMoveBy.action(2, CGPoint.ccp(200,0)),

CCCallFuncND.action(this, "callbackND", "a different implemetation of CCCallFuncND from iphone version")

);

grossini.runAction(action);

}

public void callbackND(Object sender, Object data) {

// the action runner, grossini, is the sender.

CCSprite obj = (CCSprite)sender;

String msg   = (String)data;

obj.removeFromParentAndCleanup(true);

ccMacros.CCLOGINFO("callbackND", msg);

}

public String title() {

return "CallFuncND + auto remove";

}

public String subtitle() {

return "CallFuncND + remove sprite. Grossini dissapears in 2s";

}

}

static class ActionOrbit extends ActionDemo {

public void onEnter() {

super.onEnter();

centerSprites(3);

CCOrbitCamera orbit1 = CCOrbitCamera.action(2, 1, 0, 0, 180, 0, 0);

CCSequence action1 = CCSequence.actions(

orbit1,

orbit1.reverse()

);

CCOrbitCamera orbit2 = CCOrbitCamera.action(2, 1, 0, 0, 180, -45, 0);

CCSequence action2 = CCSequence.actions(

orbit2,

orbit2.reverse()

);

CCOrbitCamera orbit3 = CCOrbitCamera.action(2, 1, 0, 0, 180, 90, 0);

CCSequence action3 = CCSequence.actions(

orbit3,

orbit3.reverse()

);

kathia.runAction(CCRepeatForever.action(action1));

tamara.runAction(CCRepeatForever.action(action2));

grossini.runAction(CCRepeatForever.action(action3));

CCMoveBy move = CCMoveBy.action(3, CGPoint.ccp(100,-100));

CCMoveBy move_back = move.reverse();

CCSequence seq = CCSequence.actions(move, move_back);

CCRepeatForever rfe = CCRepeatForever.action(seq);

kathia.runAction(rfe);

tamara.runAction(rfe.copy());

grossini.runAction(rfe.copy());

}

public String title() {

return "OrbitCamera action";

}

}

static class ActionFollow extends ActionDemo {

public void onEnter() {

super.onEnter();

centerSprites(1);

CGSize winSize = CCDirector.sharedDirector().winSize();

grossini.setPosition(CGPoint.ccp(-200, winSize.height/2));

CCMoveBy move = CCMoveBy.action(2, CGPoint.ccp(winSize.width*3,0));

CCMoveBy move_back = move.reverse();

CCSequence seq = CCSequence.actions(move, move_back);

CCRepeatForever rep = CCRepeatForever.action(seq);

grossini.runAction(rep);

this.runAction(

CCFollow.action(grossini, CGRect.make(0, 0, (winSize.width*2)-100, winSize.height))

);

}

public void draw(GL10 gl) {

CGSize winSize = CCDirector.sharedDirector().winSize();

float x = winSize.width*2 - 100;

float y = winSize.height;

CGPoint vertices[] = {

CGPoint.ccp(5,5), CGPoint.ccp(x-5,5),

CGPoint.ccp(x-5,y-5), CGPoint.ccp(5,y-5)

};

CCDrawingPrimitives.ccDrawPoly(gl, vertices, 4, true);

}

public String title() {

return "Follow action";

}

public String subtitle() {

return "The sprite should be centered, even though it is being moved";

}

}

static class ActionProperty extends ActionDemo {

public void onEnter() {

super.onEnter();

centerSprites(3);

CCPropertyAction rot = CCPropertyAction.action(2, "setRotation", 0, -270);

CCPropertyAction rot_back = rot.reverse();

CCSequence rot_seq = CCSequence.actions(rot, rot_back);

CCPropertyAction scale = CCPropertyAction.action(2, "setScale", 1, 3);

CCPropertyAction scale_back = scale.reverse();

CCSequence scale_seq = CCSequence.actions(scale, scale_back);

CCPropertyAction opacity = CCPropertyAction.action(2, "setOpacity", 255, 0);

CCPropertyAction opacity_back = opacity.reverse();

CCSequence opacity_seq = CCSequence.actions(opacity, opacity_back);

grossini.runAction(rot_seq);

tamara.runAction(scale_seq);

kathia.runAction(opacity_seq);

}

public String title() {

return "PropertyAction";

}

public String subtitle() {

return "Simulates Rotation, Scale and Opacity using a generic action";

}

}

}

时间: 2024-10-23 12:08:35

Cocos2D-Android-1之源码详解:3.ActionsTest的相关文章

android自定义SlideMenu源码详解之最简单侧滑实现

实现原理:在一个Activity的布局中需要有两部分,一个是菜单(menu)的布局,一个是内容(content)的布局.两个布局横向排列,菜单布局在左,内容布局在右.初始化的时候将菜单布局向左偏移,以至于能够完全隐藏,这样内容布局就会完全显示在Activity中.然后通过监听手指滑动事件,来改变菜单布局的左偏移距离,从而控制菜单布局的显示和隐藏. 下来来实现这个效果: 1.打开layout下的activity_main.xml <LinearLayout xmlns:android="ht

Android编程之Fragment动画加载方法源码详解

上次谈到了Fragment动画加载的异常问题,今天再聊聊它的动画加载loadAnimation的实现源代码: Animation loadAnimation(Fragment fragment, int transit, boolean enter, int transitionStyle) { 接下来具体看一下里面的源码部分,我将一部分一部分的讲解,首先是: Animation animObj = fragment.onCreateAnimation(transit, enter, fragm

Android View 事件分发机制源码详解(View篇)

前言 在Android View 事件分发机制源码详解(ViewGroup篇)一文中,主要对ViewGroup#dispatchTouchEvent的源码做了相应的解析,其中说到在ViewGroup把事件传递给子View的时候,会调用子View的dispatchTouchEvent,这时分两种情况,如果子View也是一个ViewGroup那么再执行同样的流程继续把事件分发下去,即调用ViewGroup#dispatchTouchEvent:如果子View只是单纯的一个View,那么调用的是Vie

Android ArrayMap源码详解

尊重原创,转载请标明出处    http://blog.csdn.net/abcdef314159 分析源码之前先来介绍一下ArrayMap的存储结构,ArrayMap数据的存储不同于HashMap和SparseArray,在上一篇<Android SparseArray源码详解>中我们讲到SparseArray是以纯数组的形式存储的,一个数组存储的是key值一个数组存储的是value值,今天我们分析的ArrayMap和SparseArray有点类似,他也是以纯数组的形式存储,不过不同的是他的

IntentService源码详解

IntentService可以做什么: 如果你有一个任务,分成n个子任务,需要它们按照顺序完成.如果需要放到一个服务中完成,那么IntentService就会使最好的选择. IntentService是什么: IntentService是一个Service(看起来像废话,但是我第一眼看到这个名字,首先注意的是Intent啊.),所以如果自定义一个IntentService的话,一定要在AndroidManifest.xml里面声明. 从上面的"可以做什么"我们大概可以猜测一下Inten

butterknife源码详解

butterknife源码详解 作为Android开发者,大家肯定都知道大名鼎鼎的butterknife.它大大的提高了开发效率,虽然在很早之前就开始使用它了,但是只知道是通过注解的方式实现的,却一直没有仔细的学习下大牛的代码.最近在学习运行时注解,决定今天来系统的分析下butterknife的实现原理. 如果你之前不了解Annotation,那强烈建议你先看注解使用. 废多看图: 从图中可以很直观的看出它的module结构,以及使用示例代码. 它的目录和我们在注解使用这篇文章中介绍的一样,大体

Java concurrent AQS 源码详解

一.引言 AQS(同步阻塞队列)是concurrent包下锁机制实现的基础,相信大家在读完本篇博客后会对AQS框架有一个较为清晰的认识 这篇博客主要针对AbstractQueuedSynchronizer的源码进行分析,大致分为三个部分: 静态内部类Node的解析 重要常量以及字段的解析 重要方法的源码详解. 所有的分析仅基于个人的理解,若有不正之处,请谅解和批评指正,不胜感激!!! 二.Node解析 AQS在内部维护了一个同步阻塞队列,下面简称sync queue,该队列的元素即静态内部类No

深入Java基础(四)--哈希表(1)HashMap应用及源码详解

继续深入Java基础系列.今天是研究下哈希表,毕竟我们很多应用层的查找存储框架都是哈希作为它的根数据结构进行封装的嘛. 本系列: (1)深入Java基础(一)--基本数据类型及其包装类 (2)深入Java基础(二)--字符串家族 (3)深入Java基础(三)–集合(1)集合父类以及父接口源码及理解 (4)深入Java基础(三)–集合(2)ArrayList和其继承树源码解析以及其注意事项 文章结构:(1)哈希概述及HashMap应用:(2)HashMap源码分析:(3)再次总结关键点 一.哈希概

Spring IOC源码详解之容器依赖注入

Spring IOC源码详解之容器依赖注入 上一篇博客中介绍了IOC容器的初始化,通过源码分析大致了解了IOC容器初始化的一些知识,先简单回顾下上篇的内容 载入bean定义文件的过程,这个过程是通过BeanDefinitionReader来完成的,其中通过 loadBeanDefinition()来对定义文件进行解析和根据Spring定义的bean规则进行处理 - 事实上和Spring定义的bean规则相关的处理是在BeanDefinitionParserDelegate中完成的,完成这个处理需

Spring IOC源码详解之容器初始化

Spring IOC源码详解之容器初始化 上篇介绍了Spring IOC的大致体系类图,先来看一段简短的代码,使用IOC比较典型的代码 ClassPathResource res = new ClassPathResource("beans.xml"); DefaultListableBeanFactory factory = new DefaultListableBeanFactory(); XmlBeanDefinitionReader reader = new XmlBeanDe