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")
})))
})
},
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")
})))
})
},
相关文章推荐
- native app 集成 cocos-2dx-js 3.11 (Android篇)
- 【转载】cocos2dx 3.x 声音播放、音量调节
- cocospod遇到的问题
- Expression: pSpriteFrame != 0
- 【步兵 cocos2dx】texturePacker命令行
- quick cocos2d-x 3.5打包安卓apk(编译cocos2d-x的os+打apk)
- Cocos2d-x3.0模版容器详解:cocos2d::Vector<T>
- 总结半年的游戏开发经验
- cocos creator配置表的添加
- cocosSteator整理
- cocosCreator注意点(暂时)
- cocos creator RPG游戏的背景和节点添加
- cocos creator 节点和组件的访问
- cocos creator的事件处理.
- cocos Creat 添加场景
- cocosapods pod install 报错
- <转>Cocos2d-x下Lua调用自定义C++类和函数的最佳实践
- cocos2dx 解决texture packer导出的图片程序中拼接有黑边的问题
- Cocos 2.2.1 + cocos2d-x 3.5 集成protobuf
- CocoStudio制作多分辨率UI布局