分享一个领取时金币飞向目标区域的小功能

直接贴代码 注释写好了 每个阶段都可调整 做出你想要的的效果

/**
 * 飞的金币预制
 */
@property(cc.Prefab)
private coinpre: cc.Prefab = null;
/**
 * 目标节点
 */
@property(cc.Node)
coinNode: cc.Node = null;
/**
 * 生成金币个数
 */
private createcoin: number = 20
/**
 * 随机范围(random1~random2之间)
 */
private random1: number = -200
private random2: number = 200
/**
 * 生成到赋予位置的时间
 */
private createTime: number = 0.15
/**
 * 停留时间
 */
private standingTime: number = 0.2
/**
 * 金币移动速度
 */
private coinSpeed: number = 1000

onPlayCoinAni(touchPos: cc.Vec2, callback: Function) {
    let tempPlayer = this.node.convertToNodeSpaceAR(touchPos)
    for (let i = 0; i < this.createcoin; i++) {
        let pre = cc.instantiate(this.coinpre)
        pre.parent = this.node
        pre.setPosition(tempPlayer)
        let rannumx = Math.floor(Math.random() * (this.random2 - this.random1 + 1) + this.random1)
        let rannumy = Math.floor(Math.random() * (this.random2 - this.random1 + 1) / 1.5 + this.random1 / 1.5)
        pre.runAction(cc.moveBy(this.createTime, rannumx, rannumy))
        this.scheduleOnce(() => {
            pre.stopAllActions()
            let finshend = cc.callFunc(function () {
                pre.destroy()
                this.coinNode.getComponent(cc.Animation).play()
                if (i == this.createcoin - 1) {
                    //结束
                    this.scheduleOnce(() => {
                        callback()
                    }, 0.5)
                }
            }, this);
            let pos = pre.getPosition()
            let coinpos = this.coinNode.getPosition()
            let playTime = pos.sub(coinpos).mag() / this.coinSpeed
            pre.runAction(cc.sequence(cc.moveTo(playTime, coinpos.x, coinpos.y), finshend))
        }, this.standingTime + this.createTime);

    }
}
27赞

ctrl+cctrl+v

mark

callback()是什么?

回调函数,金币动作执行完成后的回调。可以这么调用

this.btn.on(cc.Node.EventType.TOUCH_END, (event: cc.Event.EventTouch) => {
this.onPlayCoinAni(event.getLocation(), () => {
//执行加金币操作

        })
    })

this.coinNode.getComponent(cc.Animation).play();
这个身上的animation是什么动作?

animation是用到的动画属性,是用于一个动画的播放、暂停之类的

this.coinNode.getPosition()是终点的节点么?

js怎么写?我改了,那个最后的放大缩小是this.coinNode.getComponent(cc.Animation).play();这个动画么?

this.coinNode.getComponent(cc.Animation).play();这个身上的animation是什么动作?
这个是目标区域那个金币的一个放大缩小的简单动画,意义是:有金币飞到那里就播放一次动画。 一个小表现,看您需求,可删除。
this.coinNode.getPosition() 。this.coinNode是终点的节点,在注释里有写哈。 这段代码是获取终点的局部坐标

js:改一下声明属性,声明变量的语法,初始化一下全局变量。其他没啥改的了

还有什么问题吗 行吧

写完直接飘外太空去了NewProject_21.rar (434.8 KB)

您好 刚看了您的demo,有两个问题 1.这里接收的是点击位置的世界坐标哈。
2.这个coinnode因为是获取的局部坐标 所以要和生成的金币是同级的,如果您要像demo一样把这个节点放在上一层的话,这里需要转换一下坐标哈。 我刚测试过了,这两个问题修改后就可以正常运行了。

NewProject_21.rar (421.5 KB) 这是修改之后的demo

哇塞~感谢!!!!!!

cv!!!

mark

谁给改进一下,走直线不好看啊

cc.jumpto