cocos2d-android——Action(4)

来源:未知 责任编辑:责任编辑 发表时间:2014-02-02 17:44 点击:

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);
		
		
	}
}

	
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • cocos2d-x学习笔记(19)--label 、label atlas
  • cocos2d-x学习笔记(23)--地图的使用3--CCTMXLayer
  • Cocos2d-x学习(一):HelloWorld
  • cocos2dx在xcode下开发,编译到android上(2)
  • cocos2d 设置屏幕默认方向
  • cocos2d-x学习笔记(22)--地图的使用2(TMX) --Z-Order、AnchorPoi
  • Cocos2d-x 2.0 之 Actions “三板斧” 之一
  • cocos2d-x学习笔记(18)--游戏打包(windows平台)
  • cocos2d-x学习笔记(16)--spritesheet(精灵表单)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1