scheduleOnce

 

//程序开始后延时2秒才开始addSprite函数
scheduleOnce(schedule_selector(Issue1305::addSprite), 2);
转到定义:
void CCNode::scheduleOnce(SEL_SCHEDULE selector, float delay)
{
    this->schedule(selector, 0.0f, 0, delay);
}
void Issue1305::addSprite(float dt)
{
    m_pSpriteTmp->setPosition(ccp(250,250));
    addChild(m_pSpriteTmp);
}

 

// SkillManager.ts import { _decorator, Component, Node, Prefab, Vec3, v3, instantiate, Tween, tween, find } from ‘cc’; const { ccclass, property } = _decorator; import { GameManager } from ‘./GameManager’; // 导入GameManager import { MonsterController } from ‘./MonsterController’; // 导入MonsterController @ccclass(‘SkillManager’) export class SkillManager extends Component { @property({type: [Prefab]}) private skillsPrefabs: Prefab[] = []; // 技能特效节点预制体数组 // 添加对GameManager的引用 @property({type: Node}) private gameManagerNode: Node; // 挂载GameManager的节点 private gameManager: GameManager | null = null; // 存储当前激活的技能实例 private activeWanJianJueSkills: Node[] = []; //存储万剑诀实例 private activeDuJieShuSkills: Node[] = []; // 存储渡劫术实例 // 英雄节点引用 private heroNode: Node | null = null; // 万剑诀生成计时器 private wanJianJueSpawnTimer: number = 0; private wanJianJueSpawnInterval: number = 0.2; // 0.2秒生成间隔 private wanJianJueSpawnCount: number = 0; // 已生成数量 private wanJianJueTotalCount: number = 10; // 总生成数量 // 万剑诀攻击范围(像素) private readonly WAN_JIAN_JUE_ATTACK_RANGE: number = 30; protected onLoad(): void { // 获取GameManager组件 if (this.gameManagerNode) { this.gameManager = this.gameManagerNode.getComponent(GameManager); // 获取英雄节点 this.heroNode = this.gameManager.HeroNode; } } // 每帧更新 update(deltaTime: number) { // 更新万剑诀生成计时器 if (this.wanJianJueSpawnCount > 0 && this.wanJianJueSpawnCount < this.wanJianJueTotalCount) { this.wanJianJueSpawnTimer += deltaTime; // 检查是否达到生成间隔 if (this.wanJianJueSpawnTimer >= this.wanJianJueSpawnInterval) { this.wanJianJueSpawnTimer = 0; this.createWanJianJueInstance(); } } } //按键启动万剑诀 public buttenWanJianJue(){ // 获取最近的怪兽 let nearestMonster = this.getNearestMonsterForSkill(); if (nearestMonster) { // 重置生成参数 this.wanJianJueSpawnCount = 0; this.wanJianJueSpawnTimer = 0; // 立即生成第一个实例 this.createWanJianJueInstance(); } else { console.warn(“没有找到有效的怪兽作为万剑诀落点”); } } /** * 获取最近的怪兽节点 * @returns 最近的怪兽节点或null / private getNearestMonsterForSkill(): Node | null { if (!this.gameManager) { console.warn(“GameManager未设置”); return null; } return this.gameManager.getNearestAliveMonster(); } /* * 创建单个万剑诀实例 / private createWanJianJueInstance(): void { // 1. 找到万剑诀预制体 const wanJianJuePrefab = this.skillsPrefabs.find(prefab => prefab.name === “WanJianJue”); if (!wanJianJuePrefab) { console.error(“万剑诀预制体未找到!”); return; } // 确保英雄节点有效 if (!this.heroNode || !this.heroNode.isValid) { console.warn(“英雄节点无效,无法创建万剑诀实例”); return; } // 创建实例 const instance = instantiate(wanJianJuePrefab); // 获取英雄位置 const heroPos = this.heroNode.position.clone(); // 位置生成参数 const minX = heroPos.x - 350 + 4700.6; const maxX = heroPos.x + 350 + 4700.6; const minY = -350 - 1850.6; const maxY = -50 -185*0.6; const minDistance = 80; // 距离英雄的最小距离 let position: Vec3; let attempts = 0; const maxAttempts = 10; // 最大尝试次数 // 尝试找到一个满足条件的位置 do { attempts++; // 在指定范围内生成随机位置 const randomX = Math.random() * (maxX - minX) + minX; const randomY = Math.random() * (maxY - minY) + minY; position = v3(randomX, randomY, heroPos.z); // 检查是否满足距离要求 const distanceToHero = Vec3.distance(position, heroPos); // 如果满足距离要求或达到最大尝试次数 if (distanceToHero >= minDistance || attempts >= maxAttempts) { break; } } while (true); // 设置位置 instance.setPosition(position); // 添加到场景 this.heroNode.parent.addChild(instance); // 添加到激活技能列表 this.activeWanJianJueSkills.push(instance); // 增加生成计数 this.wanJianJueSpawnCount++; // 0.5秒后检测伤害(基于该实例位置) this.scheduleOnce(() => { this.detectWanJianJueDamageForInstance(position); // 0.3秒后删除该实例(总共0.8秒) this.scheduleOnce(() => { if (instance.isValid) { instance.destroy(); // 从激活列表中移除 const index = this.activeWanJianJueSkills.indexOf(instance); if (index !== -1) { this.activeWanJianJueSkills.splice(index, 1); } } }, 0.25); }, 0.5); } /** * 为单个万剑诀实例检测伤害 * @param position 实例位置 / private detectWanJianJueDamageForInstance(position: Vec3): void { if (!this.gameManager) { console.warn(“GameManager未设置,无法检测技能伤害”); return; } // 获取所有怪物 const monsters = this.gameManager.getAllMonsters(); for (const monster of monsters) { if (!monster || !monster.isValid) continue; const controller = monster.getComponent(MonsterController); if (!controller || controller.isDie) continue; // 计算距离(基于怪兽尺寸半径) const distance = Vec3.distance(monster.position, position); const monsterRadius = controller.MonsterSize; // 获取怪兽尺寸半径 // 计算总攻击范围 = 怪物半径 + 30像素攻击范围 const totalRange = monsterRadius + this.WAN_JIAN_JUE_ATTACK_RANGE; // 如果距离小于等于怪兽尺寸半径,造成伤害 if (distance <= totalRange) { // 对怪物造成伤害 controller.takeDamage(60); } } } /* * 清空所有激活的技能实例 / private clearActiveSkills(): void { for (const skill of this.activeWanJianJueSkills) { if (skill.isValid) { skill.destroy(); } } this.activeWanJianJueSkills = []; } /* * 启动横扫千军技能 / public buttenHengSaoQianJun() { // 在英雄位置创建横扫千军特效 this.startHengSaoQianJun(); } /* * 创建横扫千军技能 / private startHengSaoQianJun(): void { // 1. 找到横扫千军预制体 const hengSaoQianJunPrefab = this.skillsPrefabs.find(prefab => prefab.name === “HengSaoQianJun1”); if (!hengSaoQianJunPrefab) { console.error(“横扫千军预制体未找到!”); return; } // 确保英雄节点有效 if (!this.heroNode || !this.heroNode.isValid) { console.warn(“英雄节点无效,无法创建横扫千军特效”); return; } // 创建实例 const instance = instantiate(hengSaoQianJunPrefab); // 获取英雄朝向(Scale.x为正表示朝左,为负表示朝右) const heroScaleX = this.heroNode.scale.x; const isHeroFacingLeft = heroScaleX > 0; // 设置偏移量 - 根据英雄朝向调整X方向 const offsetX = isHeroFacingLeft ? 364 : -364; const offset = new Vec3(offsetX, 25, 0); // 计算最终位置 const heroPos = this.heroNode.position.clone(); const position = new Vec3( heroPos.x + offset.x, heroPos.y + offset.y, heroPos.z + offset.z ); instance.setPosition(position); // 调整技能朝向 - 与英雄朝向一致 const currentScale = instance.scale; if (isHeroFacingLeft) { // 英雄朝左,技能朝左(Scale.x为正) instance.setScale(new Vec3(Math.abs(currentScale.x), currentScale.y, currentScale.z)); } else { // 英雄朝右,技能朝右(Scale.x为负) instance.setScale(new Vec3(-Math.abs(currentScale.x), currentScale.y, currentScale.z)); } // 添加到场景 this.node.addChild(instance); // 添加到激活技能列表 this.activeWanJianJueSkills.push(instance); // 添加伤害检测 this.scheduleOnce(() => { this.detectHengSaoDamage(position); }, 0.7); // 0.7秒后检测伤害 // 1.7秒后删除技能实例 this.scheduleOnce(() => { if (instance.isValid) { instance.destroy(); // 从激活列表中移除 const index = this.activeWanJianJueSkills.indexOf(instance); if (index !== -1) { this.activeWanJianJueSkills.splice(index, 1); } } }, 1.6667); } /* * 检测横扫千军伤害 * @param position 技能位置 / private detectHengSaoDamage(position: Vec3): void { if (!this.gameManager) { console.warn(“GameManager未设置,无法检测技能伤害”); return; } // 获取英雄朝向(Scale.x为正表示朝左,为负表示朝右) const heroScaleX = this.heroNode.scale.x; const isHeroFacingLeft = heroScaleX > 0; // 获取英雄位置 const heroPos = this.heroNode.position.clone(); // 定义伤害区域参数 const damageLength = 400; // 伤害区域长度(像素) const damageWidth = 200; // 伤害区域宽度(像素) const damageAmount = 80; // 伤害值80点 // 计算伤害区域边界 let minX: number, maxX: number; let minY: number, maxY: number; if (isHeroFacingLeft) { // 英雄朝左,伤害区域向左延伸 minX = heroPos.x - damageLength; maxX = heroPos.x; } else { // 英雄朝右,伤害区域向右延伸 minX = heroPos.x; maxX = heroPos.x + damageLength; } // Y轴范围(以英雄位置为中心) minY = heroPos.y - damageWidth / 2; maxY = heroPos.y + damageWidth / 2; // 获取所有怪物 const monsters = this.gameManager.getAllMonsters(); for (const monster of monsters) { if (!monster || !monster.isValid) continue; const controller = monster.getComponent(MonsterController); if (!controller || controller.isDie) continue; // 获取怪物位置 const monsterPos = monster.position; // 检查怪物是否在伤害区域内 const inXRange = monsterPos.x >= minX && monsterPos.x <= maxX; const inYRange = monsterPos.y >= minY && monsterPos.y <= maxY; if (inXRange && inYRange) { // 对怪物造成伤害 controller.takeDamage(damageAmount); } } } // 按键启动渡劫术 public buttonDuJieShu() { // 获取所有存活的怪兽 const allMonsters = this.getAllAliveMonsters(); if (allMonsters.length > 0) { this.startDuJieShu(allMonsters); } else { console.warn(“没有存活的怪兽作为渡劫术目标”); } } /* * 获取所有存活的怪兽 * @returns 存活怪兽节点数组 / private getAllAliveMonsters(): Node[] { if (!this.gameManager) { console.warn(“GameManager未设置”); return []; } return this.gameManager.getAllMonsters(); } /* * 启动渡劫术技能 * @param targetMonsters 目标怪物节点数组 / private startDuJieShu(targetMonsters: Node[]): void { // 1. 找到渡劫术预制体 const duJieShuPrefab = this.skillsPrefabs.find(prefab => prefab.name === “DuJieShu-1”); if (!duJieShuPrefab) { console.error(“渡劫术预制体未找到!”); return; } // 2. 为每个怪物创建渡劫术实例 for (const monster of targetMonsters) { if (!monster || !monster.isValid) continue; const controller = monster.getComponent(MonsterController); if (!controller || controller.isDie) continue; // 创建渡劫术实例 const instance = instantiate(duJieShuPrefab); // 设置位置为怪物位置 instance.setPosition(monster.position.clone()); // 添加到场景 this.heroNode.parent.addChild(instance); // 添加到激活技能列表 this.activeDuJieShuSkills.push(instance); // 3. 立即对目标怪物造成伤害 this.applyDuJieShuDamage(monster); // 4. 0.4667秒后删除技能实例 this.scheduleOnce(() => { if (instance.isValid) { instance.destroy(); // 从激活列表中移除 const index = this.activeDuJieShuSkills.indexOf(instance); if (index !== -1) { this.activeDuJieShuSkills.splice(index, 1); } } }, 0.4667); } } /* * 应用渡劫术伤害 * @param targetMonster 目标怪物节点 */ private applyDuJieShuDamage(targetMonster: Node): void { if (!targetMonster || !targetMonster.isValid) return; const controller = targetMonster.getComponent(MonsterController); if (!controller || controller.isDie) return; // 直接对怪物造成20点伤害 controller.takeDamage(10); console.log(渡劫术对 ${targetMonster.name} 造成20点伤害!); } // 组件销毁时清理资源 protected onDestroy(): void { this.clearActiveSkills(); } }新加一个功能,横扫千军在计算伤害的同时(此时巨剑砸到地面刚好适合)屏幕抖动一次(和GameManager.ts里的暴击一样)
最新发布
07-22
import { _decorator, Component, Node, director, resources, instantiate, Vec3, math } from 'cc'; import { GameData } from './GameData'; import { A_guagouyidong } from './A_guagouyidong'; import { B_pengzhuangDefenxitong } from './B_pengzhuangDefenxitong'; import { ditu } from './C_ditu'; import { A_HUAHUAweizhi } from './A_HUAHUAweizhi'; import { zizhuandaishangwawa2 } from './zizhuandaishangwawa2'; import { fuhuoUI } from './fuhuoUI'; import { GameOverUI } from './GameOverUI'; const { ccclass, property } = _decorator; @ccclass('AA_GameManager') export class GameManager extends Component { @property(Node) clawNode: Node = null; // 爪子节点 @property(Node) collisionSystemNode: Node = null; // 碰撞系统节点 @property(Node) mapNode: Node = null; // 地图节点 @property(Node) flowerSpawnerNode: Node = null; // 花朵生成器节点 @property(Node) rotationComponentNode: Node = null; // 旋转组件节点 @property(Node) cameraFollowNode: Node = null; // 相机跟随节点 @property(Node) reviveUINode: Node = null; // 复活UI节点 @property(Node) gameOverUINode: Node = null; // 游戏结束UI节点 public isGameActive: boolean = false; private currentLevel: number = 1; onLoad() { this.initializeGame(); } /** * 初始化游戏 */ private initializeGame() { // 重置游戏数据 GameData.reset(); // 获取或初始化组件 if (!this.clawNode) this.clawNode = this.node.getChildByName('Claw'); if (!this.collisionSystemNode) this.collisionSystemNode = this.node.getChildByName('CollisionSystem'); if (!this.mapNode) this.mapNode = this.node.getChildByName('Map'); if (!this.flowerSpawnerNode) this.flowerSpawnerNode = this.node.getChildByName('FlowerSpawner'); if (!this.rotationComponentNode) this.rotationComponentNode = this.node.getChildByName('RotationComponent'); if (!this.cameraFollowNode) this.cameraFollowNode = this.node.getChildByName('CameraFollow'); if (!this.reviveUINode) this.reviveUINode = this.node.getChildByName('ReviveUI'); if (!this.gameOverUINode) this.gameOverUINode = this.node.getChildByName('GameOverUI'); // 初始化游戏状态 this.isGameActive = true; } /** * 开始游戏 */ public startGame() { if (!this.isGameActive) { // 重置所有游戏组件 this.resetGameComponents(); // 初始化UI this.resetUI(); // 设置游戏状态 this.isGameActive = true; this.currentLevel = 1; // 开始游戏循环 this.scheduleOnce(() => { this.enableGameComponents(true); }, 0.1); } } /** * 重置UI状态 */ private resetUI() { if (this.reviveUINode?.isValid) { const reviveUI = this.reviveUINode.getComponent(fuhuoUI); if (reviveUI) reviveUI.hide(); } if (this.gameOverUINode?.isValid) { const gameOverUI = this.gameOverUINode.getComponent(GameOverUI); if (gameOverUI) gameOverUI.hide(); } } /** * 重置游戏 */ public resetGame() { // 1. 停止所有游戏活动 this.isGameActive = false; this.enableGameComponents(false); // 2. 重置游戏数据 GameData.reset(); // 3. 重置所有UI状态 this.resetUI(); // 4. 重置游戏组件 this.resetGameComponents(); // 5. 延迟开始游戏确保完全重置 this.scheduleOnce(() => { this.startGame(); }, 0.3); // 增加延迟时间确保完全重置 } /** * 重置游戏组件 */ private resetGameComponents() { // 重置爪子 if (this.clawNode?.isValid) { const clawMovement = this.clawNode.getComponent(A_guagouyidong); if (clawMovement) { if (clawMovement.resetPosition) { clawMovement.resetPosition(); } else { clawMovement.stopMovement(); clawMovement.isFirstClick = true; } } } // 确保设置碰撞系统的回调 if (this.collisionSystemNode?.isValid) { const collisionSystem = this.collisionSystemNode.getComponent(B_pengzhuangDefenxitong); if (collisionSystem) { collisionSystem.setGameOverCallback(this.gameOver.bind(this)); // 确保UI引用正确 collisionSystem.GameOverUI = this.gameOverUINode?.getComponent(GameOverUI); } } // 重置碰撞系统 if (this.collisionSystemNode?.isValid) { const collisionSystem = this.collisionSystemNode.getComponent(B_pengzhuangDefenxitong); if (collisionSystem) { collisionSystem.stopAllMovements(); } } // 重置地图 if (this.mapNode?.isValid) { const mapComponent = this.mapNode.getComponent(ditu); if (mapComponent) { mapComponent.generateMap(); } } // 重置花朵生成器 if (this.flowerSpawnerNode?.isValid) { const flowerSpawner = this.flowerSpawnerNode.getComponent(A_HUAHUAweizhi); if (flowerSpawner) { flowerSpawner.showFlowers(); } } // 重置旋转组件 if (this.rotationComponentNode?.isValid) { const rotationComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2); if (rotationComponent) { rotationComponent.enabled = true; } } } /** * 启用或禁用游戏组件 * @param enable 是否启用 */ private enableGameComponents(enable: boolean) { // 启用/禁用爪子移动 if (this.clawNode?.isValid) { const clawMovement = this.clawNode.getComponent(A_guagouyidong); if (clawMovement) { if (enable) { clawMovement.resumeMovement(); } else { clawMovement.stopMovement(); } } } // 启用/禁用旋转组件 if (this.rotationComponentNode?.isValid) { const rotationComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2); if (rotationComponent) { rotationComponent.enabled = enable; } } // 启用/禁用碰撞系统 if (this.collisionSystemNode?.isValid) { const collisionSystem = this.collisionSystemNode.getComponent(B_pengzhuangDefenxitong); if (collisionSystem) { // 碰撞系统本身会根据游戏状态处理 } } } /** * 游戏结束 * @param isSuccess 是否成功 */ public gameOver(isSuccess: boolean) { // 确保只处理一次 if (!this.isGameActive) return; this.isGameActive = false; this.enableGameComponents(false); // 延迟显示确保UI更新 this.scheduleOnce(() => { if (this.gameOverUINode?.isValid) { const gameOverUI = this.gameOverUINode.getComponent(GameOverUI); if (gameOverUI) { gameOverUI.show(GameData.getScore()); } } }, 0.1); } /** * 返回主页 */ public goToHome() { this.unscheduleAllCallbacks(); director.loadScene('HomeScene'); } /** * 下一关 */ public nextLevel() { this.currentLevel++; this.resetGame(); } /** * 暂停游戏 */ public pauseGame() { if (this.isGameActive) { this.isGameActive = false; this.enableGameComponents(false); } } /** * 恢复游戏 */ public resumeGame() { if (!this.isGameActive) { this.isGameActive = true; this.enableGameComponents(true); } } }。第一个。import { _decorator, Collider2D, Component, Contact2DType, Label, Node, isValid, Camera, UITransform, Vec3, view} from 'cc'; import { GameOverUI } from './GameOverUI'; import { GameData } from './GameData'; import { Tags } from './Tags'; import { A_guagouyidong } from './A_guagouyidong'; import { fuhuoUI } from './fuhuoUI'; const { ccclass, property } = _decorator; @ccclass('B_pengzhuangDefenxitong') export class B_pengzhuangDefenxitong extends Component { // 使用单个Map替代多个Set private processedColliders: Map<string, Tags> = new Map(); @property(fuhuoUI) fuhuoUI: fuhuoUI = null; @property(GameOverUI) GameOverUI: GameOverUI = null; @property(Node) zizhuandaishangwawa2: Node = null; @property(Camera) mainCamera: Camera = null; @property(Label) chanceLabel: Label = null; // 游戏结束回调函数 private onGameOverCallback: (isSuccess: boolean) => void = null; onLoad() { // 碰撞检测 const collider = this.getComponent(Collider2D); if (collider) { collider.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this); collider.on(Contact2DType.END_CONTACT, this.onEndContact, this); } // 更新机会显示 this.updateChanceUI(); } /** * 设置游戏结束回调 * @param callback 回调函数 */ public setGameOverCallback(callback: (isSuccess: boolean) => void) { this.onGameOverCallback = callback; } onBeginContact(selfCollider: Collider2D, otherCollider: Collider2D) { const colliderId = otherCollider.uuid; // 如果已处理过该碰撞体,直接返回 if (this.processedColliders.has(colliderId)) return; switch (otherCollider.tag) { case Tags.HUAHUA: this.processedColliders.set(colliderId, Tags.HUAHUA); this.addScore(1); // 延迟销毁花朵 this.scheduleOnce(() => { if (isValid(otherCollider.node)) { otherCollider.node.destroy(); } }, 0.001); break; case Tags.WAWA: this.processedColliders.set(colliderId, Tags.WAWA); this.addScore(10); // 统一通过回调处理游戏结束 if (this.onGameOverCallback) { this.onGameOverCallback(true); } else { // 如果没有设置回调,使用本地方法 this.gameOver(true); } break; case Tags.DITU: this.processedColliders.set(colliderId, Tags.DITU); GameData.loseChance(); this.updateChanceUI(); // 检查是否需要显示复活界面 if (GameData.shouldShowRevive()) { this.stopAllMovements(); // 延迟显示复活界面 this.scheduleOnce(() => { if (this.fuhuoUI && isValid(this.fuhuoUI.node)) { this.fuhuoUI.show(); } }, 0.2); } break; } } onEndContact(selfCollider: Collider2D, otherCollider: Collider2D) { const colliderId = otherCollider.uuid; // 只移除地图碰撞记录 if (otherCollider.tag === Tags.DITU && this.processedColliders.has(colliderId)) { this.processedColliders.delete(colliderId); } } updateChanceUI() { if (this.chanceLabel && isValid(this.chanceLabel.node)) { this.chanceLabel.string = GameData.getChances().toString(); } } public stopAllMovements() { // 停止爪子移动 if (this.node?.isValid) { const moveComp = this.node.getComponent(A_guagouyidong); if (moveComp) { moveComp.stopMovement(); } } // 停止旋转组件 if (this.zizhuandaishangwawa2?.isValid) { const component = this.zizhuandaishangwawa2.getComponent("zizhuandaishangwawa2"); if (component) { component.enabled = false; } } } public gameOver(isSuccess: boolean) { // 调用停止移动的方法 if (this.node?.isValid) { const moveComp = this.node.getComponent(A_guagouyidong); if (moveComp) { moveComp.stopMovement(); } } // 停止旋转组件 if (this.zizhuandaishangwawa2?.isValid) { const component = this.zizhuandaishangwawa2.getComponent("zizhuandaishangwawa2"); if (component) { component.enabled = false; } } // 显示游戏结束界面 if (this.GameOverUI?.isValid) { const cameraWorldPos = this.mainCamera.node.worldPosition; this.GameOverUI.node.setWorldPosition(cameraWorldPos); this.GameOverUI.show(GameData.getScore()); } } private addScore(count: number = 1) { GameData.addScore(count); } }第二个。import { _decorator, Button, Component, misc, Node } from 'cc'; import { GameData } from './GameData'; import { GameManager } from './AA_GameManager'; import { zizhuandaishangwawa2 } from './zizhuandaishangwawa2'; const { ccclass, property } = _decorator; @ccclass('chongwananniu') export class chongwananniu extends Component { @property(GameManager) gameManager: GameManager = null; @property(Node) rotationComponentNode: Node = null; // 旋转组件节点(娃娃出现系统) private isEventBound = false; private currentWawaData: {x: number, y: number, angle: number}[] = []; // 保存娃娃位置和角度数据 onLoad() { this.node.on(Button.EventType.CLICK, this.onReplayClick, this); this.isEventBound = true; } private onReplayClick() { // 简单直接调用游戏管理器重置 if (this.gameManager && this.gameManager.node?.isValid) { this.gameManager.resetGame(); } } private saveCurrentWawas() { if (this.rotationComponentNode) { const wawaComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2); if (wawaComponent) { this.currentWawaData = []; for (const child of wawaComponent.children) { if (child && child.isValid) { const pos = child.position; this.currentWawaData.push({ x: pos.x, y: pos.y, angle: child.angle }); } } } } } private restoreWawas() { if (this.rotationComponentNode && this.currentWawaData.length > 0) { const wawaComponent = this.rotationComponentNode.getComponent(zizhuandaishangwawa2); if (wawaComponent) { // 确保有足够的娃娃节点 if (wawaComponent.children.length < this.currentWawaData.length) { console.warn("Not enough wawa nodes to restore"); return; } // 恢复位置和角度 for (let i = 0; i < this.currentWawaData.length; i++) { if (i < wawaComponent.children.length && wawaComponent.children[i] && wawaComponent.children[i].isValid) { const child = wawaComponent.children[i]; const data = this.currentWawaData[i]; child.setPosition(data.x, data.y); child.angle = data.angle; child.active = true; } } // 确保组件启用 wawaComponent.enabled = true; } } } onDestroy() { if (this.isEventBound) { this.node.off(Button.EventType.CLICK, this.onReplayClick, this); } } }。第三个。import { _decorator, Component, EventTouch, Input, input, Vec2, Vec3 } from 'cc'; const { ccclass, property } = _decorator; @ccclass('A_guagouyidong') export class A_guagouyidong extends Component { private isGameActive: boolean = true; public isFirstClick: boolean = true; // 是否第一次点击 private direction: Vec2 = new Vec2(0, -1); // 初始方向 -Y private initialPosition: Vec3 = new Vec3();//初始位置 @property private speed: number = 20; // 移动速度 private angle: number = -90; // 当前角度 (-90度代表-y方向) private currentAngle: number = -90; // 初始角度为 -90 度 onLoad() { // 保存初始位置 this.initialPosition = this.node.position.clone(); // 只在游戏活跃时监听触摸事件 if (this.isGameActive) { // 监听触摸事件 input.on(Input.EventType.TOUCH_START, this.onTouchStart, this); } } /** * 重置爪子位置到初始位置 */ public resetPosition() { // 确保节点有效 if (!this.node || !this.node.isValid) return; // 重置位置 this.node.position = this.initialPosition.clone(); this.stopMovement(); this.isFirstClick = true; this.direction = new Vec2(0, -1); // 重置方向为 -Y this.angle = 0; // 重置角度 this.currentAngle = 0; // 重置当前角度 // 重置旋转 this.node.setRotationFromEuler(0, 0, this.currentAngle); // 重新激活移动 this.isGameActive = true; } stopMovement() { this.isGameActive = false; // 设置游戏状态为结束 this.isFirstClick = true; // 重置点击状态(防止继续移动) input.off(Input.EventType.TOUCH_START, this.onTouchStart, this); // 移除触摸监听 } // 在 A_guagouyidong-001.ts 中添加 resumeMovement() { this.isGameActive = true; this.isFirstClick = true; // 重置点击状态 input.on(Input.EventType.TOUCH_START, this.onTouchStart, this); // 重新绑定 } onTouchStart(event: EventTouch) { if (!this.isGameActive) return; // 游戏结束时不再响应 if (this.isFirstClick) { // 第一次点击,开始向 -Y 方向移动 this.isFirstClick = false; this.moveInDirection(this.direction); } else { this.toggleDirection(); // 调用切换方向的方法 } } toggleDirection() { this.angle = (this.angle === -135) ? -45 : -135; this.direction = new Vec2(Math.cos(this.angle * (Math.PI / 180)), Math.sin(this.angle * (Math.PI / 180))); this.moveInDirection(this.direction); // 切换当前角度 this.currentAngle = (this.currentAngle === 315) ? 45 : 315; this.node.setRotationFromEuler(0, 0, this.currentAngle); } moveInDirection(direction: Vec2) { // 获取当前节点的位置 const currentPosition = this.node.position; // 更新节点位置 const newPosition = new Vec3( currentPosition.x + direction.x * this.speed * 0.02, currentPosition.y + direction.y * this.speed * 0.02, currentPosition.z); this.node.setPosition(newPosition); } update(deltaTime: number) { if (!this.isGameActive) return; // 游戏结束时不再移动 // 如果不是第一次点击,持续移动 if (!this.isFirstClick) { this.moveInDirection(this.direction); } } onDestroy() { // 移除事件监听 input.off(Input.EventType.TOUCH_START, this.onTouchStart, this); } }第四个。现在游戏重玩的部分还是有bug。首先点击重玩回到游戏界面,需要点击两次,主角开始才移动,不像没有重玩之前(刚开始游戏时),点击一次就能移动了。就是3次的机会数字没有更新,例如重玩前数字是2,重玩后还是显示2,不过碰撞后几次次数却正常的,例如重玩后显示数字是2,碰撞一次减一次机会后,数字就正常变为2了,然后就是正常2,1,0,复活后又开始3次机会,就是重玩后显示的机会数字有异常。最后就是重玩后,只要重玩到4次,5次左右,碰撞到wawa后就不会出现gameoverUI界面的,我希望可以一直可以点击重玩。请参考以上代码,帮我找出问题所在。就能优化和不需要做幅度改动。同时都帮我列出完整代码和注释还修改的部分。
06-17
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值