[Add]: Sounds
This commit is contained in:
parent
f17cad716a
commit
6e5912f50e
@ -73,6 +73,9 @@
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
"__id__": 15
|
"__id__": 15
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__id__": 16
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"_prefab": null,
|
"_prefab": null,
|
||||||
@ -156,7 +159,7 @@
|
|||||||
"__type__": "cc.Vec3",
|
"__type__": "cc.Vec3",
|
||||||
"x": 0,
|
"x": 0,
|
||||||
"y": 0,
|
"y": 0,
|
||||||
"z": 539.5338265577053
|
"z": 779.4228634059948
|
||||||
},
|
},
|
||||||
"_scale": {
|
"_scale": {
|
||||||
"__type__": "cc.Vec3",
|
"__type__": "cc.Vec3",
|
||||||
@ -629,5 +632,15 @@
|
|||||||
"__uuid__": "9c3d7d56-f77a-4e1f-a64c-865e00c80cff"
|
"__uuid__": "9c3d7d56-f77a-4e1f-a64c-865e00c80cff"
|
||||||
},
|
},
|
||||||
"_id": "0erRnoKLBJhIFfRGuNB7Qo"
|
"_id": "0erRnoKLBJhIFfRGuNB7Qo"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"__type__": "1e5d46Ai89Cn4miJXf2Ka71",
|
||||||
|
"_name": "",
|
||||||
|
"_objFlags": 0,
|
||||||
|
"node": {
|
||||||
|
"__id__": 2
|
||||||
|
},
|
||||||
|
"_enabled": true,
|
||||||
|
"_id": "8akbMFRTxMJoUZES0nQNaf"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
@ -9,6 +9,7 @@
|
|||||||
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||||
|
|
||||||
import GameData from "./GameData";
|
import GameData from "./GameData";
|
||||||
|
import SoundManager from "./SoundsManager";
|
||||||
|
|
||||||
const {ccclass, property} = cc._decorator;
|
const {ccclass, property} = cc._decorator;
|
||||||
|
|
||||||
@ -47,6 +48,7 @@ export default class NewClass extends cc.Component {
|
|||||||
var bullet = cc.instantiate(this.yellowBullet);
|
var bullet = cc.instantiate(this.yellowBullet);
|
||||||
bullet.setPosition(this.node.position.x, this.node.position.y - 60);
|
bullet.setPosition(this.node.position.x, this.node.position.y - 60);
|
||||||
this.node.parent.addChild(bullet);
|
this.node.parent.addChild(bullet);
|
||||||
|
SoundManager.instance.playEffect('gun2');
|
||||||
}
|
}
|
||||||
|
|
||||||
// LIFE-CYCLE CALLBACKS:
|
// LIFE-CYCLE CALLBACKS:
|
||||||
@ -64,6 +66,7 @@ export default class NewClass extends cc.Component {
|
|||||||
if(otherCollider.name === 'greenbullet<PolygonCollider>') {
|
if(otherCollider.name === 'greenbullet<PolygonCollider>') {
|
||||||
this.enemyLife--;
|
this.enemyLife--;
|
||||||
if(this.enemyLife <= 0 && this.playAnimation) {
|
if(this.enemyLife <= 0 && this.playAnimation) {
|
||||||
|
SoundManager.instance.playEffect('explosion');
|
||||||
this.node.getComponent(cc.Collider).enabled = false;
|
this.node.getComponent(cc.Collider).enabled = false;
|
||||||
this.node.stopAllActions();
|
this.node.stopAllActions();
|
||||||
this.playAnimation = false;
|
this.playAnimation = false;
|
||||||
@ -72,6 +75,7 @@ export default class NewClass extends cc.Component {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if(otherCollider.name === 'player<PolygonCollider>') {
|
if(otherCollider.name === 'player<PolygonCollider>') {
|
||||||
|
SoundManager.instance.stopMusic();
|
||||||
this.node.destroy();
|
this.node.destroy();
|
||||||
cc.director.loadScene('Menu');
|
cc.director.loadScene('Menu');
|
||||||
}
|
}
|
||||||
@ -97,6 +101,7 @@ export default class NewClass extends cc.Component {
|
|||||||
|
|
||||||
update (dt) {
|
update (dt) {
|
||||||
if(this.node.position.y <= -(this.node.parent.getContentSize().height)) {
|
if(this.node.position.y <= -(this.node.parent.getContentSize().height)) {
|
||||||
|
SoundManager.instance.stopMusic();
|
||||||
this.node.destroy();
|
this.node.destroy();
|
||||||
cc.director.loadScene('Menu');
|
cc.director.loadScene('Menu');
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,6 +9,7 @@
|
|||||||
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||||
|
|
||||||
import GameData from "./GameData";
|
import GameData from "./GameData";
|
||||||
|
import SoundManager from "./SoundsManager";
|
||||||
|
|
||||||
const {ccclass, property} = cc._decorator;
|
const {ccclass, property} = cc._decorator;
|
||||||
|
|
||||||
@ -66,6 +67,8 @@ export default class NewClass extends cc.Component {
|
|||||||
onLoad () {
|
onLoad () {
|
||||||
var manager = cc.director.getCollisionManager();
|
var manager = cc.director.getCollisionManager();
|
||||||
manager.enabled = true;
|
manager.enabled = true;
|
||||||
|
|
||||||
|
SoundManager.instance.playMusic('Dududum', true);
|
||||||
}
|
}
|
||||||
|
|
||||||
start () {
|
start () {
|
||||||
|
|||||||
@ -8,6 +8,8 @@
|
|||||||
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
||||||
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||||
|
|
||||||
|
import SoundManager from "./SoundsManager";
|
||||||
|
|
||||||
const {ccclass, property} = cc._decorator;
|
const {ccclass, property} = cc._decorator;
|
||||||
|
|
||||||
@ccclass
|
@ccclass
|
||||||
@ -29,9 +31,11 @@ export default class NewClass extends cc.Component {
|
|||||||
onCollisionEnter(otherCollider, selfCollider) {
|
onCollisionEnter(otherCollider, selfCollider) {
|
||||||
if(otherCollider.name === 'Bullet<PolygonCollider>') {
|
if(otherCollider.name === 'Bullet<PolygonCollider>') {
|
||||||
this.playerLifes--;
|
this.playerLifes--;
|
||||||
|
SoundManager.instance.playEffect('healthaddon');
|
||||||
this.setLifes(this.playerLifes);
|
this.setLifes(this.playerLifes);
|
||||||
otherCollider.node.destroy();
|
otherCollider.node.destroy();
|
||||||
if(this.playerLifes <= 0) {
|
if(this.playerLifes <= 0) {
|
||||||
|
SoundManager.instance.stopMusic();
|
||||||
this.node.destroy();
|
this.node.destroy();
|
||||||
cc.director.loadScene('Menu');
|
cc.director.loadScene('Menu');
|
||||||
}
|
}
|
||||||
@ -39,10 +43,12 @@ export default class NewClass extends cc.Component {
|
|||||||
else if(otherCollider.name === 'health<PolygonCollider>') {
|
else if(otherCollider.name === 'health<PolygonCollider>') {
|
||||||
this.playerLifes = 8;
|
this.playerLifes = 8;
|
||||||
this.setLifes(this.playerLifes);
|
this.setLifes(this.playerLifes);
|
||||||
|
SoundManager.instance.playEffect('gunaddon');
|
||||||
otherCollider.node.destroy();
|
otherCollider.node.destroy();
|
||||||
}
|
}
|
||||||
else if(otherCollider.name === 'rocket<PolygonCollider>') {
|
else if(otherCollider.name === 'rocket<PolygonCollider>') {
|
||||||
this.shootingInterval = Math.min(this.shootingInterval + 1, 5);
|
this.shootingInterval = Math.min(this.shootingInterval + 1, 5);
|
||||||
|
SoundManager.instance.playEffect('gunaddon');
|
||||||
otherCollider.node.destroy();
|
otherCollider.node.destroy();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -54,6 +60,7 @@ export default class NewClass extends cc.Component {
|
|||||||
}
|
}
|
||||||
|
|
||||||
shootBullets() {
|
shootBullets() {
|
||||||
|
SoundManager.instance.playEffect('gun');
|
||||||
switch (this.shootingInterval) {
|
switch (this.shootingInterval) {
|
||||||
case 1:
|
case 1:
|
||||||
this.spawnBullet(0, 40);
|
this.spawnBullet(0, 40);
|
||||||
|
|||||||
20
assets/Scripts/Root.ts
Normal file
20
assets/Scripts/Root.ts
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// Learn TypeScript:
|
||||||
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
|
||||||
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
|
||||||
|
// Learn Attribute:
|
||||||
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
||||||
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
|
||||||
|
// Learn life-cycle callbacks:
|
||||||
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
||||||
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||||
|
|
||||||
|
const {ccclass, property} = cc._decorator;
|
||||||
|
import SoundManager from "./SoundsManager";
|
||||||
|
|
||||||
|
@ccclass
|
||||||
|
export default class NewClass extends cc.Component {
|
||||||
|
|
||||||
|
onLoad() {
|
||||||
|
SoundManager.instance.preloadSounds();
|
||||||
|
}
|
||||||
|
}
|
||||||
9
assets/Scripts/Root.ts.meta
Normal file
9
assets/Scripts/Root.ts.meta
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.5",
|
||||||
|
"uuid": "1e5d4e80-8bcf-429f-89a2-2577f629aef5",
|
||||||
|
"isPlugin": false,
|
||||||
|
"loadPluginInWeb": true,
|
||||||
|
"loadPluginInNative": true,
|
||||||
|
"loadPluginInEditor": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
||||||
75
assets/Scripts/SoundsManager.ts
Normal file
75
assets/Scripts/SoundsManager.ts
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
// Learn TypeScript:
|
||||||
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/typescript.html
|
||||||
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/typescript.html
|
||||||
|
// Learn Attribute:
|
||||||
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/reference/attributes.html
|
||||||
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/reference/attributes.html
|
||||||
|
// Learn life-cycle callbacks:
|
||||||
|
// - [Chinese] https://docs.cocos.com/creator/manual/zh/scripting/life-cycle-callbacks.html
|
||||||
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||||
|
|
||||||
|
const {ccclass, property} = cc._decorator;
|
||||||
|
|
||||||
|
@ccclass
|
||||||
|
export default class SoundManager extends cc.Component {
|
||||||
|
private static _instance: SoundManager = null;
|
||||||
|
|
||||||
|
private sounds: { [key: string]: cc.AudioClip } = {};
|
||||||
|
private musicId: number = -1;
|
||||||
|
|
||||||
|
public static get instance(): SoundManager {
|
||||||
|
if (!this._instance) {
|
||||||
|
const node = new cc.Node("SoundManager");
|
||||||
|
cc.game.addPersistRootNode(node); // stays across scenes
|
||||||
|
this._instance = node.addComponent(SoundManager);
|
||||||
|
}
|
||||||
|
return this._instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Preload all your sounds at startup
|
||||||
|
preloadSounds() {
|
||||||
|
cc.loader.loadResDir("sounds", cc.AudioClip, (err, clips, urls) => {
|
||||||
|
if (err) {
|
||||||
|
cc.error("Error loading sounds:", err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = 0; i < clips.length; i++) {
|
||||||
|
const key = urls[i].split("/").pop(); // file name without path
|
||||||
|
this.sounds[key] = clips[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
cc.audioEngine.setMusicVolume(0.1);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Play short effects (clicks, shots, explosions)
|
||||||
|
playEffect(name: string, loop: boolean = false) {
|
||||||
|
const clip = this.sounds[name];
|
||||||
|
if (clip) {
|
||||||
|
cc.audioEngine.setEffectsVolume(name === 'explosion' ? 0.1 : 0.2);
|
||||||
|
cc.audioEngine.playEffect(clip, loop);
|
||||||
|
} else {
|
||||||
|
cc.warn(`Sound '${name}' not found. Make sure it's preloaded.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// ✅ Play/Stop background music
|
||||||
|
playMusic(name: string, loop: boolean = true) {
|
||||||
|
const clip = this.sounds[name];
|
||||||
|
if (clip) {
|
||||||
|
if (this.musicId !== -1) {
|
||||||
|
cc.audioEngine.stop(this.musicId);
|
||||||
|
}
|
||||||
|
this.musicId = cc.audioEngine.playMusic(clip, loop);
|
||||||
|
} else {
|
||||||
|
cc.warn(`Music '${name}' not found. Make sure it's preloaded.`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
stopMusic() {
|
||||||
|
cc.audioEngine.stopMusic();
|
||||||
|
this.musicId = -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
9
assets/Scripts/SoundsManager.ts.meta
Normal file
9
assets/Scripts/SoundsManager.ts.meta
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.5",
|
||||||
|
"uuid": "37d707d8-0ba1-44a6-9ba3-398124444f75",
|
||||||
|
"isPlugin": false,
|
||||||
|
"loadPluginInWeb": true,
|
||||||
|
"loadPluginInNative": true,
|
||||||
|
"loadPluginInEditor": false,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
||||||
@ -9,21 +9,34 @@
|
|||||||
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
|
||||||
|
|
||||||
import GameData from "./GameData";
|
import GameData from "./GameData";
|
||||||
|
import SoundManager from "./SoundsManager";
|
||||||
|
|
||||||
const {ccclass, property} = cc._decorator;
|
const {ccclass, property} = cc._decorator;
|
||||||
|
|
||||||
@ccclass
|
@ccclass
|
||||||
export default class NewClass extends cc.Component {
|
export default class MenuScreen extends cc.Component {
|
||||||
|
|
||||||
|
onLoad() {
|
||||||
|
const scene = cc.director.getScene().name;
|
||||||
|
if(scene === 'Menu') {
|
||||||
|
this.scheduleOnce(() => {
|
||||||
|
SoundManager.instance.playMusic('Judgement', true);
|
||||||
|
}, 0.1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
loadLevelsScene() {
|
loadLevelsScene() {
|
||||||
|
SoundManager.instance.playEffect('click');
|
||||||
cc.director.preloadScene('Level', () => {
|
cc.director.preloadScene('Level', () => {
|
||||||
cc.director.loadScene('Level');
|
cc.director.loadScene('Level');
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
loadGameScene(event, customData) {
|
loadGameScene(event, customData) {
|
||||||
|
SoundManager.instance.playEffect('click');
|
||||||
const selectedLevel = Number(customData);
|
const selectedLevel = Number(customData);
|
||||||
GameData.instance.selectedLevel = selectedLevel;
|
GameData.instance.selectedLevel = selectedLevel;
|
||||||
|
SoundManager.instance.stopMusic();
|
||||||
cc.director.preloadScene('Game', () => {
|
cc.director.preloadScene('Game', () => {
|
||||||
cc.director.loadScene('Game');
|
cc.director.loadScene('Game');
|
||||||
});
|
});
|
||||||
|
|||||||
7
assets/resources.meta
Normal file
7
assets/resources.meta
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
{
|
||||||
|
"ver": "1.0.1",
|
||||||
|
"uuid": "1c8ac613-25f0-4b91-9b77-dd606628cb4f",
|
||||||
|
"isSubpackage": false,
|
||||||
|
"subpackageName": "",
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
||||||
BIN
assets/resources/sounds/gunaddon.wav
Normal file
BIN
assets/resources/sounds/gunaddon.wav
Normal file
Binary file not shown.
6
assets/resources/sounds/gunaddon.wav.meta
Normal file
6
assets/resources/sounds/gunaddon.wav.meta
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"ver": "2.0.0",
|
||||||
|
"uuid": "fd59f682-d897-45c3-9371-9049b65e8cd8",
|
||||||
|
"downloadMode": 0,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
||||||
BIN
assets/resources/sounds/healthaddon.wav
Normal file
BIN
assets/resources/sounds/healthaddon.wav
Normal file
Binary file not shown.
6
assets/resources/sounds/healthaddon.wav.meta
Normal file
6
assets/resources/sounds/healthaddon.wav.meta
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"ver": "2.0.0",
|
||||||
|
"uuid": "93bb95b4-42bb-4364-95e0-fe263f6ce2bd",
|
||||||
|
"downloadMode": 0,
|
||||||
|
"subMetas": {}
|
||||||
|
}
|
||||||
@ -72,5 +72,6 @@
|
|||||||
"width": 960
|
"width": 960
|
||||||
},
|
},
|
||||||
"use-customize-simulator": true,
|
"use-customize-simulator": true,
|
||||||
"use-project-simulator-setting": false
|
"use-project-simulator-setting": false,
|
||||||
|
"start-scene": "current"
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user