Skip to content
Sign UpLog In
Profile icon

Pear Pie

@PearPie123
Repls
Community
PearPie123
PearPie123
shared a Post
2 years ago
How to iterate over all members in a class-javascript
I'm trying to create an enemy class that has a method that updates it.(turn to the player, shoot, etc) In my update loop, I want to call this method o
Phaser
HTML, CSS, JS
PearPie123
PearPie123
Thank you so much! I can't believe I didn't think of that, though. 2 years ago
PearPie123
PearPie123
@CSharpIsGud I think you misunderstood me. I don't want to loop over the properties of my objects, I want to loop over the objects in my class. So for example, I have 4 objects of my class testClass. I want to call a function for each of those objects, which updates that object. I would imagine the code to be like this: ` for (object in class){ object.doSomething(); } ` Thank you for your response, though.2 years ago
CSharpIsGud
CSharpIsGud
Try for (let obj of Object.values(testClass)) { } `2 years ago
PearPie123
PearPie123
shared a Post
2 years ago
So I have this class called 'players' and inside I have a method 'draw'. It draws a rectangle on the canvas based on four variables x1,y1,x2,y2. When
PearPie123
PearPie123
Hmm, now I got an error saying "cannot read property 'code' of undefined" I take it to mean that the event parameter is undefined and has no .code variable.2 years ago
Coder100
Coder100
hi!! You are calling it inside a function, so this becomes the function. Solution I would change the function like this: class player { // ... wasd(event) { if (event.code == "...") { /* ... */ } // ... } } let test = new player(1); test.draw(); document.addEventListener("keydown", test.wasd); Also, as a quick sidenote, it is good practice to have your classes have uppercase: Player instead of player2 years ago