[Add]: Sounds

This commit is contained in:
Mohammad_Hasan 2025-11-03 15:27:17 +04:00
parent f17cad716a
commit 6e5912f50e
28 changed files with 177 additions and 3 deletions

View File

@ -73,6 +73,9 @@
},
{
"__id__": 15
},
{
"__id__": 16
}
],
"_prefab": null,
@ -156,7 +159,7 @@
"__type__": "cc.Vec3",
"x": 0,
"y": 0,
"z": 539.5338265577053
"z": 779.4228634059948
},
"_scale": {
"__type__": "cc.Vec3",
@ -629,5 +632,15 @@
"__uuid__": "9c3d7d56-f77a-4e1f-a64c-865e00c80cff"
},
"_id": "0erRnoKLBJhIFfRGuNB7Qo"
},
{
"__type__": "1e5d46Ai89Cn4miJXf2Ka71",
"_name": "",
"_objFlags": 0,
"node": {
"__id__": 2
},
"_enabled": true,
"_id": "8akbMFRTxMJoUZES0nQNaf"
}
]

View File

@ -9,6 +9,7 @@
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
import GameData from "./GameData";
import SoundManager from "./SoundsManager";
const {ccclass, property} = cc._decorator;
@ -47,6 +48,7 @@ export default class NewClass extends cc.Component {
var bullet = cc.instantiate(this.yellowBullet);
bullet.setPosition(this.node.position.x, this.node.position.y - 60);
this.node.parent.addChild(bullet);
SoundManager.instance.playEffect('gun2');
}
// LIFE-CYCLE CALLBACKS:
@ -64,6 +66,7 @@ export default class NewClass extends cc.Component {
if(otherCollider.name === 'greenbullet<PolygonCollider>') {
this.enemyLife--;
if(this.enemyLife <= 0 && this.playAnimation) {
SoundManager.instance.playEffect('explosion');
this.node.getComponent(cc.Collider).enabled = false;
this.node.stopAllActions();
this.playAnimation = false;
@ -72,6 +75,7 @@ export default class NewClass extends cc.Component {
}
}
if(otherCollider.name === 'player<PolygonCollider>') {
SoundManager.instance.stopMusic();
this.node.destroy();
cc.director.loadScene('Menu');
}
@ -97,6 +101,7 @@ export default class NewClass extends cc.Component {
update (dt) {
if(this.node.position.y <= -(this.node.parent.getContentSize().height)) {
SoundManager.instance.stopMusic();
this.node.destroy();
cc.director.loadScene('Menu');
}

View File

@ -9,6 +9,7 @@
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
import GameData from "./GameData";
import SoundManager from "./SoundsManager";
const {ccclass, property} = cc._decorator;
@ -66,6 +67,8 @@ export default class NewClass extends cc.Component {
onLoad () {
var manager = cc.director.getCollisionManager();
manager.enabled = true;
SoundManager.instance.playMusic('Dududum', true);
}
start () {

View File

@ -8,6 +8,8 @@
// - [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
import SoundManager from "./SoundsManager";
const {ccclass, property} = cc._decorator;
@ccclass
@ -29,9 +31,11 @@ export default class NewClass extends cc.Component {
onCollisionEnter(otherCollider, selfCollider) {
if(otherCollider.name === 'Bullet<PolygonCollider>') {
this.playerLifes--;
SoundManager.instance.playEffect('healthaddon');
this.setLifes(this.playerLifes);
otherCollider.node.destroy();
if(this.playerLifes <= 0) {
SoundManager.instance.stopMusic();
this.node.destroy();
cc.director.loadScene('Menu');
}
@ -39,10 +43,12 @@ export default class NewClass extends cc.Component {
else if(otherCollider.name === 'health<PolygonCollider>') {
this.playerLifes = 8;
this.setLifes(this.playerLifes);
SoundManager.instance.playEffect('gunaddon');
otherCollider.node.destroy();
}
else if(otherCollider.name === 'rocket<PolygonCollider>') {
this.shootingInterval = Math.min(this.shootingInterval + 1, 5);
SoundManager.instance.playEffect('gunaddon');
otherCollider.node.destroy();
}
}
@ -54,6 +60,7 @@ export default class NewClass extends cc.Component {
}
shootBullets() {
SoundManager.instance.playEffect('gun');
switch (this.shootingInterval) {
case 1:
this.spawnBullet(0, 40);

20
assets/Scripts/Root.ts Normal file
View 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();
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "1e5d4e80-8bcf-429f-89a2-2577f629aef5",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View 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;
}
}

View File

@ -0,0 +1,9 @@
{
"ver": "1.0.5",
"uuid": "37d707d8-0ba1-44a6-9ba3-398124444f75",
"isPlugin": false,
"loadPluginInWeb": true,
"loadPluginInNative": true,
"loadPluginInEditor": false,
"subMetas": {}
}

View File

@ -9,21 +9,34 @@
// - [English] http://www.cocos2d-x.org/docs/creator/manual/en/scripting/life-cycle-callbacks.html
import GameData from "./GameData";
import SoundManager from "./SoundsManager";
const {ccclass, property} = cc._decorator;
@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() {
SoundManager.instance.playEffect('click');
cc.director.preloadScene('Level', () => {
cc.director.loadScene('Level');
});
}
loadGameScene(event, customData) {
SoundManager.instance.playEffect('click');
const selectedLevel = Number(customData);
GameData.instance.selectedLevel = selectedLevel;
SoundManager.instance.stopMusic();
cc.director.preloadScene('Game', () => {
cc.director.loadScene('Game');
});

7
assets/resources.meta Normal file
View File

@ -0,0 +1,7 @@
{
"ver": "1.0.1",
"uuid": "1c8ac613-25f0-4b91-9b77-dd606628cb4f",
"isSubpackage": false,
"subpackageName": "",
"subMetas": {}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
{
"ver": "2.0.0",
"uuid": "fd59f682-d897-45c3-9371-9049b65e8cd8",
"downloadMode": 0,
"subMetas": {}
}

Binary file not shown.

View File

@ -0,0 +1,6 @@
{
"ver": "2.0.0",
"uuid": "93bb95b4-42bb-4364-95e0-fe263f6ce2bd",
"downloadMode": 0,
"subMetas": {}
}

View File

@ -72,5 +72,6 @@
"width": 960
},
"use-customize-simulator": true,
"use-project-simulator-setting": false
"use-project-simulator-setting": false,
"start-scene": "current"
}