Commit 2b477b30 authored by Tt's avatar Tt

fix(play): 修复游戏初始化时鱼数据清理和更新逻辑

parent e7dff08f
...@@ -184,7 +184,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -184,7 +184,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 重新初始化系统(更新画布尺寸等) // 重新初始化系统(更新画布尺寸等)
this.initSystem(); this.initSystem();
// 更新画面渲染 // 更新画面渲染
this.update(); // this.update();
// 重新启动游戏 // 重新启动游戏
this.restartGame() this.restartGame()
} }
...@@ -341,10 +341,25 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -341,10 +341,25 @@ export class PlayComponent implements OnInit, OnDestroy {
/** /**
* 初始化游戏 * 初始化游戏
* 依次初始化游戏中的各个元素,包括背景、标题、按钮和字母块 * 依次初始化游戏中的各个元素,包括背景、标题、按钮和字母块
* 清理游戏第二阶段中的鱼游动等数据
*/ */
initGame() { initGame() {
// 清理游戏第二阶段中的鱼游动等数据
this.clearFishes(); // 清空所有鱼元素
this.m_fishArray = []; // 清空鱼群管理数组
this.m_lastFishTime = 0; // 重置上次生成鱼的时间
this.currentFishIndex = 0; // 重置当前鱼索引
this.clickedFishData = []; // 清空用户点击的鱼的数据
this.touchFish = null; // 清空当前触摸的鱼
this.isGameStart = false;
// if (this.g_animationId) {
// cancelAnimationFrame(this.g_animationId);
// this.g_animationId = null;
// }
// 初始化游戏背景 // 初始化游戏背景
this.initBackground() this.initBackground();
this.initListPage(); this.initListPage();
} }
...@@ -1198,38 +1213,38 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1198,38 +1213,38 @@ export class PlayComponent implements OnInit, OnDestroy {
} }
createFish(type, data) { createFish(type, data) {
const mapScale = this.g_mapScale > 1 ? 1 : this.g_mapScale;
// 创建鱼元素 // 创建鱼元素
const fishId = `fish_${Math.floor(Math.random() * 1000)}_${Date.now()}` const fishId = `fish_${Math.floor(Math.random() * 1000)}_${Date.now()}`
const fish = this.g_cartoon.createCartoonElement(fishId, 'MySprite'); const fish = this.g_cartoon.createCartoonElement(fishId, 'MySprite');
fish.ref.init(this.g_cartoon.images.get('fish')); fish.ref.init(this.g_cartoon.images.get('fish'));
fish.ref.scaleX = this.g_mapScale; fish.ref.scaleX = mapScale;
fish.ref.scaleY = this.g_mapScale; fish.ref.scaleY = mapScale;
fish.fishId = fishId; fish.fishId = fishId;
fish.ref.width = localImagesSize['pao_pao_da_png'].width * this.g_mapScale; fish.ref.width = localImagesSize['pao_pao_da_png'].width * mapScale;
fish.ref.height = localImagesSize['pao_pao_da_png'].height * this.g_mapScale; fish.ref.height = localImagesSize['pao_pao_da_png'].height * mapScale;
fish.ref.x = this.g_canvasWidth / 2; fish.ref.x = this.g_canvasWidth / 2;
fish.ref.y = this.g_canvasHeight / 2; fish.ref.y = this.g_canvasHeight / 2;
let itemWidth = 300 * this.g_mapScale; let itemWidth = 300 * mapScale;
let itemHeight = 250 * this.g_mapScale; let itemHeight = 250 * mapScale;
// 背景泡泡 // 背景泡泡
// 创建船节点 - 作为钓鱼台,放在最下方 // 创建船节点 - 作为钓鱼台,放在最下方
const pao_pao_da_png = this.g_cartoon.createCartoonElement('pao_pao_da_png', 'MySprite'); const pao_pao_da_png = this.g_cartoon.createCartoonElement('pao_pao_da_png', 'MySprite');
pao_pao_da_png.ref.init(this.g_cartoon.images.get('pao_pao_da_png')); pao_pao_da_png.ref.init(this.g_cartoon.images.get('pao_pao_da_png'));
pao_pao_da_png.ref.scaleX = this.g_mapScale; pao_pao_da_png.ref.scaleX = mapScale;
pao_pao_da_png.ref.scaleY = this.g_mapScale; pao_pao_da_png.ref.scaleY = mapScale;
pao_pao_da_png.ref.x = 0 pao_pao_da_png.ref.x = 0
pao_pao_da_png.ref.y = -70 * this.g_mapScale; pao_pao_da_png.ref.y = -70 * mapScale;
fish.ref.addChild(pao_pao_da_png.ref); fish.ref.addChild(pao_pao_da_png.ref);
// 创建船节点 - 作为钓鱼台,放在最下方 // 创建船节点 - 作为钓鱼台,放在最下方
const pao_pao_po_ani = this.g_cartoon.createCartoonElement('pao_pao_po_ani', 'MySprite'); const pao_pao_po_ani = this.g_cartoon.createCartoonElement('pao_pao_po_ani', 'MySprite');
pao_pao_po_ani.ref.init(this.g_cartoon.images.get('pao_pao_xu_lie_zhen_1')); pao_pao_po_ani.ref.init(this.g_cartoon.images.get('pao_pao_xu_lie_zhen_1'));
pao_pao_po_ani.ref.scaleX = this.g_mapScale; pao_pao_po_ani.ref.scaleX = mapScale;
pao_pao_po_ani.ref.scaleY = this.g_mapScale; pao_pao_po_ani.ref.scaleY = mapScale;
pao_pao_po_ani.ref.x = 0 pao_pao_po_ani.ref.x = 0
pao_pao_po_ani.ref.y = -90 * this.g_mapScale; pao_pao_po_ani.ref.y = -90 * mapScale;
fish.ref.addChild(pao_pao_po_ani.ref); fish.ref.addChild(pao_pao_po_ani.ref);
pao_pao_po_ani.ref.visible = false; pao_pao_po_ani.ref.visible = false;
// 一条小鱼 // 一条小鱼
...@@ -1237,10 +1252,10 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1237,10 +1252,10 @@ export class PlayComponent implements OnInit, OnDestroy {
const nameYu = colors[Math.floor(Math.random() * colors.length)] + '_yu' const nameYu = colors[Math.floor(Math.random() * colors.length)] + '_yu'
const xiao_yu = this.g_cartoon.createCartoonElement(nameYu, 'MySprite'); const xiao_yu = this.g_cartoon.createCartoonElement(nameYu, 'MySprite');
xiao_yu.ref.init(this.g_cartoon.images.get(nameYu)); xiao_yu.ref.init(this.g_cartoon.images.get(nameYu));
xiao_yu.ref.scaleX = this.g_mapScale; xiao_yu.ref.scaleX = mapScale;
xiao_yu.ref.scaleY = this.g_mapScale; xiao_yu.ref.scaleY = mapScale;
xiao_yu.ref.x = 20 * this.g_mapScale; xiao_yu.ref.x = 20 * mapScale;
xiao_yu.ref.y = 90 * this.g_mapScale; xiao_yu.ref.y = 90 * mapScale;
fish.ref.addChild(xiao_yu.ref); fish.ref.addChild(xiao_yu.ref);
fish['child_xiao_yu'] = xiao_yu; fish['child_xiao_yu'] = xiao_yu;
...@@ -1253,7 +1268,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1253,7 +1268,7 @@ export class PlayComponent implements OnInit, OnDestroy {
imageElement.ref.init(img); imageElement.ref.init(img);
// 设置图片位置居中 // 设置图片位置居中
imageElement.ref.x = 0; imageElement.ref.x = 0;
imageElement.ref.y = -100 * this.g_mapScale; imageElement.ref.y = -100 * mapScale;
// 根据背景卡片尺寸调整图片大小,保持适当的边距,应用屏幕适配参数 // 根据背景卡片尺寸调整图片大小,保持适当的边距,应用屏幕适配参数
const padding = 20; // 边距 const padding = 20; // 边距
const maxWidth = itemWidth - padding * 2; const maxWidth = itemWidth - padding * 2;
...@@ -1269,11 +1284,11 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -1269,11 +1284,11 @@ export class PlayComponent implements OnInit, OnDestroy {
fish['child_image'] = imageElement fish['child_image'] = imageElement
} }
if (type == 'big') { if (type == 'big') {
fish.ref.scaleX = this.g_mapScale; fish.ref.scaleX = mapScale;
fish.ref.scaleY = this.g_mapScale; fish.ref.scaleY = mapScale;
} else { } else {
fish.ref.scaleX = this.g_mapScale * 0.7; fish.ref.scaleX = mapScale * 0.7;
fish.ref.scaleY = this.g_mapScale * 0.7; fish.ref.scaleY = mapScale * 0.7;
} }
this.render(fish.ref, zIndexMap.card); this.render(fish.ref, zIndexMap.card);
fish['child_paopao_bg'] = pao_pao_da_png; fish['child_paopao_bg'] = pao_pao_da_png;
...@@ -2623,7 +2638,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -2623,7 +2638,7 @@ export class PlayComponent implements OnInit, OnDestroy {
window["air"].hideAirClassLoading(this.g_KEY, this.g_data); window["air"].hideAirClassLoading(this.g_KEY, this.g_data);
} }
this.initSystem(); this.initSystem();
this.update(); // this.update();
this.systemReady(); this.systemReady();
} catch (error) { } catch (error) {
console.error('初始化系统时出错:', error); console.error('初始化系统时出错:', error);
...@@ -2800,6 +2815,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -2800,6 +2815,7 @@ export class PlayComponent implements OnInit, OnDestroy {
* 负责清除画布、更新动画和渲染所有元素 * 负责清除画布、更新动画和渲染所有元素
*/ */
update() { update() {
this.g_animationId && window.cancelAnimationFrame(this.g_animationId);
// 请求下一帧动画,形成动画循环 // 请求下一帧动画,形成动画循环
this.g_animationId = window.requestAnimationFrame(this.update.bind(this)); this.g_animationId = window.requestAnimationFrame(this.update.bind(this));
// 清除画布内容,准备绘制新的一帧 // 清除画布内容,准备绘制新的一帧
...@@ -2880,6 +2896,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -2880,6 +2896,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 检查是否需要创建新的鱼 // 检查是否需要创建新的鱼
const currentTime = Date.now(); const currentTime = Date.now();
const mapScale = this.g_mapScale < 1 ? 1 : this.g_mapScale;
if (currentTime - this.m_lastFishTime > this.m_fishInterval) { if (currentTime - this.m_lastFishTime > this.m_fishInterval) {
// 创建新的鱼 // 创建新的鱼
if (currentStage && currentStage.questionRandomList && currentStage.questionRandomList.length > 0) { if (currentStage && currentStage.questionRandomList && currentStage.questionRandomList.length > 0) {
...@@ -2898,10 +2915,10 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -2898,10 +2915,10 @@ export class PlayComponent implements OnInit, OnDestroy {
// 设置鱼的初始位置在屏幕右侧外部 // 设置鱼的初始位置在屏幕右侧外部
fish.ref.x = this.g_canvasWidth + fish.ref.width * fish.ref.scaleX / 2; fish.ref.x = this.g_canvasWidth + fish.ref.width * fish.ref.scaleX / 2;
fish.ref.y = this.currentFishIndex % 2 == 0 ? 400 : 300; // Math.random() * (this.g_canvasHeight * 0.35) + this.g_canvasHeight * 0.25; // 在屏幕中间区域随机高度 fish.ref.y = (this.currentFishIndex % 2 == 0 ? 400 : 300) * this.g_mapScale; // Math.random() * (this.g_canvasHeight * 0.35) + this.g_canvasHeight * 0.25; // 在屏幕中间区域随机高度
// 设置鱼的游动速度 // 设置鱼的游动速度
fish.speed = 0.45; fish.speed = 0.45 * 2 * this.g_mapScale;
// 确保鱼已添加到渲染队列中 // 确保鱼已添加到渲染队列中
// createFish 方法中已经调用了 render 方法,但为了确保,这里再次检查 // createFish 方法中已经调用了 render 方法,但为了确保,这里再次检查
...@@ -3141,7 +3158,7 @@ export class PlayComponent implements OnInit, OnDestroy { ...@@ -3141,7 +3158,7 @@ export class PlayComponent implements OnInit, OnDestroy {
// 更新画布高度为容器当前高度 // 更新画布高度为容器当前高度
this.g_canvasHeight = this.wrap.nativeElement.clientHeight; this.g_canvasHeight = this.wrap.nativeElement.clientHeight;
// 更新画面渲染 // 更新画面渲染
this.update(); // this.update();
// 处理屏幕尺寸变化,重新初始化游戏 // 处理屏幕尺寸变化,重新初始化游戏
this.handleScreenResize() this.handleScreenResize()
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment