zhanwei

本文为一篇简单的占位内容,用于示例说明。具体内容待后续更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

占个位置

转载于:https://www.cnblogs.com/wing411/p/4987508.html

import { _decorator, Component, instantiate, isValid, Node, Sprite, SpriteFrame } from 'cc'; import { G_shoucangye } from './G_shoucangye'; import { G_wenhao } from './G_wenhao'; import { G_WAWAhao } from './G_WAWAhao'; const { ccclass, property } = _decorator; @ccclass('G_shoucangkongzhi') export class G_shoucangkongzhi extends Component { @property({ type: Node }) public zhanweiContainer: Node = null; @property({ type: [Node] }) public wawaNodes: Node[] = []; private wawaNodeMap: Map<number, Node> = new Map(); onLoad() { // 修复ID映射问题:使用娃娃组件上的实际ID this.wawaNodes.forEach((node) => { const wawaComp = node.getComponent(G_WAWAhao); if (wawaComp && wawaComp.id > 0) { this.wawaNodeMap.set(wawaComp.id, node); console.log(`映射娃娃ID: ${wawaComp.id} 到节点: ${node.name}`); } }); } onEnable() { // 添加:当节点激活时刷新显示(场景切换时会触发) this.updateCollectionDisplay(); } start() { this.updateCollectionDisplay(); } // 添加:手动刷新方法 public refreshCollection() { // 调用 G_shoucangye 的 load 方法重新加载数据 G_shoucangye["load"](); // 修复:使用索引访问替代直接调用 this.updateCollectionDisplay(); } updateCollectionDisplay() { console.log("=== 开始更新收藏显示 ==="); if (!this.zhanweiContainer) { console.error("展位容器未设置"); return; } // 获取已收藏的娃娃ID列表 const collectedIds = G_shoucangye["getCollectedWawaIds"](); // 修复:使用索引访问替代直接调用 if (!collectedIds) { console.error("无法获取收藏数据"); return; } console.log("已收藏的娃娃ID:", collectedIds); // 遍历所有展位 for (let i = 0; i < this.zhanweiContainer.children.length; i++) { const zhanweiNode = this.zhanweiContainer.children[i]; if (!isValid(zhanweiNode)) continue; const zhanwei = zhanweiNode.getComponent(G_wenhao); if (!zhanwei) continue; const wawaId = zhanwei.wawaId; console.log(`检查展位 ${zhanweiNode.name}, 娃娃ID: ${wawaId}`); // 检查是否已收藏 - 使用 indexOf 替代 includes const isCollected = collectedIds.indexOf(wawaId) !== -1; // 获取问号节点(假设是展位的第一个子节点) const wenhaoNode = zhanweiNode.children[0]; if (isCollected) { console.log(`需要显示娃娃 ${wawaId}`); // 隐藏问号节点 if (wenhaoNode && isValid(wenhaoNode)) { wenhaoNode.active = false; } // 检查是否已存在娃娃节点 let wawaNode = zhanweiNode.getChildByName(`Collected_Wawa_${wawaId}`); if (!wawaNode) { const wawaNodePrefab = this.wawaNodeMap.get(wawaId); if (!wawaNodePrefab) { console.error(`找到娃娃ID ${wawaId} 的节点资源`); continue; } // 实例化娃娃节点并作为展位的子节点 wawaNode = instantiate(wawaNodePrefab); wawaNode.name = `Collected_Wawa_${wawaId}`; wawaNode.setParent(zhanweiNode); wawaNode.setPosition(0, 0, 0); // 相对于展位中心 console.log(`创建新娃娃节点: ${wawaId}`); } // 确保娃娃节点可见 if (wawaNode && isValid(wawaNode)) { wawaNode.active = true; } } else { // 显示问号节点 if (wenhaoNode && isValid(wenhaoNode)) { wenhaoNode.active = true; } // 隐藏娃娃节点(如果存在) const wawaNode = zhanweiNode.getChildByName(`Collected_Wawa_${wawaId}`); if (wawaNode && isValid(wawaNode)) { wawaNode.active = false; } } } } }。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'; import { G_shoucangye } from './G_shoucangye'; import { G_WAWAhao } from './G_WAWAhao'; const { ccclass, property } = _decorator; @ccclass('B_pengzhuangDefenxitong') export class B_pengzhuangDefenxitong extends Component { private processedColliders: Map<string, Tags> = new Map();// 使用单个Map替代多个Set @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(); } // 设置游戏结束回调 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); const wawaComponent = otherCollider.node.getComponent(G_WAWAhao); // 检查娃娃是否已经被收藏 if (wawaComponent && wawaComponent.id > 0) { // 如果是第一次抓到这个娃娃 if (!G_shoucangye.isWawaCollected(wawaComponent.id)) { // 收藏并保存 G_shoucangye.collectWawa(wawaComponent.id); // 你可以在这里添加首次抓到的特殊奖励 // 确保立即保存收藏数据 G_shoucangye.save(); } // 无论是新抓到还是重复抓到,都加分 this.addScore(10); } else { // 如果娃娃没有配置ID,也默认加分 this.addScore(10); } // 统一通过回调处理游戏结束 if (this.onGameOverCallback) { this.onGameOverCallback(true); } else { // 如果没有设置回调,使用本地方法 this.gameOver(true); // 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()); } } public reset() { this.processedColliders.clear(); this.updateChanceUI(); } private addScore(count: number = 1) { GameData.addScore(count); } }。import { sys } from 'cc'; const COLLECTION_KEY = 'collected_wawas'; export class G_shoucangye { private static collectedWawas: Set<number> = new Set(); private static isInitialized = false; private static initialize() { if (!this.isInitialized) { this.load(); this.isInitialized = true; } } public static collectWawa(id: number) { this.initialize(); if (id > 0 && !this.collectedWawas.has(id)) { this.collectedWawas.add(id); this.save(); console.log(`收集到新的娃娃,ID: ${id}`); } } public static isWawaCollected(id: number): boolean { this.initialize(); return this.collectedWawas.has(id); } public static getCollectedWawaIds(): number[] { this.initialize(); return Array.from(this.collectedWawas); } public static save() { const data = JSON.stringify(Array.from(this.collectedWawas)); sys.localStorage.setItem(COLLECTION_KEY, data); } private static load() { const data = sys.localStorage.getItem(COLLECTION_KEY); if (data) { try { const ids = JSON.parse(data); if (Array.isArray(ids)) { this.collectedWawas = new Set(ids); } } catch (e) { console.error("加载收藏数据失败", e); this.collectedWawas = new Set(); } } } }。import { _decorator, CCInteger, Component, Node } from 'cc'; const { ccclass, property } = _decorator; @ccclass('G_WAWAhao') export class G_WAWAhao extends Component { @property({ type: CCInteger, tooltip: '娃娃的唯一ID (例如 1-45)' }) public id: number = 0; }。import { _decorator, CCInteger, Component, Node } from 'cc'; const { ccclass, property } = _decorator; @ccclass('G_wenhao') export class G_wenhao extends Component { @property({ type: CCInteger, tooltip: '此展位对应的娃娃ID' }) public wawaId: number = 0; }。都设置好了,可是没有效果,控制台有出现[PreviewInEditor] 收集到新的娃娃,ID: 10。但是去到收藏页,什么都没有。在重新说明一下,我创建了3个场景的,开始游戏和收藏按钮在一个场景的,然后点击收藏按钮就去到收藏页了,这是新创建的一个场景,里面创建了ShoucangyeManager节点,挂载了G_shoucangkongzhi。和wawa资源在这里。最后一个场景就是游戏场景了,在这里抓娃娃,然后抓到的wawa显示在收藏页里面。我现在是想实现,游戏场景中抓到的wawa后,能够以复制的形式,永久显示在收藏页场景里面,代替原来放在这里的对应wenhao节点。请问哪里出问题了,还有请结合场景之间的联系帮我查找一下是否和场景联系有关才使wawa能在收藏页上显示
06-24
内容概要:本文档主要展示了C语言中关于字符串处理、指针操作以及动态内存分配的相关代码示例。首先介绍了如何实现键值对(“key=value”)字符串的解析,包括去除多余空格和根据键获取对应值的功能,并提供了相应的测试用例。接着演示了从给定字符串中分离出奇偶位置字符的方法,并将结果分别存储到两个同的缓冲区中。此外,还探讨了常量(const)修饰符在变量和指针中的应用规则,解释了同类型指针的区别及其使用场景。最后,详细讲解了如何动态分配二维字符数组,并实现了对这类数组的排序与释放操作。 适合人群:具有C语言基础的程序员或计算机科学相关专业的学生,尤其是那些希望深入理解字符串处理、指针操作以及动态内存管理机制的学习者。 使用场景及目标:①掌握如何高效地解析键值对字符串并去除其中的空白字符;②学会编写能够正确处理奇偶索引字符的函数;③理解const修饰符的作用范围及其对程序逻辑的影响;④熟悉动态分配二维字符数组的技术,并能对其进行有效的排序和清理。 阅读建议:由于本资源涉及较多底层概念和技术细节,建议读者先复习C语言基础知识,特别是指针和内存管理部分。在学习过程中,可以尝试动手编写类似的代码片段,以便更好地理解和掌握文中所介绍的各种技巧。同时,注意观察代码注释,它们对于理解复杂逻辑非常有帮助。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值