Cocos2d—android 中常用的工具类
生活随笔
收集整理的這篇文章主要介紹了
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 中常用的工具类的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在大学服兵役可以留在部队吗
- 下一篇: 武警厦门市支队有几个中队