CocosCreator中使玩家移动到点击位置

来源:互联网 发布:java框架 编辑:程序博客网 时间:2024/06/06 11:03
onLoad: function () {
    var that = this
    that._playerSpeed = 300
        that.bg.on(cc.Node.EventType.TOUCH_START,function(event){
           //获取当前点击的全局坐标
           var temp = event.getLocation()
           cc.log("点击全局坐标: ",temp.x,temp.y)
           //获取当前点击的局部坐标
           var tempPlayer = that.player.parent.convertToNodeSpaceAR(temp)
           cc.log("点击局部坐标: ",tempPlayer.x,tempPlayer.y)
           //获取当前的玩家的局部坐标
           var po1 = that.player.getPosition()
           cc.log("玩家坐标: ",po1.x,po1.y)
           //对玩家的行走进行左右交换
           if(po1.x < tempPlayer.x){
               that.player.getChildByName("anim").scaleX = -1
           }else{
               that.player.getChildByName("anim").scaleX = 1
           }
           //计算玩家移动的时间
           var playTime = cc.pDistance(tempPlayer,po1) / that._playerSpeed
           //让玩家移动到点击位置
           var action = cc.moveTo(playTime,tempPlayer);
           cc.log("移动时间: ",playTime)
           //移动前停止所有动作
           that.player.stopAllActions()
           //进行移动
           that.player.runAction(action);
           //进行移动动画
           that.anim.play('playerRun')
           //移动完成过后。是玩家进入站立动画状态
           that.player.runAction(cc.sequence(action, cc.callFunc(function(){
                that.anim.play("playerStand")
            })))            
        })
    },
1 1
原创粉丝点击