加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d—android 中常用的工具类

发布时间:2020-12-14 16:26:45 所属栏目:百科 来源:网络整理
导读:span style="font-size:18px;" 在开发游戏过程中通常会用到一个经常编写的重复的代码,比如加载游戏地图,从地图中加载指定点的集合,序列帧的播放等等,下面的这个类就可以完全实现,而不需要重复的编写。/span /** * 通用工具 * * @author Administrator *
<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,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("",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);
	}

}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读