Commit 38a3e43f authored by Tt's avatar Tt

refactor(SSAPP2502): 优化糖果节点管理和滚动逻辑

parent bf4e8a1c
...@@ -349,15 +349,15 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -349,15 +349,15 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.scrollDistance = 0; this.scrollDistance = 0;
this.needScroll = unansweredQuestions.length > 4; // 如果有超过4个未回答题目,启用滚动 this.needScroll = unansweredQuestions.length > 4; // 如果有超过4个未回答题目,启用滚动
// 初始化数据队列 // 清空现有节点和待添加队列
this.activeNodes = [];
this.layout.removeAllChildren();
this.pendingCandyData = []; // 重置待添加队列
// 初始化显示的节点数量
// 如果剩余题目小于等于4个,显示所有剩余题目;否则显示5个 // 如果剩余题目小于等于4个,显示所有剩余题目;否则显示5个
const initialVisibleCount = unansweredQuestions.length <= 4 ? unansweredQuestions.length : Math.min(5, unansweredQuestions.length); const initialVisibleCount = unansweredQuestions.length <= 4 ? unansweredQuestions.length : Math.min(5, unansweredQuestions.length);
const visibleData = unansweredQuestions.slice(0, initialVisibleCount); const visibleData = unansweredQuestions.slice(0, initialVisibleCount);
this.pendingCandyData = unansweredQuestions.slice(initialVisibleCount);
// 清空现有节点
this.activeNodes = [];
this.layout.removeAllChildren();
// 添加初始可见的糖果节点 // 添加初始可见的糖果节点
visibleData.forEach((data, index) => { visibleData.forEach((data, index) => {
...@@ -372,34 +372,51 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -372,34 +372,51 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
/** /**
* 添加新的糖果节点到布局的最后位置 * 获取界面上已显示的题目ID列表
* 如果待添加队列为空,会重新获取未回答的题目
*/ */
private addNewCandyNode() { private getDisplayedQuestionIds(): number[] {
// 检查是否有糖果节点模板 const displayedIds: number[] = [];
if (!this.item) return; if (this.layout && this.layout.childrenCount > 0) {
for (let i = 0; i < this.layout.childrenCount; i++) {
const node = this.layout.children[i] as CandyNode;
if (node.active && node.data && node.data.id) {
displayedIds.push(node.data.id);
}
}
}
return displayedIds;
}
// 如果待添加队列为空,重新获取未回答的题目 /**
if (this.pendingCandyData.length === 0) { * 更新待添加队列,只添加界面上没有显示的题目
*/
private updatePendingCandyData() {
// 获取所有未回答的题目
const unansweredQuestions = this.getUnansweredQuestions(); const unansweredQuestions = this.getUnansweredQuestions();
// 获取界面上已显示的题目ID
const displayedIds = this.getDisplayedQuestionIds();
// 获取待添加队列中的题目ID
const pendingIds = this.pendingCandyData.map(item => item.id);
// 如果没有未回答的题目,则不添加新节点 // 只添加界面上没有显示且不在待添加队列中的题目
if (unansweredQuestions.length === 0) { const newPendingData = unansweredQuestions.filter(question =>
this.needScroll = false; // 停止滚动 !displayedIds.includes(question.id) && !pendingIds.includes(question.id)
return; );
}
// 如果未回答题目数量小于等于4,停止滚动并重新刷新列表 // 将新的题目添加到待添加队列
if (unansweredQuestions.length <= 4) { this.pendingCandyData.push(...newPendingData);
this.needScroll = false; // 停止滚动
// 重新显示页面
this.showPage();
return;
} }
// 将未回答的题目添加到待添加队列 /**
this.pendingCandyData = unansweredQuestions; * 添加新的糖果节点到布局的最后位置
} * 只有当界面上没有对应题目的节点时才会添加
*/
private addNewCandyNode() {
// 检查是否有糖果节点模板
if (!this.item) return;
// 更新待添加队列
this.updatePendingCandyData();
// 从待添加队列中取出一个数据 // 从待添加队列中取出一个数据
const data = this.pendingCandyData.shift(); const data = this.pendingCandyData.shift();
...@@ -478,20 +495,11 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -478,20 +495,11 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 重置无操作计时器 // 重置无操作计时器
this.inactiveTimer = 0; this.inactiveTimer = 0;
// 更新待添加的糖果数据队列,获取最新的未回答题目 // 从待添加队列中移除已回答的题目
// 这样在无限滚动时会排除刚刚回答正确的题目 if (this.touchData) {
const unansweredQuestions = this.getUnansweredQuestions(); const answeredQuestionId = this.touchData.id;
this.pendingCandyData = unansweredQuestions; this.pendingCandyData = this.pendingCandyData.filter(q => q.id !== answeredQuestionId);
}
// // 如果未回答题目数量小于5,停止滚动并重新刷新列表
// if (unansweredQuestions.length < 5 && unansweredQuestions.length > 0) {
// this.needScroll = false; // 停止滚动
// // 延迟一段时间后重新显示页面
// this.scheduleOnce(() => {
// this.showPage();
// }, 0.1); // 延迟1秒,给用户时间看到当前页面的完成状态
// return;
// }
// 在滚动列表中查找并隐藏对应题目的节点 // 在滚动列表中查找并隐藏对应题目的节点
if (this.touchData && this.layout && this.layout.childrenCount > 0) { if (this.touchData && this.layout && this.layout.childrenCount > 0) {
...@@ -512,13 +520,20 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -512,13 +520,20 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
} }
// 检查当前页面是否所有火苗都已处理完毕 // 更新待添加队列,确保只包含界面上没有显示的未回答题目
this.updatePendingCandyData();
// 检查当前页面是否所有活跃节点都已处理完毕
if (this.activeNodes.length === 0) { if (this.activeNodes.length === 0) {
// 延迟一段时间后检查是否需要切换到下一页 // 检查是否还有未回答的题目
this.scheduleOnce(() => { const unansweredQuestions = this.getUnansweredQuestions();
// 检查是否需要进入下一页 if (unansweredQuestions.length > 0) {
this.nextPage(); // 还有未回答的题目,重新显示页面
}, 1.0); // 延迟1秒,给用户时间看到当前页面的完成状态 // this.showPage();
} else {
// 所有题目都已回答完毕,游戏结束
this.gameOver();
}
} }
} }
onRecordFail() { onRecordFail() {
...@@ -556,27 +571,27 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -556,27 +571,27 @@ export default class SceneComponent extends MyCocosSceneComponent {
* 在无限滚动模式下,更新未回答题目列表并重新显示页面 * 在无限滚动模式下,更新未回答题目列表并重新显示页面
*/ */
nextPage() { nextPage() {
// 切换到下一页 // // 切换到下一页
Game.getIns().addPage(); // Game.getIns().addPage();
// 获取最新的未回答题目 // // 获取最新的未回答题目
const unansweredQuestions = this.getUnansweredQuestions(); // const unansweredQuestions = this.getUnansweredQuestions();
this.pendingCandyData = unansweredQuestions; // this.pendingCandyData = unansweredQuestions;
// 如果没有未回答的题目,游戏结束 // // 如果没有未回答的题目,游戏结束
if (unansweredQuestions.length === 0) { // if (unansweredQuestions.length === 0) {
// 可以在这里添加游戏结束逻辑 // // 可以在这里添加游戏结束逻辑
this.gameOver(); // this.gameOver();
return; // return;
} // }
// 如果未回答题目数量小于等于4,设置不滚动 // // 如果未回答题目数量小于等于4,设置不滚动
if (unansweredQuestions.length <= 4) { // if (unansweredQuestions.length <= 4) {
this.needScroll = false; // this.needScroll = false;
} // }
// 重新显示页面 // // 重新显示页面
this.showPage(); // this.showPage();
} }
/** /**
...@@ -887,7 +902,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -887,7 +902,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
// 更新滚动状态,根据剩余未回答题目数量决定是否继续滚动 // 更新滚动状态,根据剩余未回答题目数量决定是否继续滚动
this.needScroll = this.activeNodes.length > 4 || this.pendingCandyData.length > 0; // this.needScroll = this.activeNodes.length > 4 || this.pendingCandyData.length > 0;
} }
/** /**
...@@ -928,20 +943,24 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -928,20 +943,24 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 累计滚动距离 // 累计滚动距离
this.scrollDistance = Math.abs(this.layout.x) - this.addCount * 100; this.scrollDistance = Math.abs(this.layout.x) - this.addCount * 100;
if (this.layout.childrenCount <= 6) { if (this.layout.childrenCount <= 6) {
if (!this.addCount && this.pendingCandyData.length > 0) { if (!this.addCount) {
this.addCount = 1; this.addCount = 1;
this.addNewCandyNode(); // 添加新节点 this.addNewCandyNode(); // 添加新节点
} else { } else {
if (this.pendingCandyData.length == 0) { // 更新待添加队列,确保只添加界面上没有的题目
let unansweredQuestions = this.getUnansweredQuestions(); this.updatePendingCandyData();
// 如果待添加队列为空,检查是否需要停止滚动
if (this.pendingCandyData.length === 0) {
const unansweredQuestions = this.getUnansweredQuestions();
// 如果未回答题目数量小于等于4,停止滚动并重新刷新列表 // 如果未回答题目数量小于等于4,停止滚动并重新刷新列表
if (unansweredQuestions.length <= 4) { // if (unansweredQuestions.length <= 4) {
this.needScroll = false; // 停止滚动 // this.needScroll = false; // 停止滚动
this.showPage(); // 重新显示页面 // this.showPage(); // 重新显示页面
return; // return;
} // }
this.pendingCandyData = unansweredQuestions.concat();
} }
this.addCount++; this.addCount++;
this.addNewCandyNode(); // 添加新节点 this.addNewCandyNode(); // 添加新节点
} }
...@@ -950,6 +969,9 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -950,6 +969,9 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 检查第一个糖果节点是否已经移出视图 // 检查第一个糖果节点是否已经移出视图
const firstCandy = this.layout.children[0]; const firstCandy = this.layout.children[0];
if (firstCandy && firstCandy.x + this.layout.x + firstCandy.width / 2 < 0) { if (firstCandy && firstCandy.x + this.layout.x + firstCandy.width / 2 < 0) {
// 获取移出画面的节点数据
const removedNodeData = (firstCandy as CandyNode).data;
// 从活跃节点数组中移除第一个节点 // 从活跃节点数组中移除第一个节点
if (this.activeNodes.includes(firstCandy)) { if (this.activeNodes.includes(firstCandy)) {
const index = this.activeNodes.indexOf(firstCandy); const index = this.activeNodes.indexOf(firstCandy);
...@@ -962,6 +984,15 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -962,6 +984,15 @@ export default class SceneComponent extends MyCocosSceneComponent {
firstCandy.removeFromParent(); firstCandy.removeFromParent();
firstCandy.destroy(); firstCandy.destroy();
// 如果移出的节点对应的题目还未回答,将其重新添加到待添加队列
if (removedNodeData) {
const unansweredQuestions = this.getUnansweredQuestions();
const isStillUnanswered = unansweredQuestions.some(q => q.id === removedNodeData.id);
if (isStillUnanswered && !this.pendingCandyData.some(q => q.id === removedNodeData.id)) {
this.pendingCandyData.push(removedNodeData);
}
}
console.log("当前节点数量" + this.layout.childrenCount); console.log("当前节点数量" + this.layout.childrenCount);
} }
} }
...@@ -1122,6 +1153,8 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -1122,6 +1153,8 @@ export default class SceneComponent extends MyCocosSceneComponent {
let isSuccess = this.checkWords(data); let isSuccess = this.checkWords(data);
this.log("data: " + JSON.stringify(data)); this.log("data: " + JSON.stringify(data));
// 保存录音数据
Game.getIns().player.addVoice(data);
// 根据评分和单词正确性显示不同的提示 // 根据评分和单词正确性显示不同的提示
if (isSuccess) { if (isSuccess) {
this.recordTouching = false; this.recordTouching = false;
...@@ -1131,20 +1164,14 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -1131,20 +1164,14 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 80分及以上,显示优秀提示(3星) // 80分及以上,显示优秀提示(3星)
this.updateTips(TIPS_STATE.EXCELLENT); this.updateTips(TIPS_STATE.EXCELLENT);
pg.hw.playLocalAudio('score_success'); pg.hw.playLocalAudio('score_success');
// 保存录音数据
Game.getIns().player.addVoice(data);
} else if (score >= 60) { } else if (score >= 60) {
// 60-80分,显示不错提示(2星) // 60-80分,显示不错提示(2星)
this.updateTips(TIPS_STATE.NICE); this.updateTips(TIPS_STATE.NICE);
pg.hw.playLocalAudio('score_success'); pg.hw.playLocalAudio('score_success');
// 保存录音数据
Game.getIns().player.addVoice(data);
} else if (score >= 10) { } else if (score >= 10) {
// 10-60分,显示加油提示(1星) // 10-60分,显示加油提示(1星)
this.updateTips(TIPS_STATE.COMEON); this.updateTips(TIPS_STATE.COMEON);
pg.hw.playLocalAudio('score_success'); pg.hw.playLocalAudio('score_success');
// 保存录音数据
Game.getIns().player.addVoice(data);
} else { } else {
// 10分以下,显示再试一次提示(0星) // 10分以下,显示再试一次提示(0星)
this.updateTips(TIPS_STATE.TRYAGAIN); this.updateTips(TIPS_STATE.TRYAGAIN);
...@@ -1173,7 +1200,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -1173,7 +1200,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
if (this.touchData && this.touchData.parent && !this.activeNodes.includes(this.touchData.parent)) { if (this.touchData && this.touchData.parent && !this.activeNodes.includes(this.touchData.parent)) {
this.activeNodes.push(this.touchData.parent); this.activeNodes.push(this.touchData.parent);
} }
Game.getIns().player.addVoice(data);
} }
// 延迟后隐藏提示 // 延迟后隐藏提示
......
...@@ -26,25 +26,24 @@ export const defaultData = { ...@@ -26,25 +26,24 @@ export const defaultData = {
"duration": 30, "duration": 30,
"audioName": "shoot.MP3" "audioName": "shoot.MP3"
}, },
// { {
// "type": "img", "type": "img",
// "audio": "http://staging-teach.cdn.ireadabc.com/a6fb6452f0e383808cd056939ed81917_l.mp3", "text": "circle", "image": "http://staging-teach.cdn.ireadabc.com/d0652838333e892b5e495fbdd8746fa0.png", "audio": "http://staging-teach.cdn.ireadabc.com/a6fb6452f0e383808cd056939ed81917_l.mp3", "text": "circle", "image": "http://staging-teach.cdn.ireadabc.com/d0652838333e892b5e495fbdd8746fa0.png",
// "time": null, "duration": 30, "time": null, "duration": 30,
// "audioName": "levelup.MP3" "audioName": "levelup.MP3"
// }, { }, {
// "type": "txt", "type": "txt",
// "audio": "http://staging-teach.cdn.ireadabc.com/e8d058205a1bc894c55a9dc46d60717f_l.mp3", "text": "shoot", "image": "", "audio": "http://staging-teach.cdn.ireadabc.com/e8d058205a1bc894c55a9dc46d60717f_l.mp3", "text": "shoot", "image": "",
// "time": null, "duration": 30, "time": null, "duration": 30,
// "audioName": "yasuo.mp3" "audioName": "yasuo.mp3"
// }, { }, {
// "type": "txt", "audio": "", "text": "apple", "image": "", "time": null, "duration": 30, "audioName": "" "type": "txt", "audio": "", "text": "apple", "image": "", "time": null, "duration": 30, "audioName": ""
// }, { }, {
// "type": "txt", "audio": "", "text": "international", "image": "", "time": null, "duration": 30, "audioName": "" "type": "txt", "audio": "", "text": "international", "image": "", "time": null, "duration": 30, "audioName": ""
// }, { }, {
// "type": "txt", "audio": "", "text": "deep", "image": "", "time": null, "duration": 30, "audioName": "" "type": "txt", "audio": "", "text": "deep", "image": "", "time": null, "duration": 30, "audioName": ""
// }, { }, {
// "type": "txt", "audio": "", "text": "fun", "image": "", "time": null, "duration": 10, "audioName": "" "type": "txt", "audio": "", "text": "fun", "image": "", "time": null, "duration": 10, "audioName": ""
// } }
] ]
} }
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