金币飞入动画

413 阅读1分钟

金币飞入动画.gif

image.png

创建金币,飞入

    createGold(num) {
        let goldLabelPosition = this.goldLabel.node.getPosition()
        console.log(goldLabelPosition);
        

        for (let i = 0; i < num; i++) {
            const gold = cc.instantiate(this.gold)
            this.node.addChild(gold)
            const randomX = Math.random() > 0.5 ? Math.random() * 100 : -Math.random() * 100
            const randomY = Math.random() > 0.5 ? Math.random() * 100 : -Math.random() * 100
            gold.setPosition(cc.v3(randomX, randomY))

            cc.tween(gold)
            .to(1, { scale: 0.5, opacity: 0, position: goldLabelPosition})
            .call(() => {
                gold.destroy() // 销毁金币
            })
            .start()
        }

        this.updateGold(num) // 更新金币数量
    }
  • 根据传入num值动态的去生成不定个数的金币
  • 金币的位置使用random动态的去生成,可以限制在一定范围内
  • 动画使用缓动,使用position移动到指定位置,这里是移动到了goldLabelPosition