I’m trying to make an object move by itself. But no matter what I try, nothing works.
create {
let sprite = this.physics.add.sprite(470, 450, '"sprite');
}
update {
sprite.body.setVelocityX(200);
}
or
create {
let sprite = this.physics.add.sprite(470, 450, '"sprite');
}
update {
sprite.x += 2;
}
Milton
July 18, 2021, 9:05am
2
You get no errors? Change ‘let sprite
’ to ‘this.sprite
’ (so no ‘let
’), and then use ‘this.sprite
’ in update.
this did not work, the sprite began to sink into the ground. I forgot to say that
there is an error: “the sprite is not defined”
Milton
July 18, 2021, 9:51am
4
But no more error right? I guess the sinking is just gravity (and no collision).
yes, not anymore. but I don’t understand how to change it. can you show me, please?
Milton
July 18, 2021, 10:13am
6
Change what? To add collision follow this tutorial (specifically Part 3 - World Building).
thank you, but that’s not what I’m talking about. I say can you show the correct version of the code, because I don’t understand what I need to change
Milton
July 18, 2021, 10:19am
8
create {
this.sprite = this.physics.add.sprite(470, 450, '"sprite');
}
update {
this.sprite.body.setVelocityX(200);
}
You shouldn’t set velocity every frame though…
I thought you already did this, and then the sprite sunk?
I don’t know, maybe. even when I moved to create, he still fell into the ground. if this is not necessary in every frame, then need to put the code in “create”?
Milton
July 18, 2021, 10:52am
10
Maybe have a look at this example.