角色的移动和转身

本文介绍如何使用 Cocos2d-x 实现玩家角色跟随触摸位置移动的效果,包括通过触摸事件获取坐标、调整角色动画方向及平滑移动至目标位置等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

properties: {
       bg: cc.Node,
       anim: cc.Animation,
       player : cc.Node,
    },

    // use this for initialization
    onLoad: function () {
        var num = 200
        var that = this
        this.bg.on(cc.Node.EventType.TOUCH_START,function(event){
           var pos = that.player.parent.convertToNodeSpaceAR(event.getLocation())
           var playerPos =that.player.getPosition()
           cc.log( pos,that.player.getPosition())
           if (playerPos.x<pos.x){
           that.player.getChildByName("anim").scaleX = -1
           }
           else {
               that.player.getChildByName("anim").scaleX = 1
           }
           var time = cc.pDistance(pos,playerPos)/num
           var moveto = cc.moveTo(time,pos)
           that.player.stopAllActions()
           that.player.runAction(moveto)
           that.anim.play('plyerRun')
           clearTimeout(that.timeOut)
           that.timeOut = setTimeout(function () {
               that.anim.play('plyerAnima')
           },time*1000)
        })
       
        // for (var i = 0;i<=113)
       
       
       
    },

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值