Как исправить сенсорный запуск кода на Cocos Creator - PullRequest
0 голосов
/ 13 апреля 2020

Я начал изучать Cocos Cretor и TypeScript несколько дней go. Я пытался использовать клавиатуру A для перемещения игрока влево, D для перемещения вправо. Его очень хорошо. Но я не совсем понимаю, как использовать сенсорное событие, когда я нажимаю или касаюсь экрана, чтобы переместиться влево, право не работает. (Я использую 'touchstart', чтобы получить X, расположение Y в порядке). Это мой код:

const {ccclass, свойство} = cc ._ decorator; @ccclass экспорт класса по умолчанию NewClass extends cc .Component {@property jumpHeight: number = 0; @property jumpDuration: число = 0; @property maxMovementSpeed: number = 0; @property accel: число = 0;

jumpAction: any;
accLeft: boolean;
accRight: boolean;
xSpeed: number;

accLeftTouch:boolean=false;
accRightTouch:boolean=false;

// //sound
@property(cc.AudioClip)
jumpAudio:cc.AudioClip = null;

@property
balanceX:number=400;

// LIFE-CYCLE CALLBACKS:
//Hàm hành động nhảy
setJumpAtion(){
    var jumpUp=cc.moveBy(this.jumpDuration,cc.v2(0,this.jumpHeight)).easing(cc.easeCubicActionInOut());
    var jumpDown=cc.moveBy(this.jumpDuration,cc.v2(0,->this.jumpHeight)).easing(cc.easeCubicActionInOut());
    var myCallback=cc.callFunc(this.playJumpAudio,this);
    return cc.repeatForever(cc.sequence(jumpUp,jumpDown,myCallback));
}
playJumpAudio(){
    cc.audioEngine.playEffect(this.jumpAudio,false);
}
//Nhan phím
onKeyDown(event){
    switch(event.keyCode){
        case cc.macro.KEY.a:
            this.accLeft=true;
            //console.log("nhan a");
            break;
        case cc.macro.KEY.d:
            this.accRight=true;
            //console.log("nhan d");
            break;
    }

}
//Nha phím
onKeyUp(event){
    switch(event.keyCode){
        case cc.macro.KEY.a:
            this.accLeft=false;
            //console.log("nha a");
            break;
        case cc.macro.KEY.d:
            this.accRight=false;
            //console.log("nha d");
            break;
    }

}

onLoad () {
    //goi hanh dong nhay, luu vao node
    this.jumpAction=this.setJumpAtion();
    this.node.runAction(this.jumpAction);

    //Mặc định phím ban đầu là ko có gì
    this.accLeft=false;
    this.accRight=false;
    this.xSpeed=0;

    this.accLeftTouch=false;
    this.accRightTouch=false;
    //bat keyboard
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_DOWN,this.onKeyDown,this);
    cc.systemEvent.on(cc.SystemEvent.EventType.KEY_UP,this.onKeyUp,this);
    cc.systemEvent.on(cc.Node.EventType.TOUCH_START,this.onEventStart,this);//


    // this.balanceX=400; 400 is root to device Left Right location
    this.node.on('touchstart',function(event){
        // this.balanceX = event.getLocation().x;
         let worldPointX = event.getLocation().x;
        if(worldPointX<400){
            this.accLeftTouch=true;
            this.accRightTouch=false;//
        }
        if(worldPointX>400){
            this.accRightTouch=true;//
            this.accLeftTouch=false;
        }
     },this);

}

start () {

}

update (dt) {
    //console.log(this.accLeftTouch);
    //this.accLeftTouch = true;
    if(this.accLeft==true||this.accLeftTouch==true){
        this.xSpeed-=this.accel*dt;//toa do x theo speed = giatoc * delta
        console.log('ttt update 1' + this.accLeftTouch + this.accRightTouch + this.xSpeed);
    }
    if(this.accRight == true||this.accRightTouch==true){
        this.xSpeed+=this.accel*dt;
        console.log('ttt update 2' + this.accLeftTouch + this.accRightTouch + this.xSpeed);
    }
    //console.log('ttt update 3:' + this.accel + ':'+ this.xSpeed +':' + dt + ':' +this.xSpeed * dt);

    if (Math.abs(this.xSpeed)>this.maxMovementSpeed){
        this.xSpeed=this.maxMovementSpeed*this.xSpeed/Math.abs(this.xSpeed);
    }
    //console.log('ttt ' + this.xSpeed + this.accLeftTouch + this.accRightTouch);
    this.node.x+=this.xSpeed*dt;
} }

Пожалуйста, помогите мне!

...