# Turning off sprites

2

CLIGUI 2024-05-17 18:32 (Edited)

I am working on a simple space shooter, but can not figure out how to turn off the enemy sprites when they are hit.

2024-05-17 18:32

CLIGUI 2024-05-17 18:35

When an enemy is hit the code goes to KO:

I just want to make the enemy sprite disappear and stay gone

McPepic 2024-05-17 20:24

You could use an array to keep track of which sprites are active.
When an enemy is hit, you’d just have to disable the sprite and set the value in the array. Then every time you update the enemies, you first check to see whether it should be active.

CLIGUI 2024-05-17 22:33 (Edited)

Thanks for responding, with a good explanation, but I need to see it because I do not know how to use all the features of an array.

I could also trap the enemy sprites in a loop off screen with something like;

...
IF SPRITE HIT (I, 11 TO 20) THEN EY(I)=-16
...
IF EY(I)<0 THEN EY(I)=-16
WAIT VBL
LOOP

This technique works but I wonder how efficient it is (only two lines at least).

was8bit 2024-05-18 05:12 (Edited)

SPRITE OFF I

was8bit 2024-05-18 05:17 (Edited)

Also

IF SPRITE HIT (I, 11 TO 20) THEN

Will NOT yield all potential hits, only the lowest sprite # will register and all potential hits with higher #’s will be ignored

You need to loop thru each sprite separately…

FOR ISPR=11 TO 20
IF SPRITE HIT (I,ISPR) THEN SPRITE OFF THEN SPRITE OFF I
NEXT ISPR

was8bit 2024-05-18 05:28

An array is simply a variable ….

So you could write

SPR1
SPR2
SPR3

Or you could write

SPR(1)
SPR(2)
SPR(3)

Exact same thing… it’s just different when using them…

So for the first example…
If enemy hits SPR1 then
Else if enemy hits SPR2 then
Else if enemy hits SPR3 thevn

So for second example …

For I=1 to 100
If enemy hits SPR(I) then
Next I

So, for a lot of the same kind things, it reduces the amount of different variable names, assuming they all will be processed precisely the same way, and looping thru all of them makes the code SO much easier to handle :)

was8bit 2024-05-18 05:31

Just remember to initialize the array before you use it …

DIM SPR(100)

Or for strings

DIM WORD\$(100)

To allow it to be used inside of any SUB

DIM GLOBAL SPR(100)

was8bit 2024-05-18 05:40 (Edited)

One trick with complex items for array use, is to control them by an ID#… so if you want to control enemies…

DIM Enemytype(100)
DIM Xenemy(100)
DIM Yenemy(100)
DIM enemyStatus(100)
DIM enemyStrength(100)

So then enemy#1 is represented by the #1 index any array and any data you need to read or change are then easily accessed :)

was8bit 2024-05-18 05:46 (Edited)

The STATUS array above can be used to indicate, 0=Inactive, 1=active, 2=captured, 3=active… Whatever you want….

So, your FOR I=1 to 100 can ignore a status0, but will process a status3….

was8bit 2024-05-18 05:49

If you want a dynamic assignment of enemies, you would use STATUS as 0=not in use and 1 (or higher) = in use…

So when you want to add an enemy, loop thru until you find an unused #, and load that # with a new enemy

And when an enemy is removed from the game, change its status to 0

was8bit 2024-05-18 05:53

I “think” I have an example of dynamic use of arrays for dynamically adding and removing enemies… if you can’t find it, let me know and i will try to find it :)

CLIGUI 2024-05-18 13:04

I got it to work from looking at your enemy handling program, thanks! But I still need to refine it since I did it in a questionable way.

...
IF EO(I)>0 AND SPRITE HIT (0, 1 TO 10) THEN END
LOOP

KO:
EO(I)=0
RETURN

2024-05-18 13:04

CLIGUI 2024-05-18 13:11 (Edited)

Ok, I made it better by making the sprites only move when EO(I)>10 and off when EO(I)=0

So now it reduces the CPU usage too.

was8bit 2024-05-18 17:19 (Edited)

Timo has made a very cool and powerful code to make games with…

there is plenty of ability to craft your code the way you want it, and more than one way to accomplish the same end results ;)