hello扫雷 2024-04-04 20:02 采纳率: 78.4%
浏览 3

回调中调用其他方法方法报错


import { _decorator, Component, instantiate, Node, Prefab, random, tween, Vec3,MathBase, Tween, EventTouch, TweenAction } from 'cc';
const { ccclass, property } = _decorator;


let XA = []; 
let pr1; let X = 0, Y = 0;
@ccclass('main')
export class main extends Component {
    @property(Prefab) P1: Prefab;
    @property(Prefab) P2: Prefab;
    @property(Prefab) P3: Prefab;
    @property(Prefab) P4: Prefab;
    @property(Node) N: Node;

    start() {
        let parr = [this.P1, this.P2, this.P3, this.P4];
        let x1 = -240, y1 = 240;



        for (let j = 0; j < 5; j++) {
            let xa = []
            for (let i = 0; i < 5; i++) {
            let nu = (random() * 3).toFixed();
            const p1 = instantiate(parr[nu]);
            xa.push(p1);
            this.N.addChild(p1);
            p1.setPosition(x1+i*120, y1-j*120);
            this.createpre(p1);}
            XA.push(xa);
        }

    }

    update(deltaTime: number) {
        
    }

    createpre(p1) {

        setTimeout(() => {
            p1.on(Node.EventType.TOUCH_START, (event: TouchEvent) => {
                console.log(p1._siblingIndex, Math.floor((p1._siblingIndex - 1) / 5), ((p1._siblingIndex - 1) % 5));

                if (pr1 == null ) {
                    pr1 = p1;
                    X = p1._lpos.x; Y = p1._lpos.y;
                } else{
                    if (Math.abs(Math.floor((p1._siblingIndex - 1) / 5) - Math.floor((pr1._siblingIndex - 1) / 5)) <= 1 && Math.abs(((p1._siblingIndex - 1) % 5) - ((pr1._siblingIndex - 1) % 5)) <= 1) {
                        XA[Math.floor((p1._siblingIndex - 1) / 5)][((p1._siblingIndex - 1) % 5)] = pr1;
                        XA[Math.floor((pr1._siblingIndex - 1) / 5)][((pr1._siblingIndex - 1) % 5)] = p1;
                        p1._siblingIndex = p1._siblingIndex ^ pr1._siblingIndex;
                        pr1._siblingIndex = p1._siblingIndex ^ pr1._siblingIndex;
                        p1._siblingIndex = p1._siblingIndex ^ pr1._siblingIndex;

                        tween(pr1).to(0.5, { position: p1.position }).call(this.des).start();
                        tween(p1).to(0.5, { position: new Vec3(X, Y, 0) }).call(this.des).start();


                        X = 0, Y = 0; pr1 = null;
                    } else {
                        X = 0, Y = 0; pr1 = null;
                    }
                    
                }
            }, this);
        }, 2000);
    }

    dfs(x, y, no, book) {
        let next = [[-1, 0], [1, 0], [0, -1], [0, 1]];
        let tx, ty;
        for (let i = 0; i < 4; i++) {
            tx = x + next[i][0];
            ty = y + next[i][1];
            if (tx < 1 || ty < 1 || tx > 5 || ty > 5)
                continue;
            if (book[tx][ty] == 0 && XA[tx][ty]._name == no._name) {
                book[tx][ty] = -1; //标记走过
                this.dfs(tx, ty,no,book); //递归
                book[tx][ty] = 0; //取消标记
            }
        }
    }

    des(event: TweenAction) {    
       let book = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]];
       book[Math.floor((event._siblingIndex - 1) / 5)][((event._siblingIndex - 1) % 5)] = -1;


        this.dfs(Math.floor((event._siblingIndex - 1) / 5), ((event._siblingIndex - 1) % 5), event, book);
         

    }

    
}

des方法中调用dfs方法报错

  • 写回答

4条回答 默认 最新

  • Kwan的解忧杂货铺 2024-04-04 20:02
    关注

    晚上好🌙🌙🌙
    本答案参考ChatGPT-3.5

    根据提供的代码,报错信息是在des方法中调用dfs方法时出错。具体的报错信息没有给出,所以无法确定问题的具体原因。但是我可以根据代码逻辑进行分析。

    des方法中,我们可以发现event参数是一个TweenAction对象,而_siblingIndex属性是不存在于TweenAction对象中的。因此,在调用dfs方法时,event._siblingIndex - 1会报错。

    解决这个问题的方法有以下几种:

    1. **在des方法中传递p1参数:**在createpre方法中,将p1作为参数传递到des方法中,而不是将event作为参数传递。这样,dfs方法中就可以直接使用p1对象的相关属性。

    修改后的代码如下:

    // 在 createpre 方法中调用修改后的 des 方法
    this.createpre(p1, pr1);
    
    // 修改 des 方法的参数
    des(p1, pr1) {
      let book = ...;
      book[Math.floor((p1._siblingIndex - 1) / 5)][((p1._siblingIndex - 1) % 5)] = -1;
    
      this.dfs(Math.floor((p1._siblingIndex - 1) / 5), ((p1._siblingIndex - 1) % 5), p1, book);   
    }
    
    1. **在dfs方法中传递event参数的相关属性:**将event的相关属性(如_siblingIndex)作为参数传递给dfs方法,而不是直接传递event对象。这样,dfs方法中就可以直接使用这些属性。

    修改后的代码如下:

    // 在 des 方法中调用修改后的 dfs 方法
    this.des(p1, pr1);
    
    // 修改 dfs 方法的参数
    dfs(x, y, no, book, siblingIndex) {
      let next = ...;
      let tx, ty;
      for (let i = 0; i < 4; i++) {
        tx = x + next[i][0];
        ty = y + next[i][1];
        if (tx < 1 || ty < 1 || tx > 5 || ty > 5)
          continue;
        if (book[tx][ty] == 0 && XA[tx][ty]._name == no._name) {
          book[tx][ty] = -1; //标记走过
          this.dfs(tx, ty,no, book, siblingIndex); //递归
          book[tx][ty] = 0; //取消标记
        }
      }
    }
    
    // 修改 des 方法的参数
    des(event, p1) {    
      let book = ...;
      book[Math.floor((event._siblingIndex - 1) / 5)][((event._siblingIndex - 1) % 5)] = -1;
    
      this.dfs(Math.floor((event._siblingIndex - 1) / 5), ((event._siblingIndex - 1) % 5), event, book, event._siblingIndex);   
    }
    

    请注意,在修改代码时,需要保证其他部分的逻辑正确,并根据具体的需求进行调整。此外,报错信息的提供可以更好地帮助我们分析问题和提供解决方案。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 4月4日

悬赏问题

  • ¥20 Html备忘录页面制作
  • ¥15 黄永刚的晶体塑性子程序中输入的材料参数里的晶体取向参数是什么形式的?
  • ¥20 数学建模来解决我这个问题
  • ¥15 计算机网络ip分片偏移量计算头部是-20还是-40呀
  • ¥15 stc15f2k60s2单片机关于流水灯,时钟,定时器,矩阵键盘等方面的综合问题
  • ¥15 YOLOv8已有一个初步的检测模型,想利用这个模型对新的图片进行自动标注,生成labellmg可以识别的数据,再手动修改。如何操作?
  • ¥30 NIRfast软件使用指导
  • ¥20 matlab仿真问题,求功率谱密度
  • ¥15 求micropython modbus-RTU 从机的代码或库?
  • ¥15 django5安装失败