Thursday, November 17, 2022
HomeGame DevelopmentHow can I make tutorial in model 3 - Cocos Creator

How can I make [purple monster] tutorial in model 3 – Cocos Creator


I’m learning with the [purple monster] tutorial for model 2.x
However this tutorial is model 2.x
I modified the code for model 3.x

@ccclass('Participant')
export class Participant extends Element {
    @property
    jumpHeight: quantity = 0;
    @property
    jumpDuration: quantity = 0;
    @property
    maxMoveSpeed: quantity = 0;
    @property
    accel: quantity = 0;

    accLeft :boolean
    accRight:boolean;
    xSpeed:quantity;
    setJumpAction() {
        return tween(this.node)
            .repeatForever(
                tween()
                    .by(this.jumpDuration, { place: v3(0, this.jumpHeight, 0) }, { easing: 'cubicOut' })
                    .by(this.jumpDuration, { place: v3(0, -this.jumpHeight, 0) }, { easing: 'cubicIn' })

            );
    }
    setInputControl(){
        enter.on(Enter.EventType.KEY_DOWN,occasion=>{
            change(occasion.keyCode) {
                case KeyCode.ARROW_LEFT:
                    this.accLeft = true;
                    break;
                case KeyCode.ARROW_RIGHT:
                    this.accRight = true;
                    break;
            }
        });
        enter.on(Enter.EventType.KEY_UP,occasion=>{
            change(occasion.keyCode) {
                case KeyCode.ARROW_LEFT:
                    this.accLeft = false;
                    break;
                case KeyCode.ARROW_RIGHT:
                    this.accRight = false;
                    break;
            }
        });
    }
    onLoad() {
        this.accLeft=false;
        this.accRight=false;
        this.xSpeed=0;
        this.setJumpAction().begin();
        this.setInputControl();
    }
    begin() {
    }

    replace(deltaTime: quantity) {
        if(this.accLeft){
            this.xSpeed-=this.accel*deltaTime;
        }else if(this.accRight){
            this.xSpeed+=this.accel*deltaTime;
        }

        if(Math.abs(this.xSpeed)>this.maxMoveSpeed){
            this.xSpeed=this.maxMoveSpeed*this.xSpeed/Math.abs(this.xSpeed);
        }
        this.node.setPosition(this.node.getPosition().add3f(this.xSpeed*deltaTime,0,0));
    }
}

The purple monster doesn’t transfer left or proper after I press the keyboard.
I can transfer left and proper by commenting out [this.setJumpAction().start();]
(After all the purple monster is not going to transfer up and down)

What ought to I repair?


Attempt taking a look at these examples: GitHub – cocos/cocos-example-projects

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments