cocos2d-android基础教程

1、新建一个工程


2、导jar包到libs目录下,并Add to buildpath

   下载链接:http://download.csdn.net/detail/caihongshijie6/6875265

3、到所需要的图片到assets目录下(注意,一定要有那个名为fps。。.png的图片,因为框架底层用到了这个名字的图片)


4、MainActivity

package com.example.cocos2dtest;

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 {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		CCGLSurfaceView view = new CCGLSurfaceView(this);
		setContentView(view);
		
		CCDirector.sharedDirector().attachInView(view);
		
		CCDirector.sharedDirector().setDisplayFPS(true);
		CCDirector.sharedDirector().setAnimationInterval(1.0f/60);
		
		CCDirector.sharedDirector().setDeviceOrientation(CCDirector.kCCDeviceOrientationPortrait);
		CCDirector.sharedDirector().setScreenSize(480, 320);
		
		
		MyCCScene scene = new MyCCScene();
		CCDirector.sharedDirector().runWithScene(scene);
	}

	
	
	@Override
	protected void onDestroy() {
		CCDirector.sharedDirector().end();
		
		super.onDestroy();
	}



	@Override
	protected void onPause() {
		CCDirector.sharedDirector().pause();
		
		super.onPause();
	}



	@Override
	protected void onResume() {
		CCDirector.sharedDirector().resume();
		
		super.onResume();
	}



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

}


5、MyCCScene

package com.example.cocos2dtest;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCSprite;

import android.util.Log;

public class MyCCScene extends CCScene {
	
	public MyCCScene() {
		init();
	}

	private void init() {
		CCLayer layer = CCLayer.node();
		CCSprite sss = CCSprite.sprite("cover.jpg");
		sss.setAnchorPoint(0,0);
		//①被添加的子元素②重要性(重要性高的覆盖重要性低的 ) ③标签(相当于给精灵加了一个ID)
		layer.addChild(sss, 1, 1);
		
		CCSprite sprite = CCSprite.sprite("home_button.png");
		sprite.setAnchorPoint(0f, 0f);
		Log.v("getAnchorPoint========="+sprite.getOpacity(), "getPosition========="+sprite.getPosition());
		sprite.setPosition(100,200);
		layer.addChild(sprite, 2, 2);
		
		CCSprite s = CCSprite.sprite("home_button.png");
		s.setAnchorPoint(0f, 0f);
		s.setPosition(50,50);
//		s.setFlipX(true);//设置x方向镜像
//		s.setFlipY(true);
		
		s.setOpacity(100);//设置透明度
		
		layer.addChild(s, 3, 3);
		
		this.addChild(layer);
	}

//	private void init() {
//		CCLayer layer = CCLayer.node();
//		CCSprite sprite = CCSprite.sprite("home_button.png");
//		
////		sprite.setPosition(0f, 0f);
//		sprite.setAnchorPoint(0f, 0f);
//		layer.addChild(sprite);
//		
//		
//		this.addChild(layer);
//		
//	}
	
	
	
}



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

需要注意的是以下几点:

1、setAnchorPoint()和setPosition()都可以改变精灵(sprite)的位置。

cocos2d-android基础教程


2、关于镜像的说明:

cocos2d-android基础教程



3、这个实例的源代码下载地址:

http://download.csdn.net/detail/caihongshijie6/6875277





cocos2d-android基础教程

上一篇:redis学习-三种特殊数据类型-Hyperloglog


下一篇:非系统盘硬盘的初始化