游戏开发入门03
来源:互联网 发布:社交媒体网络安全问题 编辑:程序博客网 时间:2024/06/05 10:51
地图的制作
工具:tiled步骤:1.新建——指定宽度和高度——显示网格2.讲拾取的图片拖到图块图层——拾取(进行填充背景)——拾取填充道路3.讲地图保存——notepad打开——讲引入的图片的绝对路径改成相对路径(把中文改成英文)4.图层——添加对象层(rode名字)5.插入对象(将路按顺序点出来)新建的地图用notepad打开<?xml version="1.0" encoding="UTF-8"?><map version="1.0" orientation="orthogonal" width="14" height="6" tilewidth="46" tileheight="54"> <tileset firstgid="1" name="bk1" tilewidth="46" tileheight="54"> <image source="bk1.jpg" width="678" height="320"/> </tileset> <layer name="block" width="14" height="6"> <data encoding="base64" compression="zlib"> eJwTY2BgECMDW6BhQuK4zCDEprY+YtxMyBx8GADLYgpH </data> </layer> <objectgroup name="road" width="14" height="6"> <object x="22" y="80"/> <object x="160" y="82"/> <object x="161" y="244"/> <object x="299" y="243"/> <object x="298" y="82"/> <object x="436" y="83"/> <object x="436" y="246"/> <object x="620" y="246"/> </objectgroup></map>
加载地图
private void loadMap() { map = CCTMXTiledMap.tiledMap("map.tmx"); map.setAnchorPoint(0.5f,0.5f); // 因为修改了锚点 ,所以坐标也是需要修改的 map.setPosition(map.getContentSize().width/2, map.getContentSize().height/2); this.addChild(map);}
加载地图
// 解析地图private void parserMap() { roadPoits = new ArrayList<CGPoint>(); // 解析地图(对象层里面有road) CCTMXObjectGroup objectGroupNamed = map.objectGroupNamed("road"); ArrayList<HashMap<String, String>> objects = objectGroupNamed.objects; for (HashMap<String, String> hashMap : objects) { int x = Integer.parseInt(hashMap.get("x")); int y = Integer.parseInt(hashMap.get("y")); //拿到我们画的每一个点 CGPoint cgPoint = ccp(x, y); //讲所有的点放到集合里面 roadPoits.add(cgPoint); }}
加载雪花
//加载雪花private void loadParticle() { system = CCParticleSnow.node(); // 设置雪花的样式 system.setTexture(CCTextureCache.sharedTextureCache().addImage("f.png")); this.addChild(system, 1);}
加载僵尸精灵
// 展示僵尸private void loadZombies() { sprite = CCSprite.sprite("z_1_01.png"); sprite.setPosition(roadPoits.get(0)); sprite.setAnchorPoint(0.5f, 0); // 设置锚点在两腿之间 sprite.setScale(0.65f); sprite.setFlipX(true); // 水平翻转 map.addChild(sprite);// 通过地图去添加僵尸 // 地图随着手指移动,僵尸也会随着手指移动 // 序列帧的播放 ArrayList<CCSpriteFrame> frames = new ArrayList<CCSpriteFrame>(); String format = "z_1_%02d.png";// 02d 占位符 可以表示两位的整数 如果不足两位前面用0补足 for (int i = 1; i <= 7; i++) { CCSpriteFrame displayedFrame = CCSprite.sprite( String.format(format, i)).displayedFrame(); frames.add(displayedFrame); } // 配置序列帧的信息 参数1 动作的名字(给程序员看的) 参数2 每一帧播放的时间 单位秒 参数3 所有用到的帧 CCAnimation anim = CCAnimation.animation("走路", 0.2f, frames); CCAnimate animate = CCAnimate.action(anim); // 序列帧动作默认是永不停止的循环 CCRepeatForever forever = CCRepeatForever.action(animate); sprite.runAction(forever); moveToNext();}int speed=40;// 僵尸的速度public void moveToNext() { position++; if (position < roadPoits.size()) { CGPoint cgPoint = roadPoits.get(position); //计算两点的距离除以速度得到距离 float t= CGPointUtil.distance(roadPoits.get(position-1), cgPoint)/speed; CCMoveTo moveTo = CCMoveTo.action(t, cgPoint); // 调用一个对象的某一个方法 CCSequence ccSequence = CCSequence.actions(moveTo, CCCallFunc.action(this, "moveToNext")); sprite.runAction(ccSequence); } else { //雪停下来 system.stopSystem();// 停止粒子系统 sprite.stopAllActions();//停止所有动作 // 跳舞 dance(); SoundEngine engine=SoundEngine.sharedEngine(); // 1 上下文 2. 音乐资源的id 3 是否循环播放 engine.playSound(CCDirector.theApp, R.raw.psy, true); }}
让地图跟随手指的移动而移动
@Overridepublic boolean ccTouchesMoved(MotionEvent event) { map.touchMove(event, map);// 地图会随着手指的移动而移动 如果该方法生效 必须保证地图的锚点在中间位置 return super.ccTouchesMoved(event);}
游戏的暂停与继续
@Overridepublic boolean ccTouchesBegan(MotionEvent event) { this.onExit(); // 暂停 this.getParent().addChild(new PauseLayer());// 让场景添加新的图层 return super.ccTouchesBegan(event);}// 专门用来暂停的图层private class PauseLayer extends CCLayer{ private CCSprite heart; public PauseLayer(){ this.setIsTouchEnabled(true);// 打开触摸事件的开关 heart = CCSprite.sprite("heart.png"); // 获取屏幕的尺寸 CGSize winSize = CCDirector.sharedDirector().getWinSize(); heart.setPosition(winSize.width/2, winSize.height/2);// 让图片再屏幕的中间 this.addChild(heart); } // 当点击PauseLayer的时候 @Override public boolean ccTouchesBegan(MotionEvent event) { CGRect boundingBox = heart.getBoundingBox(); // 把Android坐标系中的点 转换成Cocos2d坐标系中的点 CGPoint convertTouchToNodeSpace = this.convertTouchToNodeSpace(event); if(CGRect.containsPoint(boundingBox, convertTouchToNodeSpace)){// 确实点击了心 this.removeSelf();// 回收当前图层 DemoLayer.this.onEnter();//游戏继续 } return super.ccTouchesBegan(event); }}
源码:http://download.csdn.net/detail/qq_27280457/9628545
0 0
- 游戏开发入门03
- DirectX游戏开发入门
- 游戏开发入门参考
- 游戏服务端开发入门
- android 游戏开发入门
- android 游戏开发入门
- android 游戏开发入门
- 游戏开发入门参考
- 游戏开发入门
- 游戏开发入门
- 游戏开发入门
- Android 游戏开发入门
- 游戏开发入门01
- 游戏开发入门02
- 游戏引擎与游戏引擎开发入门
- DX游戏开发入门[转]
- 游戏开发入门图书推荐
- 游戏开发入门图书推荐
- sql sql*plus
- weblogic/c3p0配置mysql数据源出现乱码问题
- iOS开发:电池电量监测
- 第 5章 Jenkins – Maven Setup
- leetcode 38. Count and Say
- 游戏开发入门03
- Java web图片验证功能实现二
- iscsi endless conn error investigation
- android studio ndk生成h头文件(找不到类的问题)
- jquery3.0源码解读(三)Selector
- 测试函数运行时间
- 第三方apk内置因签名导致SystemUI未启动启动问题案例分析
- 排序算法:插入 冒泡 快速排序
- 关于时间格式yyyy-mm-dd 格式在ie8返回NAN问题