if I add the line this.setVelocity(10, 0); after line:9 in the above link then the game won’t run.
I also tried adding scene.add.existing(this); scene.physics.add.existing(this);
in the constructor if it’s needed.
Assuming you want to use Arcade Physics: this.setVelocity only exists on an Arcade Image or an Arcade Sprite. For any other Game Object, you have to use this.body.setVelocity after adding it to the physics world with scene.physics.add. You also need to enable Arcade Physics.
export default abstract class Bullet extends Phaser.Physics.Arcade.Sprite {
private damage:integer
private v
public team:number
constructor(scene: Phaser.Scene, x:number, y:number, damage:number, team:number, image:string, angle:number, velocity:number) {
super(scene, x, y, image)
scene.add.existing(this);
scene.physics.add.existing(this);
this.damage = damage
this.team = team
this.setAngle(angle)
let v = new Phaser.Math.Vector2(1, 1);
v.setAngle(this.rotation)
v.setLength(velocity)
this.setVelocity(this.v.x, this.v.y)
}
update(t:number, dt:number) {
super.update(t, dt)
// this.setVelocity(this.v.x, this.v.y)
}
}
If I comment this.setVelocity(this.v.x, this.v.y) in the constructor and add it in the update() then the sprite moves as excepted. But why do I need to setVelocity on every update?
@samme thanks for clarifying
I am using ts so this.bullets.defaults = {};is showing me error.
So I created a callback when defining the groups like this
Then I set the velocity and other physics properties in the create() to get around this.
Anyways, how does overwriting the values set by contructor with default values make sense?
This will be so non-trivial to figure out if you don’t know all ins and outs of phaser