cocos2dx-master之Aciton(一)

前端之家收集整理的这篇文章主要介绍了cocos2dx-master之Aciton(一)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
刚看是学习cocos2dx,借用宝地做些笔记。<img alt="大笑" src="http://static.blog.csdn.net/xheditor/xheditor_emot/default/laugh.gif" />


直接上代码各种Action的练习

MainAcitivity.java


package org.game.secondgame;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;

import android.os.Bundle;
import android.app.Activity;
import android.text.Layout;
import android.view.Menu;

public class MainActivity extends Activity {
	private CCGLSurfaceView view=null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view=new CCGLSurfaceView(this);
        setContentView(view);
        //获取一个导演
        CCDirector director=CCDirector.sharedDirector();
        director.attachInView(view);
        director.setDisplayFPS(true);
        director.setAnimationInterval(1/30.0);
        CCScene scene=CCScene.node();
        GameLayout gameLayout=new GameLayout();
        scene.addChild(gameLayout);
        director.runWithScene(scene);
        
        
       
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main,menu);
        return true;
    }
    
}

GameLayout.java


package org.game.secondgame;

import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCCallFuncN;
import org.cocos2d.actions.interval.CCFadeOut;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCRepeat;
import org.cocos2d.actions.interval.CCRotateTo;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.actions.interval.CCSpawn;
import org.cocos2d.actions.interval.CCTintTo;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.ccColor3B;

public class GameLayout extends CCLayer {
	
	CCSprite sprite;
	public GameLayout(){
		sprite=CCSprite.sprite("player.png");
		this.addChild(sprite);
		CGPoint init=CGPoint.ccp(100,100);
		sprite.setPosition(init);
		
		CGPoint target=CGPoint.ccp(300,300);
		CCMoveTo moveTo1=CCMoveTo.action(1,target);
		CCMoveTo moveTo2=CCMoveTo.action(1,init);
		CCRotateTo rotateTo=CCRotateTo.action(4,180);
		//顺序执行,先moveTo后rotateTo(可加多个动作)
//		CCSequence sequence=CCSequence.actions(moveTo,rotateTo);
		
		//同时执行moveTo,rotateTo(可加多个动作)
//		CCSpawn spawn=CCSpawn.actions(moveTo,rotateTo);
		
		//第一个参数:本地对象;第二个参数:所要调用函数名
//		CCCallFuncN funcn=CCCallFuncN.action(this,"onActionFinish");
//		CCSequence sequence=CCSequence.actions(moveTo,funcn);
		
		//逐渐淡出
//		CCFadeOut fadeOut=CCFadeOut.action(5);
		
		//RGB决定颜色
//		ccColor3B color3B=ccColor3B.ccc3(255,0);
		//将精灵颜色转变为color3B颜色
//		CCTintTo tinTo=CCTintTo.action(5,color3B);
		
		CCSequence sequence=CCSequence.actions(moveTo1,moveTo2);
		//执行sequence行为3次
//		CCRepeat repeat=CCRepeat.action(sequence,3);
		
		CCRepeatForever repeatForever=CCRepeatForever.action(sequence);
		sprite.runAction(repeatForever);
	}
	
	public void onActionFinish(Object sender){
		System.out.println("onActionFinish");
	}
}





原文链接:https://www.f2er.com/cocos2dx/346314.html

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