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.

P#25270 2016-07-13 20:11 ( Edited 2016-07-15 00:28)

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?

P#25271 2016-07-13 20:25 ( Edited 2016-07-14 00:25)

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

Example:

\ \ \
\ \ \
\ \ \

P#25277 2016-07-13 21:52 ( Edited 2016-07-14 01:52)

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

P#25322 2016-07-14 20:28 ( Edited 2016-07-15 00:28)

[Please log in to post a comment]

Follow Lexaloffle:          
Generated 2024-04-18 06:48:46 | 0.014s | Q:15