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.