spriteFrame图片数组用splice操作后报错问题

发现一个奇怪的现象:
把一组图片保存在一个数组里,用数组[index]方式来赋值给一个节点,显示正常。现在我想用随机数的方式取其中一个数组元素,用到数组的splice()后,再设置给节点,就一直报错。
有大佬知道原因吗?


你这样不是把原数组给修改了吗

我就是要把原数组中取出一个,下次再取就不会取到这个。
防止重复取到同一个图

看起来像是资源被释放了,检查一下吧

怎么检查呢?求指导

用temp接收一下splice后的结果,都是SpriteFrame……,就是赋值给Component后就不对了。直接从spriteFrame数组里取也可以,太奇怪了。
image

你展开看 spriteFrame 的 texture 还在不在呀

建议上代码

好像是splice的问题,之前在微信小游戏开发软件上也有问题……

从代码来查, 取出来的rect 与 uv 是个undefined, 像是spriteFrame绑定的texture不存在了


image

打印出来看,都在的啊~

那就只能上传你的工程看看了

上代码吧 多半是使用的问题 换个方式吧,打乱你的数组,然后从0开始取sprireFrame一样的

@ccclass(‘test’)

export class test extends Component {

start() {

    setTimeout(() => {

        console.log('111111111');

        console.log('pic',find('Canvas').getComponent(NewComponent).pic);

        let randonNun = randomRangeInt(0,find('Canvas/node').getComponent(NewComponent).pic.length)

        this.getComponent(Sprite).spriteFrame = find('Canvas').getComponent(NewComponent).pic.splice(randomRangeInt(0,randonNun),1) //这个报错

        // this.getComponent(Sprite).spriteFrame = find('Canvas').getComponent(NewComponent).pic[0]  //这个可以

    }, 5000);

}

update(deltaTime: number) {

   

}

}

@ccclass(‘NewComponent’)

export class NewComponent extends Component {

pic = []

start() {

    console.log('2333333333333');

   

    for (let index = 1; index < 31; index++) {

        resources.load(index + '/spriteFrame', SpriteFrame, (err, spriteFrame) => {

            this.pic.push(spriteFrame)

        })

    }

}

update(deltaTime: number) {

}

}

节点目录
image

上了,大神看一眼

splice返回值不是一个数组吗
你的 pic.splice(x , 1) // 应该是 [SpriteFrame]
相当于你直接
A.getComponent(Sprite).spriteFrame = [SpriteFrame]
肯定不行的
你试试

start() {

    setTimeout(() => {

        console.log('111111111');

        console.log('pic',find('Canvas').getComponent(NewComponent).pic);

        let randonNun = randomRangeInt(0,find('Canvas/node').getComponent(NewComponent).pic.length)

        this.getComponent(Sprite).spriteFrame = find('Canvas').getComponent(NewComponent).pic.splice(randomRangeInt(0,randonNun),1)[0] //这里加一个 [0]

        // this.getComponent(Sprite).spriteFrame = find('Canvas').getComponent(NewComponent).pic[0]  //这个可以

    }, 5000);

}

image

但是数组里放的是spriteFrame对象啊

哈哈哈哈,他说的是 splice 出来的是一个 数组。

可以!!!!这个bug太隐蔽了!!!