My student created a defender game with weapons and enemies. However, we can’t seem to get the weapons to destroy the enemies. We have tried collide, is touching, displace, etc. I condensed the program to one enemy & one weapon (link above), just to try to get it working.
When you run, it loads. Hit space bar to create enemies. Hit “z” to launch fireballs. Everything works to this point. However, when fireballs hit wolves nothing happens. We have not been able to get any response. Have tried nested loops and separate functions. Any help would be greatly appreciated.
I can’t get the program to show a wolf, so I can’t test it, but it looks like a general problem with the way the program is set up is that the program keeps creating new fire sprites, but trying to put them all into the same fire variable. When the latest sprite gets set into a variable, the previous sprite is knocked out, so the collision will only ever work for the latest sprites that have been put into the variable. There is a similar issue with the wolf sprites.
In addition to that, the program creates a new, local ‘fire’ variable inside the dFence function. That variable that you create only exists inside the dFence function, and it’s not the same as the global variable that exists outside of the dFence function. So, in actuality, you are creating a variable to hold your new sprite that you cannot access anywhere else in the program. There is a similar problem with the wolf variable.
This program needs a couple things to work:
- a way to check all collisions between every wolf and every fireball, not just the ones that were most recently created. (this type of functionality usually uses groups, which is outside the scope of CSD)
- a different way of using variables that does not create variables inside a function definition, or if it does, only uses those variables inside the function definition.