从0开始用CocosCreator开发飞机大战小游戏(十六)

290 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第24天,点击查看活动详情

上次说到了声音方面的东西,只说了一些配置声音的,但还没有说到在哪里调用音频。今天就说下这些

1. 打开play.ts脚本。

(1)在发射普通子弹函数shootNormalBullet的最后加上播放声音

this.persistNode.getComponent(AudioSource).playOneShot(this.persistNode.getComponent(persistNode).bulletAudioClip)

  (2)在发射激光子弹函数shootLightlBullet的最后加上播放声音    this.persistNode.getComponent(AudioSource).playOneShot(this.persistNode.getComponent(persistNode).lightAudioClip)

  (3)在发射激光子弹函数shootmissileBullet的最后加上播放声音   this.persistNode.getComponent(AudioSource).playOneShot(this.persistNode.getComponent(persistNode).missileAudioClip)

2. 打开enemy.ts脚本

(1)引入AudioSource image.png

(2)在onBeginContact函数里加上      this.persistNode.getComponent(AudioSource).playOneShot(this.persistNode.getComponent(persistNode).boomAudioClip) image.png

(3)同时也是在onBeginContact函数里加游戏结束时的声音

 this.persistNode.getComponent(AudioSource).playOneShot(this.persistNode.getComponent(persistNode).gameOverAudioClip) image.png

3.打开playerBullet.ts脚本

(1)引入AudioSource image.png

(2) 同样也在onBeginContact函数里加上

  this.persistNode.getComponent(AudioSource).playOneShot(this.persistNode.getComponent(persistNode).boomAudioClip) image.png

4.打开enemyBullet.ts脚本

(1)引入AudioSource image.png   (2)同样也是在onBeginContact函数里加上     this.persistNode.getComponent(AudioSource).playOneShot(this.persistNode.getComponent(persistNode).gameOverAudioClip) image.png

至此游戏音效功能就做完了

5. 接下就说下游戏敌机爆炸的效果,并且制作一个动画爆炸的预制体

(1)新建一个Sprite(精灵),并且命名为animation image.png (2)在assets下新建一个文件夹,并且命名为animaition,专门用来存放动画文件 image.png

(3) 在animation文件夹下新建一个动画剪辑(Animation Clip),并且命名为boom image.png

(4)选择animation节点

image.png

(5)打开动画编辑器,并且将动画文件boom拖动过来,这样就可以对boom动画进行编辑了 image.png

(6)添加属性轨道 image.png

image.png

(7)将爆炸效果的图片拖动到轨道中 image.png

(8)调整动画的大小 image.png

(9)最后保存动画 image.png

(10)将节点animation拖动到prefab文件夹里 image.png

(11)删除节点animation,这样爆炸效果的预制体就制作完了

6. 打开persistNode.ts脚本

(1)添加装饰器

    @property(Prefab)

    animationPrefab:Prefab=null;  //爆炸动画预制体

(2)打开常驻节点PersistNode。关联animation预制体 image.png

(3)在srcipt文件夹下新建一个脚本,并且命名为animation.ts image.png

(4) 打来prefab文件下的animation预制体。将脚本animation.ts挂载到animation预制体上去。 image.png

今天就到这里了,主要完善了声音方面的东西和制作爆炸效果预制体。下次我们继续完爆炸效果这个功能。可能写的过程中还有很多不好的地方,希望大家能指出来,在此,谢谢大家