Cocos2d—android 中常用的工具类

<span style="font-size:18px;">     在开发游戏过程中通常会用到一个经常编写的重复的代码,比如加载游戏地图,从地图中加载指定点的集合,序列帧的播放等等,下面的这个类就可以完全实现,而不需要重复的编写。</span>
/**
 * 通用工具
 * 
 * @author Administrator
 * 
 */
public class CommonUtil {

	/**
	 * 加载游戏地图
	 * @param tmxFile
	 * @return
	 */
	public static CCTMXTiledMap loadMap(String tmxFile)
	{
		CCTMXTiledMap gameMap = CCTMXTiledMap.tiledMap(tmxFile);
		
		//便于手动平移地图
		gameMap.setAnchorPoint(0.5f, 0.5f);
		CGSize contentSize = gameMap.getContentSize();
		gameMap.setPosition(contentSize.width / 2, contentSize.height / 2);
		
		return gameMap;
	}
	/**
	 * 从地图中加载指定名称的点集合
	 * @param map
	 * @param name
	 * @return
	 */
	public static List<CGPoint> loadPoint(CCTMXTiledMap map,String name)
	{
		CCTMXObjectGroup objectGroup = map.objectGroupNamed(name);
		// 获取僵尸位置信息
		ArrayList<HashMap<String, String>> objects = objectGroup.objects;
		// 分别以x和y为键,获取坐标值信息---->封装到点集合中
		List<CGPoint> points = new ArrayList<CGPoint>();
		for (HashMap<String, String> item : objects) {
			float x = Float.parseFloat(item.get("x"));
			float y = Float.parseFloat(item.get("y"));
			points.add(CGPoint.ccp(x, y));
		}
		return points;
	}
	
	
	
	/**
	 * 序列帧播放(永不停止)
	 * 
	 * @param frames
	 * @param num
	 *            当前加载的图片数量
	 * @param filepath
	 *            路径(通用)
	 * @return
	 */
	public static CCAction getRepeatForeverAnimate(ArrayList<CCSpriteFrame> frames, int num, String filepath) {
		if (frames == null) {
			frames = new ArrayList<CCSpriteFrame>();
			for (int i = 1; i <= num; i++) {
				frames.add(CCSprite.sprite(String.format(filepath, i)).displayedFrame());
			}
		}
		CCAnimation anim = CCAnimation.animation("", 0.2f, frames);

		CCAnimate animate = CCAnimate.action(anim);
		return CCRepeatForever.action(animate);
	}

	/**
	 * 播放一次的序列帧
	 */
	public static CCAnimate getAnimate(ArrayList<CCSpriteFrame> frames, int num, String filepath) {
		if (frames == null) {
			frames = new ArrayList<CCSpriteFrame>();
			// frames信息加载
			for (int i = 1; i <= num; i++) {
				frames.add(CCSprite.sprite(String.format(filepath, i)).displayedFrame());
			}
		}
		CCAnimation animation = CCAnimation.animation("", 0.2f, frames);
		return CCAnimate.action(animation, false);// 只播放一次
	}

	/**
	 * 判断是否被点击
	 * 
	 * @param event
	 * @param node
	 * @return
	 */
	public static boolean isClicke(MotionEvent event, CCLayer layer, CCNode node) {
		CGPoint point = layer.convertTouchToNodeSpace(event);
		return CGRect.containsPoint(node.getBoundingBox(), point);
	}

	//
	// /**
	// * 判断是否被点击
	// *
	// * @param touchPoint
	// * @param node
	// * @return
	// */
	// public static boolean isClicke(CGPoint touchPoint, CCNode node) {
	// return CGRect.containsPoint(node.getBoundingBox(), touchPoint);
	// }
	/**
	 * 切换场景
	 * @param targetLayer
	 */
	public static void changeLayer(CCLayer targetLayer)
	{
		CCScene scene = CCScene.node();
		scene.addChild(targetLayer);
		CCFadeTransition transition = CCFadeTransition.transition(1, scene);
		CCDirector.sharedDirector().replaceScene(transition);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值