I’ve been working on a project by myself until ran into a problem where if an enemy spawns the previous enemy only does its last command. I am somewhat new to coding so I don’t expect my code to be good so I figured that someone would know how to fix my issue. The link is here.
Can you give us a specific example of the problem in your game and what you’ve tried to do already to fix it?
I’m not sure I fully understand the issue without a little more explanation of how this applies to the game you created.
thanks,
Mike
Greetings @broxscar,
I don’t know exactly what your problem is, but it looks your enemy spawning and assigning enemy properties seems to be inefficient when you say “the previous enemy only does it’s last command”. You can easily spawn enemies and assign them properties (health, etc.) by following these steps.
- Set up an array (an array is just multiple datas in 1 variable, sort of like a list of data in one variable)
var enemies = [];
- In the block where you spawn your enemies in, you should add that enemy to the “enemies” list like this:
if (ec < el) {
enem = createSprite(200, 200);
enem.setAnimation("Enemy");
enem.setCollider("rectangle", 0, 0, 50, 50); // This line seems a little unnecessary btw
ec = ec + 1;
enemies.push(enem);
}
- Now, we can go through and check the list constantly by using a for loop in your draw loop:
for (var e = enemies.length-1; e > -1; e--) { // This goes through the whole enemy array and checks for properties forever
}
- Now, to assign the properties to your enemies you need to get said enemy in the first place
for (var e = enemies.length-1; e > -1; e--) {
var ee = enemies[e]; // This variable stores the enemy itself
drawSprite(ee); // Note: This is different from drawSprites as it's not plural and is needed for the enemy to appear
}
- You can assign any properties you want like collide, visibility, health, etc!
for (var e = enemies.length-1; e > -1; e--) {
var ee = enemies[e];
drawSprite(ee);
ee.collide(edges)
ee.health = 100;
if (ee.health <= 0) {
ee.destroy();
enemies.splice(e, 1) //If you wanna know what this does, this simply just removes the enemy from the array
}
I didn’t test this out on your project yet because I don’t know what properties you want, but you can ask more questions if needed and I’ll try to answer them!
I think I understand on how this works it’s just when I try to edit the code to be like this it doesn’t work because of drawSprite(ee); and enem.collide(edges);. drawSprite(ee); doen’t work because it drawSprite needs to be called and enem.collide(edges); doesn’t work because for .collide to work both things need to either be a sprite or a group. and I still don’t know enough about arrays and stuff to fix it.
Edit: forgot link
Hello, @broxscar.
It seems like you fixed the drawSprite problem, however I see why .collide doesn’t work.
Firstly, you set the sprite “enem” as one of the targets. You should put ee. Secondly, I didn’t know the syntax edges didn’t count as a sprite. If you want to collide it with edges however, make your own edges through a sprite.
When I also inspected your code, you put the enemies array in the draw loop, meaning that the enemies array will be always empty. This means that no enemies would have no properties, because they weren’t there at all.
In summary, just put the enemy array outside of the draw loop, replace enem in the collide with ee, and making your own edges using sprites (if you still want edge collision).
Sincerely, pluto