I am studying with the [purple monster] tutorial for version 2.x
But this tutorial is version 2.x
I changed the code for version 3.x
@ccclass('Player')
export class Player extends Component {
@property
jumpHeight: number = 0;
@property
jumpDuration: number = 0;
@property
maxMoveSpeed: number = 0;
@property
accel: number = 0;
accLeft :boolean
accRight:boolean;
xSpeed:number;
setJumpAction() {
return tween(this.node)
.repeatForever(
tween()
.by(this.jumpDuration, { position: v3(0, this.jumpHeight, 0) }, { easing: 'cubicOut' })
.by(this.jumpDuration, { position: v3(0, -this.jumpHeight, 0) }, { easing: 'cubicIn' })
);
}
setInputControl(){
input.on(Input.EventType.KEY_DOWN,event=>{
switch(event.keyCode) {
case KeyCode.ARROW_LEFT:
this.accLeft = true;
break;
case KeyCode.ARROW_RIGHT:
this.accRight = true;
break;
}
});
input.on(Input.EventType.KEY_UP,event=>{
switch(event.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().start();
this.setInputControl();
}
start() {
}
update(deltaTime: number) {
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 does not move left or right when I press the keyboard.
I can move left and right by commenting out [this.setJumpAction().start();]
(Of course the purple monster will not move up and down)
What should I fix?