你的第一个 Dora 游戏第八章:游戏界面:开始和结束场景本页总览第八章:游戏界面:开始和结束场景 在这一章节中,我们将学习如何为游戏创建开始界面和结束场景。开始界面用于展示游戏标题并引导玩家进入游戏,而结束场景用于显示游戏结束状态和得分。通过场景的切换,我们可以为游戏实现完整的交互体验。 目标 创建一个简单的游戏开始界面。 实现游戏结束逻辑,并展示得分。 使用 Label 组件显示文本信息。 使用 InputManager 响应玩家的启动操作。 1. 创建开始界面 开始界面是玩家进入游戏时看到的第一屏。我们之前教程中实现的带有游戏标题和“开始”按钮的界面中,增加进入游戏场景的切换功能。 代码实现 dodge_the_creeps/init.tsxconst Background = () => ( <draw-node> <rect-shape width={width} height={height} fillColor={0xff4b6b6c}/> </draw-node>);const StartUp = () => { // 切换到 UI 输入上下文 inputManager.popContext(); inputManager.pushContext('UI'); return ( <> {/* 绘制背景 */} <Background/> {/* 显示标题 */} <label fontName='Xolonium-Regular' fontSize={80} text='Dodge the Creeps!' textWidth={400} y={200} /> {/* 创建开始按钮 */} <draw-node y={-150}> <rect-shape width={250} height={80} fillColor={0xff3a3a3a} /> <label fontName='Xolonium-Regular' fontSize={60} text={'Start'} /> {/* 按钮的交互逻辑 */} <node width={250} height={80} onTapped={() => emit('Input.Start')} onMount={node => { node.gslot('Input.Start', () => { Director.entry.removeAllChildren(); // 清空当前场景 toNode(<Game />); // 进入游戏场景 }); }} /> </draw-node> </> );}; 关键点解析 draw-node 用于绘制背景和按钮框。 label 用于显示文字信息,如标题和按钮文本。 onTapped 响应玩家的点击事件。 使用 emit 和 gslot 来绑定输入事件。 2. 实现游戏结束逻辑