android cocos2d demo

僵尸行走的demo ..比较简单,啥也不想说。但还是想记录下来

mainActivity

package com.zkk;

import org.cocos2d.layers.CCScene;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.opengl.CCGLSurfaceView;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {
	private CCDirector director;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.getWindow().setFlags(
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		CCGLSurfaceView surfaceView=new CCGLSurfaceView(this);
		setContentView(surfaceView);
		WindowManager wm = this.getWindowManager();
		int width = wm.getDefaultDisplay().getWidth();  
	    int height = wm.getDefaultDisplay().getHeight();
	  //  Log.i("zhouke", ":"+width+":"+height);
		director=CCDirector.sharedDirector();
		director.setDeviceOrientation(CCDirector.kCCDeviceOrientationLandscapeLeft);
		director.setScreenSize(720,1280);
		director.setDisplayFPS(true);
		director.attachInView(surfaceView);
		CCScene scene=CCScene.node();
		DemoLayer layer=new DemoLayer();
		scene.addChild(layer);
		director.runWithScene(scene);
	}
	@Override
	protected void onResume() {
		director.onResume();
		super.onResume();
	}
	@Override
	protected void onPause() {
		director.onPause();
		super.onPause();
	}
	
	@Override
	protected void onDestroy() {
		director.end();
		super.onDestroy();
	}

}
demoLayer

package com.zkk;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.cocos2d.actions.base.CCFollow;
import org.cocos2d.actions.base.CCRepeatForever;
import org.cocos2d.actions.instant.CCCallFunc;
import org.cocos2d.actions.instant.CCPlace;
import org.cocos2d.actions.interval.CCAnimate;
import org.cocos2d.actions.interval.CCMoveTo;
import org.cocos2d.actions.interval.CCSequence;
import org.cocos2d.layers.CCLayer;
import org.cocos2d.layers.CCTMXObjectGroup;
import org.cocos2d.layers.CCTMXTiledMap;
import org.cocos2d.nodes.CCAnimation;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.nodes.CCSpriteFrame;
import org.cocos2d.nodes.CCTextureCache;
import org.cocos2d.particlesystem.CCParticleSnow;
import org.cocos2d.particlesystem.CCParticleSystem;
import org.cocos2d.sound.SoundEngine;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;

import com.example.jsapp.R;

import android.util.Log;
import android.view.MotionEvent;

public class DemoLayer extends CCLayer {
	private CCTMXTiledMap gameMap;
	private List roadPos;
	private CCParticleSystem particleSystem;

	public DemoLayer() {
		init();
	}

	private void init() {
		// 地图的处理 生成xml
		setIsTouchEnabled(true);
		// 加载地图
		loadMap();
		// 解析地图内容 (按照顺序)
		parseMap();
		// 僵尸的移动 平移 绝对的移动
		move();

		// 序列帧的播放

		// moveMap();

		ArrayList frames = new ArrayList();
		String filepath = "z_1_%02d.png";// 如果有20张图片
		for (int i = 1; i <= 7; i++) {
			CCSpriteFrame frame = CCSprite.sprite(String.format(filepath, i))
					.displayedFrame();
			frames.add(frame);
		}
		CCAnimation anim = CCAnimation.animation("", 0.2f, frames);
		CCAnimate animate = CCAnimate.action(anim);

		sprint.runAction(CCRepeatForever.action(animate));
		// 声音的处理
		// 飘雪场景
		// 粒子系统模拟飘雪

		particalSystem();
	}

	private void particalSystem() { 
		particleSystem = CCParticleSnow.node();
		particleSystem.setTexture(CCTextureCache.sharedTextureCache().addImage(
				"f.png"));
		this.addChild(particleSystem);
	}

	private void moveMap() {
		// 地图是否是大图
		CCFollow follow = CCFollow.action(sprint);
		this.runAction(follow);

	}

	private void move() {
		sprint = CCSprite.sprite("z_1_01.png");
		// sprint.setAnchorPoint(0, 0);
		sprint.setPosition(roadPos.get(0));
		// 缩放控制
		sprint.setScale(0.5);
		sprint.setFlipX(true);

		// this.addChild(sprint);
		gameMap.addChild(sprint);
		// for 方案一 循环
		/*
		 * for(int index=1;index();
		// 获取系统解析的信息
		CCTMXObjectGroup road = gameMap.objectGroupNamed("road");
		ArrayList> objects = road.objects;
		for (HashMap item : objects) {
			int x = Integer.parseInt(item.get("x"));
			int y = Integer.parseInt(item.get("y"));
			roadPos.add(CGPoint.ccp(x,y));
		}
	}

	@Override
	public boolean ccTouchesMoved(MotionEvent event) {
		gameMap.touchMove(event, gameMap);
		return super.ccTouchesMoved(event);
	}
	@Override
	public boolean ccTouchesBegan(MotionEvent event) {
		//任意点击屏幕暂停游戏
		PauseLayer pauseLayer=new PauseLayer();
		//关于暂停layer 的添加。添加到DemoLayer 的parent 里面
		//一旦Demolayer 处于冻结状态的时候, 他下面所有的东西都不能被点击了
		this.getParent().addChild(pauseLayer);
		this.onExit();//游戏暂停
		return super.ccTouchesBegan(event);
	}
	/**
	 * 暂停的layer
	 * @author Administrator
	 *
	 */
	private class PauseLayer extends CCLayer{
		private CCSprite sprite_;
		public PauseLayer(){
			this.setIsTouchEnabled(true);
			sprite_ = CCSprite.sprite("heart.png");
			CGSize winSize = CCDirector.sharedDirector().getWinSize();
			sprite_.setPosition(winSize.width/2,winSize.height/2);
			this.addChild(sprite_);
		}
		@Override
		public boolean ccTouchesBegan(MotionEvent event) {
			CGPoint touchPos = this.convertTouchToNodeSpace(event);
			if(CGRect.containsPoint(sprite_.getBoundingBox(), touchPos)){
				//游戏继续
				DemoLayer.this.onEnter();//为DemoLayer 添加可以点击操作功能
				//this.setVisible(false);//无法解决问题
				this.removeSelf();
			}
			return super.ccTouchesBegan(event);
		}
	}
}








你可能感兴趣的:(android cocos2d demo)