Confused about physics (specifically velocity)

Hey everyone, I held off on this for a while but I can’t seem to wrap my head around it.

I have an arrow class (that extends sprite) that’s supposed to fly through the air when created. If I code the following in create():

create(){
...
this.cls_arrow = new Arrow(this)
this.cls_arrow.body.setVelocity(-500, -20)
...
}

It works as intended. However I want to put this in a reusable function, and this is where the issue comes up. If I have the exact same thing in a function outside of the create() function such as the following:

shootArrow(){
this.cls_arrow = new Arrow(this)
this.cls_arrow.body.setVelocity(-500, -20)
this.arrows.add(this.cls_arrow)
}

The arrow’s velocity instantly drops to 0, and the arrow simply falls to the ground. Needless to say I’m at a loss as to why the same code doesn’t work just because it’s put inside a function.

Any insight would be appreciated.
Thanks.

Edit: O̶u̶t̶ ̶o̶f̶ ̶b̶l̶i̶n̶d̶ ̶t̶e̶s̶t̶i̶n̶g̶ ̶I̶ ̶a̶d̶d̶e̶d̶ ̶a̶n̶ ̶x̶/̶y̶ ̶v̶e̶l̶o̶c̶i̶t̶i̶e̶s̶ ̶t̶o̶ ̶t̶h̶e̶ ̶A̶r̶r̶o̶w̶’̶s̶ ̶c̶o̶n̶s̶t̶r̶u̶c̶t̶o̶r̶ ̶p̶a̶r̶a̶m̶e̶t̶e̶r̶s̶ ̶(̶a̶n̶d̶ ̶t̶h̶e̶n̶ ̶s̶e̶t̶ ̶t̶h̶i̶s̶.̶b̶o̶d̶y̶.̶v̶e̶l̶o̶c̶i̶t̶y̶.̶x̶/̶y̶ ̶t̶o̶ ̶t̶h̶e̶s̶e̶ ̶p̶a̶r̶a̶m̶e̶t̶e̶r̶s̶)̶ ̶a̶n̶d̶ ̶i̶t̶ ̶w̶o̶r̶k̶e̶d̶. Turns out that adding the arrow to the physics group of arrows resets its velocity to 0. So now I have

build_arrow(x_vel, y_vel){
this.cls_arrow = new Arrow(this, x_vel, y_vel)
this.cls_arrow.hit_knight = false
this.arrows.add(this.cls_arrow)
this.cls_arrow.body.setVelocity(x_vel, y_vel)
}

which definitely does not look pretty, but I guess it gets the job done. If there’s a better way I’m all ears.

I am not an expert but main thing it gets the job done?
But I think in your first example you lose this somewhere on the line so its basically gets undefined.
and you need to add existing somewhere as well maybe? when init the arrow in the scene

That was my first thought, and I put a console.log() in both the build arrow function and the update() function. When the arrow was created, it had the proper velocity. But the moment that arrow was added to the arrows group, both its X velocity and Y velocity were set to 0.

But as we’ve said, at least it’s working now.

yes, the phaser is really well written… to be honest, and the documentation is simple and clear…
I’m sorry, I couldn’t point u in the correct direction then… but after 10 years you will find out that some hacks are needed and far more easy then understand why the normal way doesn’t do the job =)
Do you have any clue how to generate random objects on a tile map perhaps?