Cocos Creator中角色移动案例

一、处理情景

用键盘和按钮控制角色的左右移动,涉及到的点:
1.左右移动时(人物转向),spriteFrame的切换;
2.移动时音频的调用;
3.鼠标键盘控制,类之间方法的调用。

二、场景和资源

资源层级图
场景图

三、处理方法

首先,处理左右移动时spriteFrame的切换。因此,要引入两个cc.SpriteFrame对象存储两种不同的spriteFrame。
然后,涉及到音频的调用,需要引入一个cc.AudioClip对象存储音频信息。

	//用于存储向左和向右移动的SpriteFrame
	@property(cc.SpriteFrame)
    moveLeftSpriteFrame : cc.SpriteFrame = null

    @property(cc.SpriteFrame)
    moveRightSpriteFrame : cc.SpriteFrame = null
	
	@property(cc.AudioClip)
    audio : cc.AudioClip = null

对应的sprite-frame存储
最后,使用Cocos Creator的键盘按键事件和鼠标事件。Cocos Creator可接受的事件可参考《Cocos Creator的生命周期和支持的系统事件》

案例涉及到的核心代码:
所有代码:
Gamer Script文件

const {ccclass, property} = cc._decorator;

@ccclass
export default class NewClass extends cc.Component {

    @property(cc.AudioClip)
    audio : cc.AudioClip = null

    @property(cc.SpriteFrame)
    moveLeftSpriteFrame : cc.SpriteFrame = null

    @property(cc.SpriteFrame)
    moveRightSpriteFrame : cc.SpriteFrame = null

    @property("number")
    step : number = 20
    // LIFE-CYCLE CALLBACKS:

    onLoad () {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN, this.keyDownForMove, this)
    }

    moveLeft() {
        let gamerSprite : cc.Sprite = cc.find("Canvas/Gamer/toward").getComponent(cc.Sprite)
        if(this.moveLeftSpriteFrame !== null) gamerSprite.spriteFrame = this.moveLeftSpriteFrame
        this.node.x -= 20
        if(this.audio !== null) cc.audioEngine.play(this.audio, false, 1)
    }

    moveRight() {
        let gamerSprite : cc.Sprite = cc.find("Canvas/Gamer/toward").getComponent(cc.Sprite)
        if(this.moveRightSpriteFrame !== null) gamerSprite.spriteFrame = this.moveRightSpriteFrame
        this.node.x += 20
        if(this.audio !== null) cc.audioEngine.play(this.audio, false, 1)
    }

    keyDownForMove(evt) {
        if(evt.keyCode === 37) {
            this.moveLeft()
        } else if(evt.keyCode === 39) {
            this.moveRight()
        }
    }
}

moveLeft Script文件(moveRight Script道理是一样的):

const {ccclass, property} = cc._decorator;
import GamerScript from "./Gmaer Script"

@ccclass
export default class MoveLeft extends cc.Component {

    @property(cc.Label)
    label: cc.Label = null;

    // LIFE-CYCLE CALLBACKS:
    onLoad () {
        this.node.on("mousedown", this.onMouseDown, this)
    }

    onMouseDown() {
        let GamerClass = cc.find("Canvas/Gamer").getComponent(GamerScript)
        GamerClass.moveLeft()
    }
}

学习记录一下,如有错误和不足,欢迎各位大哥指正!

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Cocos Creator是一款基于Cocos2d-x游戏引擎的游戏开发工具,它提供了丰富的API和功能来开发移动端和Web游戏。在Cocos Creator实现路径点移动,可以通过以下步骤进行操作: 1. 创建路径点:在场景创建一个节点作为路径点,可以使用Sprite组件来可视化路径点。通过设置节点的position属性,可以确定路径点在场景位置。 2. 创建移动对象:在场景创建一个需要进行路径移动的对象,可以是玩家角色、敌人或其他游戏元素。添加相应的脚本组件来控制对象的移动。 3. 设置路径点移动逻辑:在移动对象的脚本组件,设置路径点移动的逻辑。可以使用定时器或帧事件来控制移动对象的移动速度和路径点的切换。 4. 实现路径点的移动:在移动对象的脚本组件,使用Cocos Creator提供的移动方法(例如cc.moveTo、cc.moveBy)来实现从当前位置到下一个路径点的移动。 5. 路径点的切换:在移动对象的脚本组件,根据移动对象当前位置和路径点之间的距离,判断是否需要切换到下一个路径点。当移动对象接近当前路径点时,通过代码设置移动对象的目标位置为下一个路径点的位置。 6. 循环移动:根据游戏需求,可以设置移动对象是否需要循环移动路径点。如果需要循环移动,当移动对象到达最后一个路径点时,将其目标位置设置为第一个路径点的位置。 通过以上步骤,在Cocos Creator可以实现路径点移动的效果。可以根据具体的游戏需求进行调整和扩展,添加更多的逻辑和动画效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值