游戏地图,精灵移动处理,粒子系统,声音引擎,地图移动,游戏暂停

来源:互联网 发布:js修改对象的属性值 编辑:程序博客网 时间:2024/05/29 19:24

游戏地图的制作

工具:Tiled绘图工具

步骤:
1、新建地图,设置地图大小(以块为单位),设置块大小(以像素为单位)
2、显示网格需要设置视图中的显示网格选项
3、通过地图—新图块,来导入地图资源,从而利用工具进行地图的编辑
4、对象层,通过在图层里右键添加。

生成的tmx文件说明:
1、image source属性中填写相对路径
2、层和对象命名尽量使用英文
3、对象层中信息,封装在objectgroup
4、objectgroup中信息按照添加顺序进行排列object

制作完毕后,将地图资源以及制作好的地图tmx文件都放到assets文件夹下。


游戏地图的加载

CCTMXTiledMap map = CCTMXTiledMap.tiledMap("map.tmx");

加载后,添加到当前显示层上即可。地图的默认锚点是(0, 0)。


对象层加载


roads = new ArrayList<CGPoint>();CCTMXObjectGroup objectGroup = map.objectGroupNamed("road");ArrayList<HashMap<String, String>> objects = objectGroup.objects;for(HashMap<String, String> item: objects){int x = Integer.parseInt(item.get("x"));int y = Integer.parseInt(item.get("y"));roads.add(CGPoint.ccp(x, y));}


精灵移动处理

对象层加载后,所有的点坐标信息都在roads列表中。

CCCallFunc.action(Object target, String selector);//target是Object对象,selector是要调用函数的名字,另外,该函数必须为public类型的。该action的动作是调用target对象中的名为selector的方法。
CGPointUtil.distance(p1, p2);//计算两个点之间的距离。

private int speed = 30;private int current = 0;public void moveToNext(){current++;if(current < roads.size()){float time = CGPointUtil.distance(sprite.getPosition(), roads.get(current))/speed;CCMoveTo moveTo = CCMoveTo.action(time, roads.get(current));CCSequence sequence = CCSequence.actions(moveTo, CCCallFunc.action(this, "moveToNext"));sprite.runAction(sequence);}}
上面函数能够使精灵从起点走到终点,添加精灵后,设置精灵的位置为起点,然后再调用moveToNext方法即可。


移动过程中播放序列帧

String.format(String format, Object args);使用指定的格式字符串和参数返回一个格式化的字符串,如:

String.format("sprite%02d.png",5);即:用5替换%02d,替换后是sprite5.png。%02d的意思是:最多能替换两位数,不足两位数的,在前面补0.

private void animate() {ArrayList<CCSpriteFrame> frames = new ArrayList<CCSpriteFrame>();String fileName = "z_1_%02d.png";for(int i = 1; i<=7;i++){CCSpriteFrame frame = CCSprite.sprite(String.format(fileName, i)).displayedFrame();frames.add(frame);}CCAnimation anim = CCAnimation.animation("", 0.2f, frames);CCAnimate animate = CCAnimate.action(anim);CCRepeatForever forever = CCRepeatForever.action(animate);sprite.runAction(forever);}


粒子系统

粒子系统表示三维计算机图形学中模拟一些特定的模糊现象的技术。经常使用的粒子系统模拟的现象有火、爆炸、烟、水流、火花、落叶、云、雾、雪、尘、流星尾迹或者像发光轨迹这样的抽象视觉效果等等。

private void particleSystem() {CCParticleSystem snow = CCParticleSnow.node();snow.setTexture(CCTextureCache.sharedTextureCache().addImage("snow.png"));this.addChild(snow);}
图片也是放在assets文件夹下。

snow.stopSystem();//停止粒子系统

sprite.stopAllActions();//停止精灵的所有动作

自定义效果

制作工具ParticleDesigner,使用工具将效果导出成.plist文件。



声音引擎

声音类型:音乐和音效

可以从长短上来做区分,音乐一般用于游戏的背景声音,音效主要是短小的声音,如植物发射子弹,僵尸吃植物时发出的声音。

声音控制,如播放、暂停、声音大小调节、静音。

通过缓存音乐和音效文件来提高声音处理的效率。

声音文件放在res文件夹下的raw文件夹下。

SoundEngine soundEngine = SoundEngine.sharedEngine();//单例模式

声音引擎的方法有:

setEffectsVolume(Float volume);设置音效的音量

getEffectsVolume();获得音效的音量

setSoundVolume(Float volume);设置音乐的音量

getSoundsVolume();获得音乐的音量

mute();静音

unmute();不静音

isMute();当前是否是静音状态

preloadEffect(Context app, int resId);预加载音效

playEffect(Context app, int resId,boolean isloop);播放音效,app固定是CCDirector.theApp。因为在创建CCGLSurfaceView的时候,在它的构造方法中将context存储为CCDirector.theApp了。

pauseSound();暂停声音

resumeSound();继续播放


地图移动

大地图的移动

this.runAction(CCFollow.action(followedNode));

this跟随着followedNode的移动而移动,现象是followedNode一直处在地图的正中间

小地图的移动

小地图的移动需要处理点击事件,需要设置setIsTouchEnabled(true);重写ccTouchesMoved方法,在该方法内处理地图的移动。

@Overridepublic boolean ccTouchesMoved(MotionEvent event) {map.touchMove(event, map);return super.ccTouchesMoved(event);}

如果需要手动移动地图,加载地图的时候必须将锚点设置到中心点上,修改地图的坐标(中点)。

map.setAnchorPoint(0.5f, 0.5f);CGSize mapSize = map.getContentSize();// 获得地图的大小map.setPosition(mapSize.width/2, mapSize.height/2);

另外,添加精灵的时候,应该用map.addChild(sprite);要不然精灵不随地图的移动而移动。


游戏暂停

处理游戏暂停的思路:

当前主CCLayer处于暂停状态时,无法接收用户的触摸事件。在主CCLayer暂停之前需要启动另外一个CCLayer显示,除提示玩家游戏已经暂停外,如果玩家需要切换到游戏界面时,可以点击当前CCLayer中的按钮完成继续游戏的处理。

注意:

①该暂停用CCLayer必须添加到与主CCLayer相同的parent下。

②点击继续游戏时,需要清除当前显示的CCLayer。

核心方法:onExit()--游戏暂停、onEnter()--游戏继续

主Layer中重写touch方法:

@Overridepublic boolean ccTouchesBegan(MotionEvent event) {// 主Layer处于冻结状态,此时该Layer不能响应touch事件this.onExit();// 显示暂停LayerPauseLayer pauseLayer = new PauseLayer();this.getParent().addChild(pauseLayer);return super.ccTouchesBegan(event);}
暂停Layer中重写touch方法:

@Overridepublic boolean ccTouchesBegan(MotionEvent event) {CGPoint touchPos = this.convertTouchToNodeSpace(event);if(CGRect.containsPoint(playOn.getBoundingBox(), touchPos)){// 游戏继续// 销毁掉当前Layerthis.removeSelf();GameLayer.this.onEnter();}return super.ccTouchesBegan(event);}
playOn是暂停Layer中的继续游戏按钮,GameLayer是主Layer。

0 0
原创粉丝点击