mirror of
http://junepve.top:3000/chicrobot/CINC.git
synced 2026-10-29 21:31:29 +08:00
fix(scenes) 种田系统测试,添加按钮等
This commit is contained in:
1 parent
ce4e04740a
commit
761a9338ba
19 files changed
+1248
-86
No files matched your search
+129
-11
@@ -11,11 +11,12 @@ export default class BootScene extends Phaser.Scene {
|
||||
this.cameras.main.setBackgroundColor('#000000');
|
||||
|
||||
// 添加标题文本
|
||||
this.add.text(this.sys.game.config.width / 2, 100, '测试场景菜单', {
|
||||
const title = this.add.text(this.sys.game.config.width / 2, 50, '测试场景菜单', {
|
||||
fontSize: '32px',
|
||||
fill: '#ffffff',
|
||||
fontFamily: 'Arial'
|
||||
}).setOrigin(0.5, 0.5);
|
||||
title.setScrollFactor(0); // 固定标题,不随滚动移动
|
||||
|
||||
// 测试场景列表
|
||||
const testScenes = [
|
||||
@@ -36,17 +37,46 @@ export default class BootScene extends Phaser.Scene {
|
||||
{ key: 'GameScene', name: '返回主游戏场景' }
|
||||
];
|
||||
|
||||
// 创建按钮网格
|
||||
const buttonWidth = 400;
|
||||
const buttonHeight = 50;
|
||||
const buttonSpacing = 20;
|
||||
const startY = 200;
|
||||
// 滚动容器配置
|
||||
const containerWidth = this.sys.game.config.width - 40;
|
||||
const containerHeight = this.sys.game.config.height - 150;
|
||||
const containerX = 20;
|
||||
const containerY = 100;
|
||||
|
||||
// 创建滚动容器背景
|
||||
const scrollBackground = this.add.rectangle(
|
||||
this.sys.game.config.width / 2,
|
||||
containerY + containerHeight / 2,
|
||||
containerWidth,
|
||||
containerHeight,
|
||||
0x111111,
|
||||
0.5
|
||||
);
|
||||
scrollBackground.setOrigin(0.5, 0.5);
|
||||
scrollBackground.setScrollFactor(0); // 固定背景,不随滚动移动
|
||||
|
||||
// 创建滚动容器
|
||||
this.scrollContainer = this.add.container(containerX, containerY);
|
||||
|
||||
// 按钮配置
|
||||
const columns = 2; // 2列布局
|
||||
const buttonWidth = (containerWidth - 30) / columns; // 两列按钮,留出间距
|
||||
const buttonHeight = 50;
|
||||
const buttonSpacing = 20; // 按钮间距
|
||||
const columnSpacing = 10; // 列间距
|
||||
|
||||
// 创建按钮
|
||||
testScenes.forEach((scene, index) => {
|
||||
// 计算按钮位置(2列布局)
|
||||
const col = index % columns;
|
||||
const row = Math.floor(index / columns);
|
||||
const x = col * (buttonWidth + columnSpacing);
|
||||
const y = row * (buttonHeight + buttonSpacing);
|
||||
|
||||
// 创建按钮背景
|
||||
const button = this.add.rectangle(
|
||||
this.sys.game.config.width / 2,
|
||||
startY + index * (buttonHeight + buttonSpacing),
|
||||
x + buttonWidth / 2,
|
||||
y + buttonHeight / 2,
|
||||
buttonWidth,
|
||||
buttonHeight,
|
||||
0x333333
|
||||
@@ -54,15 +84,16 @@ export default class BootScene extends Phaser.Scene {
|
||||
|
||||
// 添加按钮文本
|
||||
const text = this.add.text(
|
||||
this.sys.game.config.width / 2,
|
||||
startY + index * (buttonHeight + buttonSpacing),
|
||||
x + buttonWidth / 2,
|
||||
y + buttonHeight / 2,
|
||||
scene.name,
|
||||
{
|
||||
fontSize: '18px',
|
||||
fontSize: '16px',
|
||||
fill: '#ffffff',
|
||||
fontFamily: 'Arial'
|
||||
}
|
||||
).setOrigin(0.5, 0.5);
|
||||
text.setWordWrapWidth(buttonWidth - 20); // 文本自动换行
|
||||
|
||||
// 使按钮可交互
|
||||
button.setInteractive({
|
||||
@@ -82,6 +113,93 @@ export default class BootScene extends Phaser.Scene {
|
||||
button.on('pointerout', () => {
|
||||
button.setFillStyle(0x333333);
|
||||
});
|
||||
|
||||
// 将按钮和文本添加到滚动容器
|
||||
this.scrollContainer.add(button);
|
||||
this.scrollContainer.add(text);
|
||||
});
|
||||
|
||||
// 计算滚动容器的实际高度
|
||||
const rows = Math.ceil(testScenes.length / columns);
|
||||
const contentHeight = rows * (buttonHeight + buttonSpacing);
|
||||
|
||||
// 添加滚动功能
|
||||
this.setupScrolling(contentHeight, containerHeight);
|
||||
|
||||
// 添加滚动提示
|
||||
const scrollHint = this.add.text(
|
||||
this.sys.game.config.width / 2,
|
||||
this.sys.game.config.height - 30,
|
||||
'鼠标滚轮或触摸滑动滚动',
|
||||
{
|
||||
fontSize: '14px',
|
||||
fill: '#aaaaaa',
|
||||
fontFamily: 'Arial'
|
||||
}
|
||||
).setOrigin(0.5, 0.5);
|
||||
scrollHint.setScrollFactor(0); // 固定提示,不随滚动移动
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置滚动功能
|
||||
* @param {number} contentHeight - 内容高度
|
||||
* @param {number} containerHeight - 容器高度
|
||||
*/
|
||||
setupScrolling(contentHeight, containerHeight) {
|
||||
// 只有当内容高度大于容器高度时才添加滚动
|
||||
if (contentHeight <= containerHeight) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 滚动位置
|
||||
this.scrollY = 0;
|
||||
// 最大滚动位置
|
||||
this.maxScrollY = contentHeight - containerHeight;
|
||||
|
||||
// 添加鼠标滚轮事件
|
||||
this.input.on('wheel', (pointer, gameObjects, deltaX, deltaY, deltaZ) => {
|
||||
// 计算新的滚动位置
|
||||
this.scrollY += deltaY * 0.5; // 调整滚动速度
|
||||
|
||||
// 限制滚动范围
|
||||
this.scrollY = Math.max(0, Math.min(this.scrollY, this.maxScrollY));
|
||||
|
||||
// 应用滚动
|
||||
this.scrollContainer.setPosition(this.scrollContainer.x, this.scrollContainer.y - this.scrollContainer.list[0].y + this.scrollY);
|
||||
});
|
||||
|
||||
// 添加触摸滚动支持
|
||||
let isDragging = false;
|
||||
let lastY = 0;
|
||||
|
||||
this.input.on('pointerdown', (pointer) => {
|
||||
isDragging = true;
|
||||
lastY = pointer.y;
|
||||
});
|
||||
|
||||
this.input.on('pointermove', (pointer) => {
|
||||
if (isDragging) {
|
||||
const deltaY = pointer.y - lastY;
|
||||
|
||||
// 计算新的滚动位置
|
||||
this.scrollY -= deltaY * 0.5; // 调整滚动速度
|
||||
|
||||
// 限制滚动范围
|
||||
this.scrollY = Math.max(0, Math.min(this.scrollY, this.maxScrollY));
|
||||
|
||||
// 应用滚动
|
||||
this.scrollContainer.setPosition(this.scrollContainer.x, this.scrollContainer.y - this.scrollContainer.list[0].y + this.scrollY);
|
||||
|
||||
lastY = pointer.y;
|
||||
}
|
||||
});
|
||||
|
||||
this.input.on('pointerup', () => {
|
||||
isDragging = false;
|
||||
});
|
||||
|
||||
this.input.on('pointerout', () => {
|
||||
isDragging = false;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user