Commit 839d7efc authored by Tt's avatar Tt

refactor(SSAPP2502): 优化糖果节点布局逻辑并更新默认数据

parent b85c0295
...@@ -174,8 +174,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -174,8 +174,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
private needScroll: boolean = false; private needScroll: boolean = false;
/** 糖果节点宽度 */ /** 糖果节点宽度 */
private candyNodeWidth: number = 0; private candyNodeWidth: number = 0;
/** 待添加的糖果数据队列 */
private pendingCandyData: Option[] = [];
/** 滚动距离累计 */ /** 滚动距离累计 */
private scrollDistance: number = 0; private scrollDistance: number = 0;
// 移除了提示动画相关的变量 // 移除了提示动画相关的变量
...@@ -290,12 +288,12 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -290,12 +288,12 @@ export default class SceneComponent extends MyCocosSceneComponent {
* @param data 节点数据 * @param data 节点数据
* @param index 题目在数组中的索引,用于确定显示哪个糖果图标 * @param index 题目在数组中的索引,用于确定显示哪个糖果图标
*/ */
updateItem(candyNode: CandyNode, data: Option, index: number = 0) { updateItem(candyNode: CandyNode, data: Option, x: number) {
// 设置基本属性 // 设置基本属性
candyNode.data = data; candyNode.data = data;
candyNode.active = true; candyNode.active = true;
candyNode.y = 0; candyNode.y = 0;
candyNode.x = index * 320 + 150; candyNode.x = x;
// 获取子节点 // 获取子节点
const iconNode = candyNode.getChildByName('icon'); const iconNode = candyNode.getChildByName('icon');
const bgFullNode = candyNode.getChildByName('bg_full'); const bgFullNode = candyNode.getChildByName('bg_full');
...@@ -308,7 +306,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -308,7 +306,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 根据索引设置糖果图标(candy_0 ~ candy_4) // 根据索引设置糖果图标(candy_0 ~ candy_4)
if (iconNode) { if (iconNode) {
// 确保索引在有效范围内(0-4) // 确保索引在有效范围内(0-4)
const candyIndex = index % 5; const candyIndex = data.id % 5;
const candyIconName = `candy_${candyIndex}`; const candyIconName = `candy_${candyIndex}`;
// 使用pg.view.setImg加载对应的糖果图标 // 使用pg.view.setImg加载对应的糖果图标
...@@ -352,18 +350,12 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -352,18 +350,12 @@ export default class SceneComponent extends MyCocosSceneComponent {
// 清空现有节点和待添加队列 // 清空现有节点和待添加队列
this.activeNodes = []; this.activeNodes = [];
this.layout.removeAllChildren(); this.layout.removeAllChildren();
this.pendingCandyData = []; // 重置待添加队列
// 初始化显示的节点数量
// 如果剩余题目小于等于4个,显示所有剩余题目;否则显示5个
const initialVisibleCount = unansweredQuestions.length <= 4 ? unansweredQuestions.length : Math.min(5, unansweredQuestions.length);
const visibleData = unansweredQuestions.slice(0, initialVisibleCount);
// 添加初始可见的糖果节点 // 添加初始可见的糖果节点
visibleData.forEach((data, index) => { unansweredQuestions.forEach((data, index) => {
const candyNode = cc.instantiate(this.item) as CandyNode; const candyNode = cc.instantiate(this.item) as CandyNode;
this.layout.addChild(candyNode); this.layout.addChild(candyNode);
this.updateItem(candyNode, data, index); this.updateItem(candyNode, data, index * 320 + 150);
if (index === 0) { if (index === 0) {
this.candyNodeWidth = candyNode.width; this.candyNodeWidth = candyNode.width;
} }
...@@ -379,7 +371,7 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -379,7 +371,7 @@ export default class SceneComponent extends MyCocosSceneComponent {
if (this.layout && this.layout.childrenCount > 0) { if (this.layout && this.layout.childrenCount > 0) {
for (let i = 0; i < this.layout.childrenCount; i++) { for (let i = 0; i < this.layout.childrenCount; i++) {
const node = this.layout.children[i] as CandyNode; const node = this.layout.children[i] as CandyNode;
if (node.active && node.data && node.data.id) { if (node.active && node.data && (node.data.id || node.data.id == 0)) {
displayedIds.push(node.data.id); displayedIds.push(node.data.id);
} }
} }
...@@ -387,52 +379,31 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -387,52 +379,31 @@ export default class SceneComponent extends MyCocosSceneComponent {
return displayedIds; return displayedIds;
} }
/**
* 更新待添加队列,只添加界面上没有显示的题目
*/
private updatePendingCandyData() {
// 获取所有未回答的题目
const unansweredQuestions = this.getUnansweredQuestions();
// 获取界面上已显示的题目ID
const displayedIds = this.getDisplayedQuestionIds();
// 获取待添加队列中的题目ID
const pendingIds = this.pendingCandyData.map(item => item.id);
// 只添加界面上没有显示且不在待添加队列中的题目
const newPendingData = unansweredQuestions.filter(question =>
!displayedIds.includes(question.id) && !pendingIds.includes(question.id)
);
// 将新的题目添加到待添加队列
this.pendingCandyData.push(...newPendingData);
}
/** /**
* 添加新的糖果节点到布局的最后位置 * 添加新的糖果节点到布局的最后位置
* 只有当界面上没有对应题目的节点时才会添加 * 只有当界面上没有对应题目的节点时才会添加
*/ */
private addNewCandyNode() { // private lastid: number = 0;
// 检查是否有糖果节点模板 // private addNewCandyNode() {
if (!this.item) return; // // 检查是否有糖果节点模板
// if (!this.item) return;
// 更新待添加队列 // // 从待添加队列中取出一个数据
this.updatePendingCandyData(); // const data = this.pendingCandyData.shift();
// if (!data) return;
// 从待添加队列中取出一个数据 // const displayedIds = this.getDisplayedQuestionIds();
const data = this.pendingCandyData.shift(); // if (displayedIds.includes(data.id)) return;
if (!data) return; // // 创建新的糖果节点
// const candyNode = cc.instantiate(this.item) as CandyNode;
// 创建新的糖果节点 // this.layout.addChild(candyNode);
const candyNode = cc.instantiate(this.item) as CandyNode; // this.updateItem(candyNode, data);
this.layout.addChild(candyNode); // this.activeNodes.push(candyNode);
this.updateItem(candyNode, data, this.addCount + 4);
this.activeNodes.push(candyNode); // // 如果有足够的节点,启用滚动
// if (this.activeNodes.length > 4) {
// 如果有足够的节点,启用滚动 // this.needScroll = true;
if (this.activeNodes.length > 4) { // }
this.needScroll = true; // }
}
}
/** /**
* 火苗点击事件处理 * 火苗点击事件处理
* 保存触摸数据并显示卡片 * 保存触摸数据并显示卡片
...@@ -496,10 +467,10 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -496,10 +467,10 @@ export default class SceneComponent extends MyCocosSceneComponent {
this.inactiveTimer = 0; this.inactiveTimer = 0;
// 从待添加队列中移除已回答的题目 // 从待添加队列中移除已回答的题目
if (this.touchData) { // if (this.touchData) {
const answeredQuestionId = this.touchData.id; // const answeredQuestionId = this.touchData.id;
this.pendingCandyData = this.pendingCandyData.filter(q => q.id !== answeredQuestionId); // this.pendingCandyData = this.pendingCandyData.filter(q => q.id !== answeredQuestionId);
} // }
// 在滚动列表中查找并隐藏对应题目的节点 // 在滚动列表中查找并隐藏对应题目的节点
if (this.touchData && this.layout && this.layout.childrenCount > 0) { if (this.touchData && this.layout && this.layout.childrenCount > 0) {
...@@ -520,8 +491,6 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -520,8 +491,6 @@ export default class SceneComponent extends MyCocosSceneComponent {
} }
} }
// 更新待添加队列,确保只包含界面上没有显示的未回答题目
this.updatePendingCandyData();
// 检查当前页面是否所有活跃节点都已处理完毕 // 检查当前页面是否所有活跃节点都已处理完毕
if (this.activeNodes.length === 0) { if (this.activeNodes.length === 0) {
...@@ -942,58 +911,26 @@ export default class SceneComponent extends MyCocosSceneComponent { ...@@ -942,58 +911,26 @@ 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.addCount) { if (!this.addCount) {
this.addCount = 1; this.addCount = 1;
this.addNewCandyNode(); // 添加新节点
} else {
// 更新待添加队列,确保只添加界面上没有的题目
this.updatePendingCandyData();
// 如果待添加队列为空,检查是否需要停止滚动
if (this.pendingCandyData.length === 0) {
const unansweredQuestions = this.getUnansweredQuestions();
// 如果未回答题目数量小于等于4,停止滚动并重新刷新列表
// if (unansweredQuestions.length <= 4) {
// this.needScroll = false; // 停止滚动
// this.showPage(); // 重新显示页面
// return;
// }
} }
this.checkRemoveNode();
this.addCount++;
this.addNewCandyNode(); // 添加新节点
} }
} }
/**
* 检查并移除移出视图的节点
*/
private checkRemoveNode() {
// 检查第一个糖果节点是否已经移出视图 // 检查第一个糖果节点是否已经移出视图
const firstCandy = this.layout.children[0]; const children = this.layout.children.concat();
children.sort((A, B) => A.x - B.x);
const firstCandy = 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)) {
const index = this.activeNodes.indexOf(firstCandy);
if (index !== -1) {
this.activeNodes.splice(index, 1);
}
}
// 从布局中移除第一个节点
firstCandy.removeFromParent();
firstCandy.destroy();
// 如果移出的节点对应的题目还未回答,将其重新添加到待添加队列 // 如果移出的节点对应的题目还未回答,将其重新添加到待添加队列
if (removedNodeData) { if (firstCandy) {
const unansweredQuestions = this.getUnansweredQuestions(); const x = children[children.length - 1].x + 320;
const isStillUnanswered = unansweredQuestions.some(q => q.id === removedNodeData.id); firstCandy.x = x;
if (isStillUnanswered && !this.pendingCandyData.some(q => q.id === removedNodeData.id)) {
this.pendingCandyData.push(removedNodeData);
}
}
console.log("当前节点数量" + this.layout.childrenCount);
} }
} }
} }
......
...@@ -8,42 +8,67 @@ ...@@ -8,42 +8,67 @@
* @description 默认游戏数据对象,包含游戏的基本配置和问题数据 * @description 默认游戏数据对象,包含游戏的基本配置和问题数据
* @type {Object} * @type {Object}
*/ */
export const defaultData = { export const defaultData ={
"score": 0, "title": "", "questionText": "", "questionType": "read", "score": 0,
"questionTextAudio": "", "bgAudio": "", "questions": [ "title": "",
"questionText": "",
"questionType": "read",
"questionTextAudio": "",
"bgAudio": "",
"questions": [
{ {
"type": "img", "type": "img",
"audio": "http://staging-teach.cdn.ireadabc.com/ae2538a7ef5de1b4836a882d625271af_l.mp3", "audio": "https://staging-teach.cdn.ireadabc.com/a6a5c388c636bc6d063946e91b4bd21a_l.mp3",
"text": "left", "text": "egg",
"image": "http://staging-teach.cdn.ireadabc.com/41b95f47d23e0712f825eec9e718caed.png", "image": "https://staging-teach.cdn.ireadabc.com/7b315ba7227294d63933cd659d5372fb.png",
"time": null, "time": null,
"duration": 30, "audioName": "heal_money.mp3" "duration": "",
}, { "audioName": "egg.mp3"
},
{
"type": "img", "type": "img",
"audio": "http://staging-teach.cdn.ireadabc.com/4f3bb8f8fa048f40c1bb5d6d2f6d9a33_l.mp3", "audio": "https://staging-teach.cdn.ireadabc.com/c08af4f22c71ad8b3d8786a837787ac9_l.mp3",
"text": "down", "text": "hat",
"image": "http://staging-teach.cdn.ireadabc.com/fbe3d792ce2a72310e5298e1d75d0d71.png", "time": null, "image": "https://staging-teach.cdn.ireadabc.com/9621563afaae7ac0ed01e431a11fa961.png",
"duration": 30, "time": null,
"audioName": "shoot.MP3" "duration": "",
"audioName": "hat.mp3"
},
{
"type": "txt",
"audio": "https://staging-teach.cdn.ireadabc.com/e634280a803a6c9d0b0b51b41f017254_l.mp3",
"text": "lion",
"image": "",
"time": null,
"duration": "",
"audioName": "lion.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": "https://staging-teach.cdn.ireadabc.com/c0926db86deeb3edc9642cb14a516b36_l.mp3",
"time": null, "duration": 30, "text": "horse",
"audioName": "levelup.MP3" "image": "https://staging-teach.cdn.ireadabc.com/40384ac2d06139da212c1f0abf9217c4.png",
}, { "time": null,
"duration": "",
"audioName": "zebra.mp3"
},
{
"type": "img",
"audio": "https://staging-teach.cdn.ireadabc.com/ce3337620f22232dd15c29c66af57736_l.mp3",
"text": "monkey",
"image": "https://staging-teach.cdn.ireadabc.com/340eb5c942ddfbf3fbebace4d14d4472.png",
"time": null,
"duration": "",
"audioName": "monkey.mp3"
},
{
"type": "txt", "type": "txt",
"audio": "http://staging-teach.cdn.ireadabc.com/e8d058205a1bc894c55a9dc46d60717f_l.mp3", "text": "shoot", "image": "", "audio": "https://staging-teach.cdn.ireadabc.com/dea81adc2abe8e7849df33bc952900bc_l.mp3",
"time": null, "duration": 30, "text": "rabbit",
"audioName": "yasuo.mp3" "image": "",
}, { "time": null,
"type": "txt", "audio": "", "text": "apple", "image": "", "time": null, "duration": 30, "audioName": "" "duration": "",
}, { "audioName": "rabbit.mp3"
"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": "fun", "image": "", "time": null, "duration": 10, "audioName": ""
} }
] ]
} }
\ No newline at end of file
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