CocosCreator中的键盘和屏幕触摸事件

1. 通过键盘ADWS 控制物体的八方向移动

export default class TestMove extends cc.Component 
{
  
    //274,410(边界)
    isLeft:boolean;
    isRight:boolean;
    isForward:boolean;
    isBehind:boolean;
    
    moveSpeed:number=400;

    start ()
    {
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.keyDownEvent,this);
        cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.keyUpEvent,this);

    }
     
    update(dt)
    {
        if (this.isLeft)
        {
            this.node.x-=this.moveSpeed*dt;
            if(this.node.x<=-274)
            {
                this.node.x=-274;
            }
        }
         if(this.isRight)
        {
            this.node.x+=this.moveSpeed*dt;
            if(this.node.x>=274)
            {
                this.node.x=274;
            }
        }
         if(this.isForward)
        {
            this.node.y+=this.moveSpeed*dt;
            if (this.node.y>410)
            {
                this.node.y=410;
            }
        }
         if(this.isBehind)
        {
            this.node.y-=this.moveSpeed*dt;
            if(this.node.y<-410)
            {
                this.node.y=-410;
            }
        }
    }

    keyDownEvent(event)
    {
        switch(event.keyCode)
        {
            case cc.macro.KEY.a:
                 this.isLeft=true;
            break;
            case cc.macro.KEY.d:
                 this.isRight=true;
            break;
            case cc.macro.KEY.w:
                 this.isForward=true;
            break;
            case cc.macro.KEY.s:
                 this.isBehind=true;
            break;

        }
    }

    keyUpEvent(event)
    {
        switch(event.keyCode)
        {
            case cc.macro.KEY.a:
                 this.isLeft=false;
            break;
            case cc.macro.KEY.d:
                 this.isRight=false;
            break;
            case cc.macro.KEY.w:
                 this.isForward=false;
            break;
            case cc.macro.KEY.s:
                 this.isBehind=false;
            break;
        }
    }
   
    onDestroy()
    {
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_DOWN,this.keyDownEvent,this);
        cc.systemEvent.off(cc.SystemEvent.EventType.KEY_UP,this.keyUpEvent,this);
    }

}

再Start 方法里面注册监听键盘按下的事件,因为是2d的,所以四方向,只需更改对应的分坐标即可。

 

2.在cocosCreator中,鼠标左中右键的监听事件如下:

 start () 
    {
        this.node.on(cc.Node.EventType.MOUSE_DOWN,this.mouseEvent,this);
    }

    mouseEvent(event)
    {
        if (event.getButton()==0)
        {
            //console.log("left mouse");
            let pos=event.getLocation();
            //this.targetNode.position=  this.node.convertToNodeSpaceAR(pos);
            this.targetPos=this.node.convertToNodeSpaceAR(pos);
        }
        if(event.getButton()==2)
        {
            //console.log("right mouse");

        }
        if(event.getButton()==1)
        {
            //console.log("Middle mouse");
        }
    }

3.移动端触摸事件

常用的api如下,之后会使用这些api 写一些常见的人物控制方式(下一篇文章):

 

  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值