欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

Cocos2d—android 中常用的工具类

发布时间:2024/10/8 编程问答 34 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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);}}

总结

以上是生活随笔为你收集整理的Cocos2d—android 中常用的工具类的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。