Log In  


Just need to put this somewhere, since this always causes a problem when I'm making a game.

To create a table of interactive (that is, something the player can interact with) enemies in my space invaders clone, here's what I do:

1st: In the _init function, create a table, and add the appropriate enemy sprites to it (I iterate over each sprite use 2 "for" loops, one for the in-game y-coord, the other for the in-game x-coord, in that order). Set every sprite's table value to "true".

2nd: In the _update function, create a hitbox in the form of an if statement (if player's projectile hits enemy's boundary). To account for every enemy, use the same "for" loops as described above, in the same order, and add the appropriate values to the x and y coords of the enemy in-game. In the body of the if statement, state that the missile did indeed impact an enemy (done in the form of a boolean value).

3rd: Again in the _update function, check for the boolean value stated above. If true, then set the appropriate enemy sprite's table value to "false", as well as reset the boolean value, and despawn the missile, again with the appropriate boolean.

4th: Adjust above steps as needed for current project.

Will update if needed.



Good idea to write your notes down...I should do the same thing.

Although wondering why you have separate loops for your X and Y?


Because otherwise, the enemies would be cut into lines going at an angle

Example:

\ \ \
\ \ \
\ \ \


You can lay them out using one for...all or foreach loop if you think about it a little differently. Try using % modulo operator.



[Please log in to post a comment]