您的位置:首页 > 移动开发 > Cocos引擎

cocos2d-android——给精灵添加动作

2014-01-23 16:44 337 查看
1、将player.jpg放到assets目录下

2、MainActivity

package com.njupt.firstgame;

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

}


3、GameLayer

package com.njupt.firstgame;

import org.cocos2d.actions.interval.CCJumpTo;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;

public class GameLayer extends CCLayer{

//声明一个精灵对象
CCSprite player;

public GameLayer() {
//初始化一个精灵对象,sprite()方法会默认到assets目录下去找名为player.png的文件
player = CCSprite.sprite("player.png");

//设置精灵对象的位置
player.setPosition(100,100);

/**
* 设置精灵的位置还可以通过以下这种方法:
* CGPoint point = CGPoint.ccp(150, 150);
player.setPosition(point);
*/

//将精灵对象添加到布景层中
this.addChild(player);

//CGPoint对象通常用于表示坐标,或者是表示向量
CGPoint target = CGPoint.ccp(400, 100);

/**
* CCJumpTo:一个跳跃的动作
* CCJumpTo.action(3, target, 200, 3)这4个参数分别为动作的持续时间,终点,跳跃的高度,跳跃的次数..
*/
CCJumpTo jumpTo = CCJumpTo.action(3, target, 200, 3);

//使用精灵对象执行这个动作
player.runAction(jumpTo);

}
}


-----------------------

本例子源码的下载链接:http://download.csdn.net/detail/caihongshijie6/6877777
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: