第三十九天 一乐在其中—Android的小游戏打飞机(三)让子弹飞
2014-08-08 13:18
232 查看
8月7日,小雨。“乳鸦啼散玉屏空,一枕新凉一扇风。睡起秋色无觅处,满阶梧桐月明中。”
上篇已经让飞机随手指自由移动,本篇加载子弹和音效及背景音乐。
本篇要用到的几个函数讲解:
一、cocos2d的音效、背景音乐函数如下:
1、SoundEngine.sharedEngine().playSound(Context
ctxt,intresId,
booleanloop)
用于播放背景音乐。
2、SoundEngine.sharedEngine().playEffect(Contextapp,intresId)
用于播放音效,如子弹射击、碰撞、爆炸等音乐效果。
3、
Completely
shutdownthesoundsystem
二、单线程的定时器schedule
schedule(Stringselector,float
interval)
第二个参数的意思是,每隔多少秒执行一次函数,记住,单位是秒。
为了使游戏变得更加有趣,我们会随着时间连续不断地发射一些精灵出来。可以使用cocos2d的定时scheduler,并指定一个回调函数来完成此功能。一秒钟或半秒调用一次回调函数就可以了。
三、action
cocos2d里面提供了许多非常方便的内置的action,你可以使用这样action来让你的精灵动起来。比如move action,jump action,fade action,animation action(就是播放图片序列)等。这里,我们对目标对象子弹使用了3种类型的action:
CCMoveTo:我们使用CCMoveTo.action让子弹移动到屏幕的上端,直到移出屏幕。注意,这里我们可以指定这个过程要花费多长时间。这里使用了变化的时间间隔2-4秒。
CCCallFuncN:它可以让你为某个执行此action的对象指定一个回调函数。我们指定的回调函数是:onBulletMoveToFinished。
CCSequence:它允许我们把一系列的action组成一个action序列,并且这些acton可以按顺序执行。一次执行完所有的action。
四、MainActivity.java
packageedu.eurasia.cocos2d_game03;
importorg.cocos2d.layers.CCScene;
importorg.cocos2d.nodes.CCDirector;
importorg.cocos2d.opengl.CCGLSurfaceView;
importorg.cocos2d.sound.SoundEngine;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Window;
importandroid.view.WindowManager;
publicclassMainActivityextendsActivity{
//创建一个view对象,cocos2d引擎会把图形绘制在该view对象上面
privateCCGLSurfaceViewview=null;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//不显示标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置当前程序全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置不允许屏幕自动休眠
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
view=newCCGLSurfaceView(this);
setContentView(view);
//获取导演对象
CCDirectordirector=CCDirector.sharedDirector();
//设置游戏引擎画面的输出目标View
director.attachInView(view);
//设置游戏是否显示FPS值
//director.setDisplayFPS(true);
//设置游戏的刷新率FPS=framepersecond
director.setAnimationInterval(1/60.0f);
//生成场景对象
CCScenescene=CCScene.node();
//生成图层对象
PlaneLayerlayer=newPlaneLayer(this);
//将图层添加至场景当中
scene.addChild(layer,1);
//通知导演,运行场景
director.runWithScene(scene);
}
@Override
protectedvoidonDestroy(){
super.onDestroy();
//清理所有的音效
SoundEngine.sharedEngine().realesAllSounds();
SoundEngine.sharedEngine().realesAllEffects();
//完全关闭音响系统
SoundEngine.purgeSharedEngine();
}
}
五、PlaneLayer.java
packageedu.eurasia.cocos2d_game03;
importjava.util.ArrayList;
importjava.util.List;
importorg.cocos2d.actions.instant.CCCallFuncN;
importorg.cocos2d.actions.interval.CCMoveTo;
importorg.cocos2d.actions.interval.CCSequence;
importorg.cocos2d.layers.CCLayer;
importorg.cocos2d.nodes.CCDirector;
importorg.cocos2d.nodes.CCSprite;
importorg.cocos2d.sound.SoundEngine;
importorg.cocos2d.types.CGPoint;
importorg.cocos2d.types.CGRect;
importorg.cocos2d.types.CGSize;
importandroid.content.Context;
importandroid.view.MotionEvent;
publicclassPlaneLayerextendsCCLayer{
//声明一个精灵对象
privateCCSpriteplane;
privateCCDirectordirector;
privateCGSizewinSize;
privateCGPointoffset;
privatebooleanflag=false;
//定义子弹的速度为每秒500像素
privatefloatbulletSpeed=500;
privateContextcontext;
privateList<CCSprite>bullets=newArrayList<CCSprite>();
publicPlaneLayer(Contextcontext){
super();
this.context=context;
//设置当前图层是否接受触摸事件
this.setIsTouchEnabled(true);
director=CCDirector.sharedDirector();
winSize=director.winSize();
//初始化精灵对象
plane=CCSprite.sprite("p.png");
//设置精灵对象的位置
plane.setPosition(CGPoint.ccp(winSize.width/2,200));
this.addChild(plane);
//定时器schedule
schedule("addBullet",0.5f);
//背景音乐
SoundEngine.sharedEngine().playSound(context,R.raw.game_music,true);
}
//当用户开始触摸屏幕,执行该方法
@Override
publicbooleanccTouchesBegan(MotionEventevent){
CGPointpoint=director.convertToGL(CGPoint.ccp(event.getX(),
event.getY()));
CGRectrect=plane.getBoundingBox();
flag=CGRect.containsPoint(rect,point);
if(flag){
offset=CGPoint.ccpSub(plane.getPosition(),point);
}
returnsuper.ccTouchesBegan(event);
}
//当用户手指离开屏幕时,执行该方法
@Override
publicbooleanccTouchesEnded(MotionEventevent){
flag=false;
returnsuper.ccTouchesEnded(event);
}
//当用户手指在屏幕移动时,执行该方法
@Override
publicbooleanccTouchesMoved(MotionEventevent){
if(flag){
CGPointpoint=director.convertToGL(CGPoint.ccp(event.getX(),
event.getY()));
point=CGPoint.ccpAdd(point,offset);
plane.setPosition(point);
}
returnsuper.ccTouchesMoved(event);
}
publicvoidaddBullet(floatdelta){
//生成一个子弹精灵对象
CCSpritebullet=CCSprite.sprite("bullet.png");
//将子弹对象添加至图层当中
this.addChild(bullet);
//将新添加的子弹对象放置在bullets集合当中
bullets.add(bullet);
//获得精灵的大小,getContentSize函数来获得节点原始的大小
CGSizeplaneSize=plane.getContentSize();
CGSizebulletSize=bullet.getContentSize();
CGPointinitPos=plane.getPosition();
//子弹的y轴的初始位置
initPos.y=initPos.y+planeSize.height/2+bulletSize.height/2;
bullet.setPosition(initPos);
//创建一个代表坐标的对象
CGPointtargetPos=CGPoint.ccp(initPos.x,winSize.height);
//计算两个坐标点之间的距离,计算子弹运行的距离
floatdistance=CGPoint.ccpDistance(initPos,targetPos);
//计算子弹运行的时间
floatt=distance/bulletSpeed;
//生成一个动画对象,让子弹移动到屏幕的上端
CCMoveTomoveTo=CCMoveTo.action(t,targetPos);
//生成一个动作对象,该动作执行时,将会调用当前对象的onBulletMoveToFinished方法
//CCCallFuncN:
//它可以让你为某个执行此action的对象指定一个回调函数。我们指定的回调函数是:onBulletMoveToFinished
CCCallFuncNfunc=CCCallFuncN.action(this,"onBulletMoveToFinished");
//CCSequence:
//它允许我们把一系列的action组成一个action序列,并且这些acton可以按顺序执行。一次执行完所有的action。
CCSequenceseq=CCSequence.actions(moveTo,func);
//通知精灵执行动作
bullet.runAction(seq);
//子弹声效
SoundEngine.sharedEngine().playEffect(context,R.raw.bullet);
}
publicvoidonBulletMoveToFinished(Objectsender){
if(senderinstanceofCCSprite){
CCSpritesprite=(CCSprite)sender;
//将子弹对象从集合中移除
bullets.remove(sprite);
//将子弹对象从屏幕中移除
this.removeChild(sprite,true);
}
}
}
六、运行结果
源代码下载地址:http://download.csdn.net/detail/zwszws/7731459
上篇已经让飞机随手指自由移动,本篇加载子弹和音效及背景音乐。
本篇要用到的几个函数讲解:
一、cocos2d的音效、背景音乐函数如下:
1、SoundEngine.sharedEngine().playSound(Context
ctxt,intresId,
booleanloop)
用于播放背景音乐。
2、SoundEngine.sharedEngine().playEffect(Contextapp,intresId)
用于播放音效,如子弹射击、碰撞、爆炸等音乐效果。
3、
SoundEngine.sharedEngine().realesAllSounds();
清理所有的音效-Cleaneverythingup
SoundEngine.purgeSharedEngine();
完全关闭音响系统
-
CCMoveTo:我们使用CCMoveTo.action让子弹移动到屏幕的上端,直到移出屏幕。注意,这里我们可以指定这个过程要花费多长时间。这里使用了变化的时间间隔2-4秒。
CCCallFuncN:它可以让你为某个执行此action的对象指定一个回调函数。我们指定的回调函数是:onBulletMoveToFinished。
CCSequence:它允许我们把一系列的action组成一个action序列,并且这些acton可以按顺序执行。一次执行完所有的action。
importorg.cocos2d.layers.CCScene;
importorg.cocos2d.nodes.CCDirector;
importorg.cocos2d.opengl.CCGLSurfaceView;
importorg.cocos2d.sound.SoundEngine;
importandroid.os.Bundle;
importandroid.app.Activity;
importandroid.view.Window;
importandroid.view.WindowManager;
publicclassMainActivityextendsActivity{
//创建一个view对象,cocos2d引擎会把图形绘制在该view对象上面
privateCCGLSurfaceViewview=null;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
//不显示标题栏
requestWindowFeature(Window.FEATURE_NO_TITLE);
//设置当前程序全屏显示
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
//设置不允许屏幕自动休眠
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
view=newCCGLSurfaceView(this);
setContentView(view);
//获取导演对象
CCDirectordirector=CCDirector.sharedDirector();
//设置游戏引擎画面的输出目标View
director.attachInView(view);
//设置游戏是否显示FPS值
//director.setDisplayFPS(true);
//设置游戏的刷新率FPS=framepersecond
director.setAnimationInterval(1/60.0f);
//生成场景对象
CCScenescene=CCScene.node();
//生成图层对象
PlaneLayerlayer=newPlaneLayer(this);
//将图层添加至场景当中
scene.addChild(layer,1);
//通知导演,运行场景
director.runWithScene(scene);
}
@Override
protectedvoidonDestroy(){
super.onDestroy();
//清理所有的音效
SoundEngine.sharedEngine().realesAllSounds();
SoundEngine.sharedEngine().realesAllEffects();
//完全关闭音响系统
SoundEngine.purgeSharedEngine();
}
}
importjava.util.ArrayList;
importjava.util.List;
importorg.cocos2d.actions.instant.CCCallFuncN;
importorg.cocos2d.actions.interval.CCMoveTo;
importorg.cocos2d.actions.interval.CCSequence;
importorg.cocos2d.layers.CCLayer;
importorg.cocos2d.nodes.CCDirector;
importorg.cocos2d.nodes.CCSprite;
importorg.cocos2d.sound.SoundEngine;
importorg.cocos2d.types.CGPoint;
importorg.cocos2d.types.CGRect;
importorg.cocos2d.types.CGSize;
importandroid.content.Context;
importandroid.view.MotionEvent;
publicclassPlaneLayerextendsCCLayer{
//声明一个精灵对象
privateCCSpriteplane;
privateCCDirectordirector;
privateCGSizewinSize;
privateCGPointoffset;
privatebooleanflag=false;
//定义子弹的速度为每秒500像素
privatefloatbulletSpeed=500;
privateContextcontext;
privateList<CCSprite>bullets=newArrayList<CCSprite>();
publicPlaneLayer(Contextcontext){
super();
this.context=context;
//设置当前图层是否接受触摸事件
this.setIsTouchEnabled(true);
director=CCDirector.sharedDirector();
winSize=director.winSize();
//初始化精灵对象
plane=CCSprite.sprite("p.png");
//设置精灵对象的位置
plane.setPosition(CGPoint.ccp(winSize.width/2,200));
this.addChild(plane);
//定时器schedule
schedule("addBullet",0.5f);
//背景音乐
SoundEngine.sharedEngine().playSound(context,R.raw.game_music,true);
}
//当用户开始触摸屏幕,执行该方法
@Override
publicbooleanccTouchesBegan(MotionEventevent){
CGPointpoint=director.convertToGL(CGPoint.ccp(event.getX(),
event.getY()));
CGRectrect=plane.getBoundingBox();
flag=CGRect.containsPoint(rect,point);
if(flag){
offset=CGPoint.ccpSub(plane.getPosition(),point);
}
returnsuper.ccTouchesBegan(event);
}
//当用户手指离开屏幕时,执行该方法
@Override
publicbooleanccTouchesEnded(MotionEventevent){
flag=false;
returnsuper.ccTouchesEnded(event);
}
//当用户手指在屏幕移动时,执行该方法
@Override
publicbooleanccTouchesMoved(MotionEventevent){
if(flag){
CGPointpoint=director.convertToGL(CGPoint.ccp(event.getX(),
event.getY()));
point=CGPoint.ccpAdd(point,offset);
plane.setPosition(point);
}
returnsuper.ccTouchesMoved(event);
}
publicvoidaddBullet(floatdelta){
//生成一个子弹精灵对象
CCSpritebullet=CCSprite.sprite("bullet.png");
//将子弹对象添加至图层当中
this.addChild(bullet);
//将新添加的子弹对象放置在bullets集合当中
bullets.add(bullet);
//获得精灵的大小,getContentSize函数来获得节点原始的大小
CGSizeplaneSize=plane.getContentSize();
CGSizebulletSize=bullet.getContentSize();
CGPointinitPos=plane.getPosition();
//子弹的y轴的初始位置
initPos.y=initPos.y+planeSize.height/2+bulletSize.height/2;
bullet.setPosition(initPos);
//创建一个代表坐标的对象
CGPointtargetPos=CGPoint.ccp(initPos.x,winSize.height);
//计算两个坐标点之间的距离,计算子弹运行的距离
floatdistance=CGPoint.ccpDistance(initPos,targetPos);
//计算子弹运行的时间
floatt=distance/bulletSpeed;
//生成一个动画对象,让子弹移动到屏幕的上端
CCMoveTomoveTo=CCMoveTo.action(t,targetPos);
//生成一个动作对象,该动作执行时,将会调用当前对象的onBulletMoveToFinished方法
//CCCallFuncN:
//它可以让你为某个执行此action的对象指定一个回调函数。我们指定的回调函数是:onBulletMoveToFinished
CCCallFuncNfunc=CCCallFuncN.action(this,"onBulletMoveToFinished");
//CCSequence:
//它允许我们把一系列的action组成一个action序列,并且这些acton可以按顺序执行。一次执行完所有的action。
CCSequenceseq=CCSequence.actions(moveTo,func);
//通知精灵执行动作
bullet.runAction(seq);
//子弹声效
SoundEngine.sharedEngine().playEffect(context,R.raw.bullet);
}
publicvoidonBulletMoveToFinished(Objectsender){
if(senderinstanceofCCSprite){
CCSpritesprite=(CCSprite)sender;
//将子弹对象从集合中移除
bullets.remove(sprite);
//将子弹对象从屏幕中移除
this.removeChild(sprite,true);
}
}
}
源代码下载地址:http://download.csdn.net/detail/zwszws/7731459
相关文章推荐
- 第三十七天 一乐在其中—Android的小游戏打飞机(一)环境搭建
- 第四十一天 一乐在其中—Android的小游戏打飞机(五)添加背景
- 第三十八天 一乐在其中—Android的小游戏打飞机(二)移动飞机
- 第四十天 一乐在其中—Android的小游戏打飞机(四)添加敌机
- Android 打飞机小游戏 cocos2d
- android小游戏 飞机躲子弹
- Android 打飞机 小游戏 的实现 - 画面移动 对象封装 碰撞效果
- python-小游戏(打飞机)v1.2(三发子弹)
- unity3d开发 打飞机小游戏(三)(飞机子弹设置)
- unity3d开发 打飞机小游戏(三)(飞机子弹设置)
- Android实战打飞机游戏之子弹生成与碰撞以及爆炸效果(5)
- android 游戏 实战打飞机游戏 子弹生成与碰撞 以及爆炸效果(5)
- Android 打飞机 小游戏 的实现 - 画面移动 对象封装 碰撞效果
- Android开发资料超级给力小游戏(精典美女搓搓 妄撮版)源码
- Android 小游戏
- 3天打造一个 android 小游戏
- 汉字先生-----初学android时做的小游戏
- 开源项目之Android小游戏 Android Shapes
- Android小游戏-九宫格,有源码的哦!
- 妄撮小游戏的开发思想-Android开发资料-《妄撮(撕开美女衣服)》游戏源代码外传