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

cocos2d-android的基础代码

2014-01-23 15:18 190 查看
1、新建项目

2、将下载好的cocos-master中的cocos2d-android中的libs目录中的所有东西拷到libs目录下,并将cocos2d-android.jar添加到buildpath

3、开始编写代码

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

}


2)GameLayer

package com.njupt.firstgame;

import org.cocos2d.layers.CCLayer;

public class GameLayer extends CCLayer{

public GameLayer() {

}
}


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

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