i want to iterate throug an Array of Objects and use a function from the Objects in the array. I'm coming from JAVA and there it's pretty normal to do that. For that reason i guess it works in JS as well but i'm not sure how.
My Code looks like this:
class Whateever {
constructor(ctx){
this.ctx
this.shotsFired = [];
}
onButtonPush(){
let shot = new Shot(this.position.x, this.position.y);
this.shotsFired.push(shot);
}
//called every frame
update(){
for (let shot in this.shotsFired) {
shot.update(); <-- I want to call this function
shot.draw(this.ctx); <-- and that function
}
}
--
class shoot{
constructor(x,y){
this.position = {
x : x,
y : y
};
}
update(){
// Do nothing
}
draw(ctx){
// do nothing
}
}
But with this construct i get always the error message:
Whateever.js:40 Uncaught TypeError: shot.draw is not a function
at Whateever.update (ship.js:40)
at gameLoop (index.js:23)
The shot.update() function works by itself.
I just want to know how to call object functions through an loop in an array?
This is an simplyfied snippit from a big class. If i would post the whole class it would be confusion. Question is absolute clear!
Array of Objects -> Iteration -> during Iteration call of functions from the Objects.
If u need an example i can post JAVA Code the make clear what i mean.