cocos2d-android——Action(4)

1、MainActivity

package com.njupt.ccaction04;

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.view.Menu;

public class MainActivity extends Activity {

	//cocos2d引擎会把图形会知道在该view对象上
	private CCGLSurfaceView view = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		view = new CCGLSurfaceView(this);
		setContentView(view);
		
		
		CCDirector director = CCDirector.sharedDirector();//得到CCDirector对象
		/**
		 * 设置游戏程序的相关属性
		 */
		director.attachInView(view);//设置当前游戏程序中所使用的view对象
		director.setDisplayFPS(true);//设置游戏程序是否显示FPS值
		director.setAnimationInterval(1.0f/60);//设置游戏渲染一帧所需要的时间
		
		//生成一个游戏场景对象
		CCScene scene = CCScene.node();
	
		//生成布景层对象
		GameLayer gameLayer = new GameLayer();
		
		//将布景层对象添加至游戏场景中
		scene.addChild(gameLayer);
		
		//运行游戏场景
		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;
	}

}



2、GameLayer

package com.njupt.ccaction04;

import org.cocos2d.actions.base.CCRepeatForever;
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.CCRepeat;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.actions.interval.CCTintBy;
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 GameLayer extends CCLayer{
	private CCSprite sprite;
	
	public GameLayer() {
		
		sprite = CCSprite.sprite("player.png");
		sprite.setPosition(300,300);
		this.addChild(sprite);
		
		//CCFadeIn:淡入效果
//		CCFadeIn fadeIn = CCFadeIn.action(3);
//		sprite.runAction(fadeIn);
		
		//CCFadeOut:淡出效果
//		CCFadeOut fadeOut = CCFadeOut.action(3);
//		sprite.runAction(fadeOut);
		
		/**
		 * CCTintTo和CCTintBy的作用都是改变颜色。前者进行的是=运算,后者进行的是delta运算
		 */
//		ccColor3B color3b = ccColor3B.ccc3(255, 0, 0);
//		CCTintTo tintTo = CCTintTo.action(3, color3b);
//		sprite.runAction(tintTo);
		
//		ccColor3B color3b = ccColor3B.ccc3(-255, 120, 120);
//		CCTintBy tintBy = CCTintBy.action(3, color3b);
//		sprite.runAction(tintBy);
		
		/**
		 * CCRepeat repeat = CCRepeat.action(seq, 3)
		 * 第一个参数是需要循环的动作
		 * 第二个参数是需要循环的次数
		 * 
		 * CCRepeatForever.action(seq);
		 * 一直循环指定动作
		 */
		CGPoint initPoint = CGPoint.ccp(300, 300);
		CGPoint targetPoint = CGPoint.ccp(100, 100);
		CCMoveTo moveTo1 = CCMoveTo.action(3, targetPoint);
		CCMoveTo moveTo2 = CCMoveTo.action(3, initPoint);
		CCSequence seq = CCSequence.actions(moveTo1, moveTo2);
//		CCRepeat repeat = CCRepeat.action(seq, 3);
		CCRepeatForever forever = CCRepeatForever.action(seq);
		sprite.runAction(forever);
		
		
	}
}




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。