生活随笔
收集整理的這篇文章主要介紹了
cocos做飞机大战笔记【添加游戏音效】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄 前言 添加音頻腳本并綁定組件 子彈發射播放音頻并將播放音頻方法在管理腳本中暴露 點擊按鈕播放音頻 敵機銷毀的時候播放音頻 玩家飛機銷毀播放音頻 完整代碼
前言
游戲音效會分為游戲開始音效,玩家飛機死亡音效,敵機死亡音效,以及子彈發射音效和按鈕點擊音效
添加音頻腳本并綁定組件
首先創建個空節點audio用于存放音頻,然后將背景音樂掛在其下
音頻腳本
創建腳本AudioManager.ts,將需要的音頻用一個數組接收, 將音頻腳本掛入引擎
子彈發射播放音頻并將播放音頻方法在管理腳本中暴露
將音頻節點掛入
點擊按鈕播放音頻
敵機銷毀的時候播放音頻
玩家飛機銷毀播放音頻
在引擎中添加音頻
完整代碼
import { _decorator
, Component
, Node
, AudioClip
, AudioSource
} from 'cc' ;
const { ccclass
, property
} = _decorator
; interface IAudioMap { [ name
: string ] : AudioClip
;
} @
ccclass ( 'AudioManager' )
export class AudioManager extends Component { @
property ( [ AudioClip
] ) public audioList
: AudioClip
[ ] = [ ] ; private _dict
: IAudioMap
= { } ; private _audioSource
: AudioSource
= null ; start ( ) { for ( let i
= 0 ; i
< this . audioList
. length
; i
++ ) { const element
= this . audioList
[ i
] ; this . _dict
[ element
. name
] = element
; } this . _audioSource
= this . getComponent ( AudioSource
) ; } public play ( name
: string ) { const audioClip
= this . _dict
[ name
] ; if ( audioClip
!== undefined ) { this . _audioSource
. playOneShot ( audioClip
) ; } }
}
import { _decorator
, Component
, Node
, Prefab
, instantiate
, math
, Vec3
, BoxCollider
, macro
, Label
} from 'cc' ;
import { Bullet
} from '../bullet/Bullet' ;
import { BulletProp
} from '../bullet/BulletProp' ;
import { EnemyPlane
} from '../plane/EnemyPlane' ;
import { SelfPlane
} from '../plane/SelfPlane' ;
import { AudioManager
} from './AudioManager' ;
import { Constant
} from './Constant' ;
const { ccclass
, property
} = _decorator
; @
ccclass ( 'GameManager' )
export class GameManager extends Component { @
property ( SelfPlane
) public playerPlane
: SelfPlane
= null ; @
property ( Prefab
) public bullet01
: Prefab
= null ; @
property ( Prefab
) public bullet02
: Prefab
= null ; @
property ( Prefab
) public bullet03
: Prefab
= null ; @
property ( Prefab
) public bullet04
: Prefab
= null ; @
property ( Prefab
) public bullet05
: Prefab
= null ; @property
public shootTime
= 0.3 ; @property
public bulletSpeed
= 1 ; @
property ( Node
) public bulletRoot
: Node
= null ; @
property ( Prefab
) public enemy01
: Prefab
= null ; @
property ( Prefab
) public enemy02
: Prefab
= null ; @property
public createEnemtTime
= 1 ; @property
public enemy1Speed
= 0.5 ; @property
public enemy2Speed
= 0.7 ; @
property ( Prefab
) public bulletPropM
: Prefab
= null ; @
property ( Prefab
) public bulletPropH
: Prefab
= null ; @
property ( Prefab
) public bulletPropS
: Prefab
= null ; @property
public bulletPropSpeed
= 0.3 ; @
property ( Node
) public gamePage
: Node
= null ; @
property ( Node
) public gameOverPage
: Node
= null ; @
property ( Label
) public gameOverScore
: Label
= null ; @
property ( Label
) public gameScore
: Label
= null ; @
property ( AudioManager
) public audioEffect
: AudioManager
= null ; public isGameStart
= false ; private _currShootTime
= 0 ; private _isShooting
= false ; private _currCreateEnemyTime
= 0 private _combinationInterval
= Constant
. Combination
. PLAN1 private _bulletType
= Constant
. BulletPropType
. BULLET_M ; private _score
= 0 ; start ( ) { this . _init ( ) ; } update ( deltaTime
: number ) { if ( ! this . isGameStart
) { return ; } if ( this . playerPlane
. isDie
) { this . gameOver ( ) ; return ; } this . _currShootTime
+= deltaTime
; if ( this . _isShooting
&& this . _currShootTime
> this . shootTime
) { if ( this . _bulletType
=== Constant
. BulletPropType
. BULLET_H ) { this . createPlayerBulletH ( ) ; } else if ( this . _bulletType
=== Constant
. BulletPropType
. BULLET_S ) { this . createPlayerBulletS ( ) ; } else { this . createPlayerBulletM ( ) ; } const name
= 'bullet' + ( this . _bulletType
% 2 + 1 ) ; this . playAudioEffect ( name
) ; this . _currShootTime
= 0 ; } this . _currCreateEnemyTime
+= deltaTime
if ( this . _combinationInterval
== Constant
. Combination
. PLAN1 ) { if ( this . _currCreateEnemyTime
> this . createEnemtTime
) { this . createEnemyPlane ( ) this . _currCreateEnemyTime
= 0 } } else if ( this . _combinationInterval
== Constant
. Combination
. PLAN2 ) { if ( this . _currCreateEnemyTime
> this . createEnemtTime
* 0.9 ) { const randomCombination
= math
. randomRangeInt ( 1 , 3 ) if ( randomCombination
=== Constant
. Combination
. PLAN2 ) { this . createCombination1 ( ) } else { this . createEnemyPlane ( ) } this . _currCreateEnemyTime
= 0 } } else { if ( this . _currCreateEnemyTime
> this . createEnemtTime
* 0.8 ) { const randomCombination
= math
. randomRangeInt ( 1 , 4 ) if ( randomCombination
=== Constant
. Combination
. PLAN2 ) { this . createCombination1 ( ) } else if ( randomCombination
=== Constant
. Combination
. PLAN3 ) { this . createCombination2 ( ) } else { this . createEnemyPlane ( ) } this . _currCreateEnemyTime
= 0 } } } public returnMain ( ) { this . _currShootTime
= 0 ; this . _currCreateEnemyTime
= 0 ; this . _combinationInterval
= Constant
. Combination
. PLAN1 ; this . _bulletType
= Constant
. BulletPropType
. BULLET_M ; this . playerPlane
. node
. setPosition ( 0 , 0 , 15 ) ; this . _score
= 0 ; } public gameStart ( ) { this . isGameStart
= true ; this . _changePlanMode ( ) ; this . _score
= 0 ; this . gameScore
. string = this . _score
. toString ( ) ; } public gameReStart ( ) { this . isGameStart
= true ; this . _currShootTime
= 0 ; this . _currCreateEnemyTime
= 0 ; this . _combinationInterval
= Constant
. Combination
. PLAN1 ; this . _bulletType
= Constant
. BulletPropType
. BULLET_M ; this . playerPlane
. node
. setPosition ( 0 , 0 , 15 ) ; this . _score
= 0 ; this . _changePlanMode ( ) ; this . gameScore
. string = this . _score
. toString ( ) ; } public gameOver ( ) { this . isGameStart
= false ; this . gamePage
. active
= false ; this . gameOverPage
. active
= true ; this . gameOverScore
. string = this . _score
. toString ( ) ; this . _isShooting
= false ; this . unschedule ( this . _modeChanged
) ; this . playerPlane
. init ( ) ; this . _destroyAll ( ) ; } public addScore ( ) { this . _score
++ ; this . gameScore
. string = this . _score
. toString ( ) ; } public createPlayerBulletM ( ) { const bullet
= instantiate ( this . bullet01
) ; bullet
. setParent ( this . bulletRoot
) ; const pos
= this . playerPlane
. node
. position
; bullet
. setPosition ( pos
. x
, pos
. y
, pos
. z
- 7 ) ; const bulletComp
= bullet
. getComponent ( Bullet
) ; bulletComp
. show ( this . bulletSpeed
, false ) } public createPlayerBulletH ( ) { const pos
= this . playerPlane
. node
. position
; const bullet1
= instantiate ( this . bullet03
) ; bullet1
. setParent ( this . bulletRoot
) ; bullet1
. setPosition ( pos
. x
- 2.5 , pos
. y
, pos
. z
- 7 ) ; const bulletComp1
= bullet1
. getComponent ( Bullet
) ; bulletComp1
. show ( this . bulletSpeed
, false ) ; const bullet2
= instantiate ( this . bullet03
) ; bullet2
. setParent ( this . bulletRoot
) ; bullet2
. setPosition ( pos
. x
+ 2.5 , pos
. y
, pos
. z
- 7 ) ; const bulletComp2
= bullet2
. getComponent ( Bullet
) ; bulletComp2
. show ( this . bulletSpeed
, false ) ; } public createPlayerBulletS ( ) { const pos
= this . playerPlane
. node
. position
; const bullet
= instantiate ( this . bullet05
) ; bullet
. setParent ( this . bulletRoot
) ; bullet
. setPosition ( pos
. x
, pos
. y
, pos
. z
- 7 ) ; const bulletComp
= bullet
. getComponent ( Bullet
) ; bulletComp
. show ( this . bulletSpeed
, false ) ; const bullet1
= instantiate ( this . bullet05
) ; bullet1
. setParent ( this . bulletRoot
) ; bullet1
. setPosition ( pos
. x
- 4 , pos
. y
, pos
. z
- 7 ) ; const bulletComp1
= bullet1
. getComponent ( Bullet
) ; bulletComp1
. show ( this . bulletSpeed
, false , Constant
. Direction
. LEFT ) ; const bullet2
= instantiate ( this . bullet05
) ; bullet2
. setParent ( this . bulletRoot
) ; bullet2
. setPosition ( pos
. x
+ 4 , pos
. y
, pos
. z
- 7 ) ; const bulletComp2
= bullet2
. getComponent ( Bullet
) ; bulletComp2
. show ( this . bulletSpeed
, false , Constant
. Direction
. RIGHT ) ; } public createEnemyBullet ( targetPos
: Vec3
) { const bullet
= instantiate ( this . bullet01
) ; bullet
. setParent ( this . bulletRoot
) ; bullet
. setPosition ( targetPos
. x
, targetPos
. y
, targetPos
. z
+ 6 ) ; const bulletComp
= bullet
. getComponent ( Bullet
) ; bulletComp
. show ( 1 , true ) const colliderComp
= bullet
. getComponent ( BoxCollider
) ; colliderComp
. setGroup ( Constant
. CollisionType
. ENEMY_BULLET ) ; colliderComp
. setMask ( Constant
. CollisionType
. SELF_PLANE ) ; } public createEnemyPlane ( ) { const whichEnemy
= math
. randomRangeInt ( 1 , 3 ) let prefab
: Prefab
= null let speed
= 0 if ( whichEnemy
== Constant
. EnemyType
. TYPE1 ) { prefab
= this . enemy01speed
= this . enemy1Speed
} else { prefab
= this . enemy02speed
= this . enemy2Speed
} const enemy
= instantiate ( prefab
) console . log ( enemy
) ; enemy
. setParent ( this . node
) const enemyComp
= enemy
. getComponent ( EnemyPlane
) enemyComp
. show ( this , speed
, true ) const randomPos
= math
. randomRangeInt ( - 25 , 26 ) enemy
. setPosition ( randomPos
, 0 , - 50 ) } public createCombination1 ( ) { const enemyArray
= new Array < Node> ( 5 ) for ( let i
= 0 ; i
< enemyArray
. length
; i
++ ) { enemyArray
[ i
] = instantiate ( this . enemy01
) const element
= enemyArray
[ i
] element
. parent
= this . nodeelement
. setPosition ( - 20 + i
* 10 , 0 , - 50 ) const enemyComp
= element
. getComponent ( EnemyPlane
) enemyComp
. show ( this , this . enemy1Speed
, false ) } } public createCombination2 ( ) { const enemyArray
= new Array < Node> ( 7 ) const combinationPos
= [ - 21 , 0 , - 60 , - 14 , 0 , - 55 , - 7 , 0 , - 50 , 0 , 0 , - 45 , 7 , 0 , - 50 , 14 , 0 , - 55 , 21 , 0 , - 60 ] for ( let i
= 0 ; i
< enemyArray
. length
; i
++ ) { enemyArray
[ i
] = instantiate ( this . enemy02
) const element
= enemyArray
[ i
] element
. parent
= this . node
const startIndex
= i
* 3 element
. setPosition ( combinationPos
[ startIndex
] , combinationPos
[ startIndex
+ 1 ] , combinationPos
[ startIndex
+ 2 ] ) const enemyComp
= element
. getComponent ( EnemyPlane
) enemyComp
. show ( this , this . enemy2Speed
, false ) } } public createBulletProp ( ) { const randomProp
= math
. randomRangeInt ( 1 , 4 ) ; let prefab
: Prefab
= null ; if ( randomProp
=== Constant
. BulletPropType
. BULLET_H ) { prefab
= this . bulletPropH
; } else if ( randomProp
=== Constant
. BulletPropType
. BULLET_S ) { prefab
= this . bulletPropS
; } else { prefab
= this . bulletPropM
; } const prop
= instantiate ( prefab
) ; prop
. setParent ( this . node
) ; prop
. setPosition ( 15 , 0 , - 50 ) ; const propComp
= prop
. getComponent ( BulletProp
) ; propComp
. show ( this , - this . bulletPropSpeed
) ; } public isShooting ( value
: boolean ) { this . _isShooting
= value
; } public changeBulletType ( type : number ) { this . _bulletType
= type ; } public playAudioEffect ( name
: string ) { this . audioEffect
. play ( name
) ; } private _init ( ) { this . _currShootTime
= this . shootTime
; this . playerPlane
. init ( ) ; } private _changePlanMode ( ) { this . schedule ( this . _modeChanged
, 10 , macro
. REPEAT_FOREVER ) ; } private _modeChanged ( ) { this . _combinationInterval
++ this . createBulletProp ( ) ; } private _destroyAll ( ) { let children
= this . node
. children
; let length
= children
. length
; let i
= 0 ; for ( i
= length
- 1 ; i
>= 0 ; i
-- ) { const child
= children
[ i
] ; child
. destroy ( ) ; } children
= this . bulletRoot
. children
; length
= children
. length
; for ( i
= length
- 1 ; i
>= 0 ; i
-- ) { const child
= children
[ i
] ; child
. destroy ( ) ; } }
}
import { _decorator
, Component
, Node
, systemEvent
, SystemEvent
, Touch
, EventTouch
, Vec2
} from 'cc' ;
import { GameManager
} from '../framework/GameManager' ;
const { ccclass
, property
} = _decorator
; @
ccclass ( 'UIMain' )
export class UIMain extends Component { @property
public planeSpeed
= 1 ; @
property ( Node
) public playerPlane
: Node
= null ; @
property ( GameManager
) public gameManager
: GameManager
= null ; @
property ( Node
) public gameStart
: Node
= null ; @
property ( Node
) public game
: Node
= null ; @
property ( Node
) public gameOver
: Node
= null ; start ( ) { this . node
. on ( SystemEvent
. EventType
. TOUCH_START , this . _touchStart
, this ) ; this . node
. on ( SystemEvent
. EventType
. TOUCH_MOVE , this . _touchMove
, this ) ; this . node
. on ( SystemEvent
. EventType
. TOUCH_END , this . _touchEnd
, this ) ; this . gameStart
. active
= true ; } public reStart ( ) { this . gameOver
. active
= false ; this . game
. active
= false ; this . gameManager
. playAudioEffect ( 'button' ) ; this . gameManager
. gameReStart ( ) ; } public returnMain ( ) { this . gameOver
. active
= false ; this . gameStart
. active
= true ; this . gameManager
. playAudioEffect ( 'button' ) ; this . gameManager
. returnMain ( ) ; } _touchStart ( touch
: Touch
, event
: EventTouch
) { if ( this . gameManager
. isGameStart
) { this . gameManager
. isShooting ( true ) ; } else { this . gameStart
. active
= false ; this . game
. active
= true ; this . gameManager
. playAudioEffect ( 'button' ) ; this . gameManager
. gameStart ( ) ; } } _touchMove ( touch
: Touch
, event
: EventTouch
) { if ( ! this . gameManager
. isGameStart
) { return ; } const delta
= touch
. getDelta ( ) ; let pos
= this . playerPlane
. position
; this . playerPlane
. setPosition ( pos
. x
+ 0.01 * this . planeSpeed
* delta
. x
, pos
. y
, pos
. z
- 0.01 * this . planeSpeed
* delta
. y
) ; } _touchEnd ( touch
: Touch
, event
: EventTouch
) { if ( ! this . gameManager
. isGameStart
) { return ; } this . gameManager
. isShooting ( false ) ; }
}
import { _decorator
, Component
, Node
, Collider
, ITriggerEvent
} from 'cc' ;
import { Constant
} from '../framework/Constant' ;
import { GameManager
} from '../framework/GameManager' ;
const { ccclass
, property
} = _decorator
;
const OUTOFBOUNCE = 50 @
ccclass ( 'EnemyPlane' )
export class EnemyPlane extends Component { @property
public createBulletTime
= 0.5 private _enemySpeed
= 0 ; private _needBullet
= false private _gameManager
: GameManager
= null private _currCreateBulletTime
= 0 start ( ) { } onEnable ( ) { const collider
= this . getComponent ( Collider
) ; collider
. on ( 'onTriggerEnter' , this . _onTriggerEnter
, this ) ; } onDisable ( ) { const collider
= this . getComponent ( Collider
) ; collider
. off ( 'onTriggerEnter' , this . _onTriggerEnter
, this ) ; } update ( deltaTime
: number ) { const pos
= this . node
. position
const movePos
= pos
. z
+ this . _enemySpeed
this . node
. setPosition ( pos
. x
, pos
. y
, movePos
) if ( this . _needBullet
) { this . _currCreateBulletTime
+= deltaTime
if ( this . _currCreateBulletTime
> this . createBulletTime
) { this . _gameManager
. createEnemyBullet ( this . node
. position
) this . _currCreateBulletTime
= 0 } } if ( movePos
> OUTOFBOUNCE ) { this . node
. destroy ( ) } } show ( gameManager
: GameManager
, speed
: number , needBullet
: boolean ) { this . _gameManager
= gameManager
this . _enemySpeed
= speed
this . _needBullet
= needBullet
} private _onTriggerEnter ( event
: ITriggerEvent
) { const collisionGroup
= event
. otherCollider
. getGroup ( ) ; if ( collisionGroup
=== Constant
. CollisionType
. SELF_PLANE || collisionGroup
=== Constant
. CollisionType
. SELF_BULLET ) { this . _gameManager
. playAudioEffect ( 'enemy' ) ; this . node
. destroy ( ) ; this . _gameManager
. addScore ( ) ; } }
}
import { _decorator
, Component
, Node
, Collider
, ITriggerEvent
, AudioSource
} from 'cc' ;
import { Constant
} from '../framework/Constant' ;
const { ccclass
, property
} = _decorator
; @
ccclass ( 'SelfPlane' )
export class SelfPlane extends Component { public lifeValue
= 5 ; public isDie
= false ; private _currLife
= 0 ; private _audioEffect
: AudioSource
= null ; start ( ) { this . _audioEffect
= this . getComponent ( AudioSource
) ; } onEnable ( ) { const collider
= this . getComponent ( Collider
) ; collider
. on ( 'onTriggerEnter' , this . _onTriggerEnter
, this ) ; } onDisable ( ) { const collider
= this . getComponent ( Collider
) ; collider
. off ( 'onTriggerEnter' , this . _onTriggerEnter
, this ) ; } public init ( ) { this . _currLife
= this . lifeValue
; this . isDie
= false ; } private _onTriggerEnter ( event
: ITriggerEvent
) { const collisionGroup
= event
. otherCollider
. getGroup ( ) ; if ( collisionGroup
=== Constant
. CollisionType
. ENEMY_PLANE || collisionGroup
=== Constant
. CollisionType
. ENEMY_BULLET ) { this . _currLife
-- ; if ( this . _currLife
<= 0 ) { this . isDie
= true ; this . _audioEffect
. play ( ) ; console . log ( 'self plane is die' ) ; } } } }
總結
以上是生活随笔 為你收集整理的cocos做飞机大战笔记【添加游戏音效】 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。