cocos2d-android——向量
1、MainActivity
package com.njupt.cocos2dvector;
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.cocos2dvector;
import org.cocos2d.actions.interval.CCJumpBy;
import org.cocos2d.actions.interval.CCJumpTo;
import org.cocos2d.actions.interval.CCMoveBy;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
public class GameLayer extends CCLayer{
//声明一个精灵对象
CCSprite aSprite;
CCSprite bSprite;
public GameLayer() {
aSprite = CCSprite.sprite("player.png");
bSprite = CCSprite.sprite("player.png");
this.addChild(aSprite);
this.addChild(bSprite);
CGPoint initPoint = CGPoint.ccp(100, 100);
aSprite.setPosition(initPoint);
bSprite.setPosition(initPoint);
// CGPoint detaPoint = CGPoint.ccp(0, 100);
// CGPoint target = CGPoint.ccpAdd(initPoint, detaPoint);//向量的加法
//
// bSprite.setPosition(target);
/**
* CCMoveBy.action(3, pos)
* CCMoveBy将精灵当前位置的值加上pos的值
*/
// CGPoint pos = CGPoint.ccp(100, 100);
// CCMoveBy MoveBy = CCMoveBy.action(3, pos);
// aSprite.runAction(MoveBy);
/**
* CCJumpBy.action(3, pos, 200, 3);
* 四个参数分别为:动作持续时间,增量,跳跃高度,跳跃次数
*/
CGPoint pos = CGPoint.ccp(300, 400);
CCJumpBy jumpBy = CCJumpBy.action(3, pos, 200, 3);
aSprite.runAction(jumpBy);
}
}
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。