写了一个类似于Ren'Py文字小说的游戏Demo,但不会做存档功能,HarmonyOS 鸿蒙Next大神们有没有办法介绍如何制作?
写了一个类似于Ren’Py文字小说的游戏Demo,但不会做存档功能,HarmonyOS 鸿蒙Next大神们有没有办法介绍如何制作? #主菜单
import { router } from '@kit.ArkUI'
// StartScreen.ets
@Entry
@Component
struct StartScreen {
@State isMenuVisible: boolean = true
build() {
Stack() {
// 背景图
Image($r('app.media.01'))
.width('100%')
.height('100%')
// 主菜单(居中布局)
Column() {
// 游戏标题
Text("测试")
.fontSize(40)
.fontColor(Color.White)
.margin({ bottom: 50 })
// 开始游戏按钮
Button("开始游戏", { type: ButtonType.Normal })
.width(200)
.height(50)
.backgroundColor('#4CAF50')
.onClick(() => {
// 处理开始游戏逻辑
router.pushUrl({ url: 'pages/GamePage' })
})
// 设置按钮
Button("设置", { type: ButtonType.Normal })
.width(200)
.height(50)
.margin({ top: 20 })
.backgroundColor('#2196F3')
.onClick(() => {
// 处理开始游戏逻辑
router.pushUrl({ url: 'pages/设置' })
})
}.alignItems(HorizontalAlign.Center)
.justifyContent(FlexAlign.Center)
.width('100%')
.height('100%')
// 设置面板(默认隐藏)
}
}
}
游戏内容
import { router } from '@kit.ArkUI'
@Entry
@Component
struct MediaStory {
@State currentMediaIndex: number = 0
@State currentTextIndex: number = 0
@State textOpacity: number = 1 // 改为textOpacity专用控制
//游戏脚本内容
private mediaSequence: MediaItem[] = [
{
type: 'image',
source: $r('app.media.01'),
captions: ['你好', '1122', '1222234']
},
{
type: 'image',
source: $r('app.media.02'),
captions: ['你好', '12222', '12331214']
},
{
type: 'video',
source: $r('app.media.video01'),
captions: ['你好', '12121', '1222234']
},
{
type: 'image',
source: $r('app.media.03'),
captions: ['你好', '12121', '123121214']
}
]
handleTap() {
this.textOpacity = 0
setTimeout(() => {
if (this.currentTextIndex < this.mediaSequence[this.currentMediaIndex].captions.length - 1) {
this.currentTextIndex += 1
} else {
this.currentMediaIndex = (this.currentMediaIndex + 1) % this.mediaSequence.length
this.currentTextIndex = 0
}
this.textOpacity = 1
}, 300)
}
build() {
Stack({ alignContent: Alignment.Bottom }) {
// 媒体展示部分保持不变
if (this.mediaSequence[this.currentMediaIndex].type === 'image') {
Image(this.mediaSequence[this.currentMediaIndex].source)
.width('100%')
.height('100%')
.objectFit(ImageFit.Cover)
} else {
Video({
src: this.mediaSequence[this.currentMediaIndex].source,
controller: new VideoController()
})
.width('100%')
.height('100%')
.controls(false)
.autoPlay(true) // 必须设置
}
// 文字层修改点
Column() {
Text(this.mediaSequence[this.currentMediaIndex].captions[this.currentTextIndex])
.fontSize(24)
.fontColor(Color.White)
.textShadow({ radius: 2, color: Color.Black, offsetX: 1, offsetY: 1 })
.opacity(this.textOpacity) // 改为textOpacity控制
.padding("10%")
Row() {
Button('主菜单')
.fontColor(Color.White)
.backgroundColor('#3CB371')
.onClick(() => {
router.pushUrl({ url: 'pages/Index' })
})
Button('设置')
.fontColor(Color.White)
.backgroundColor('#4682B4')
.onClick(() => {
router.pushUrl({ url: 'pages/设置' })
})
}
.width('90%')
.height(50)
.position({ x: '5%', y: 20 })
.justifyContent(FlexAlign.SpaceBetween)
}
.padding(10)
.width('80%')
}
.onClick(() => this.handleTap())
.height('100%')
}
}
interface MediaItem {
type: 'image' | 'video'
source: Resource
captions: string[]
}
更多关于写了一个类似于Ren'Py文字小说的游戏Demo,但不会做存档功能,HarmonyOS 鸿蒙Next大神们有没有办法介绍如何制作?的实战教程也可以访问 https://www.itying.com/category-93-b0.html
1 回复
更多关于写了一个类似于Ren'Py文字小说的游戏Demo,但不会做存档功能,HarmonyOS 鸿蒙Next大神们有没有办法介绍如何制作?的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在鸿蒙Next中实现存档功能,可以使用Preferences
或Database
来存储游戏数据。Preferences
适合存储简单的键值对数据,而Database
适合存储结构化数据。
- 使用Preferences:
- 首先,导入
@ohos.data.preferences
模块。 - 创建
Preferences
实例,并定义存储的键值对。 - 使用
put()
方法保存数据,使用get()
方法读取数据。 - 最后,调用
flush()
方法将数据持久化到文件中。
示例代码:
import preferences from '@ohos.data.preferences';
let pref = preferences.getPreferences(this.context, 'gameData');
pref.put('gameState', 'currentState');
pref.flush();
- 使用Database:
- 首先,导入
@ohos.data.rdb
模块。 - 创建或打开数据库,并定义表结构。
- 使用
insert()
方法插入数据,使用query()
方法查询数据。 - 数据操作完成后,关闭数据库连接。
示例代码:
import relationalStore from '@ohos.data.rdb';
let store = relationalStore.getRdbStore(this.context, { name: 'gameDB' });
let sql = 'INSERT INTO gameState (state) VALUES (?)';
store.executeSql(sql, ['currentState']);
根据你的游戏数据复杂度,选择合适的方法实现存档功能。