How to enable target tracking for more then one

I’m currently making a fun plane game for school, the coding is messy as it is my first time doing this. I am currently confused on why missiles (the little things that follow your y axis) stop tracking targets after another missile spawns. If anyone can help, thank you!

2 Likes

for some reason I can’t move on mobile.

Looking at your code @2cool4u The problem is that every enemy missile shares the same global variable (ENEMY_MISSILE), so whenever a new missile is spawned, ENEMY_MISSILE is reassigned to the newest sprite. After that, all of your forever loops are only updating that newest missile, and the older missiles are abandoned

Specifically:

ENEMY_MISSILE = sprites.create(...)

creates a new missile, but overwrites the old reference.

Then later:

forever(function () {
    ENEMY_MISSILE.y = tracked_target_pathfind
})

only affects whatever missile was created most recently.

A much better approach is to update all tracking missiles:

forever(function () {
    for (let missile of sprites.allOfKind(SpriteKind.tracking_missile)) {
        missile.y = tracked_target_pathfind
    }
})

or even better, give each missile its own tracking logic when it’s created instead of relying on one global variable.

I also noticed a couple of other things:

  • The for (let index = 0; index < 1; index++) loops don’t do anything useful—you can just remove them and create the sprite directly.
  • This loop is suspicious:
for (let ENEMY_MISSILE2 of sprites.allOfKind(SpriteKind.tracking_missile)) {
    ENEMY_MISSILE2 = sprites.createProjectileFromSprite(...)
}

Assigning to ENEMY_MISSILE2 doesn’t replace the sprite in the array—it just changes the local variable, so it probably isn’t doing what you expect.

So the issue isn’t really that you’re recreating sprites in game.onUpdate (you’re actually spawning them every 5 seconds). It’s mainly that you’re storing every missile in the same variable, causing only the newest missile to continue tracking.

If you’re using Blocks, you could either:

  • update all tracking missiles in a loop (sprites.allOfKind(...)) instead of just ENEMY_MISSILE, or
  • if you’re comfortable switching to JavaScript for a small part, replace the code that updates ENEMY_MISSILE.y with:
for (let missile of sprites.allOfKind(SpriteKind.tracking_missile)) {
    missile.y = tracked_target_pathfind
}

aw man :anxious_face_with_sweat:

i didnt test on mobile, only computer

I’m on mobile u can move but the background doesn’t move so it doesn’t seem like it to prove my point move forwards while shooting with a the projectiles can’t keep up but if you just shoot they got forwards

Thank you so much! I’ve done some work and figured out of fixes. Is there anything else i can do to better optimize code?

try the missiles, they are transferred less momentum, so it’d be more obvious

1 Like

ngl this is very fun

Thank you! I’m just trying to get mechanics working rn, hopefully soon a full pledged game will come

2 Likes

You could check this Game thingy I made a few months ago

Is actually pretty good I needed to make better path finding for the ai to fight each other too

1 Like

yo, this has some cool features such as rotation and the minimap, awesome work!

hey you can make that the misiles face torward the jet with the oficial sprite fix extension, it would be cool!