您的位置:首页 > 移动开发 > Cocos引擎

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

2016-07-19 15:33 381 查看
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")

            })))            

        })

    },
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: