How to destroy 'double' units on the same lane

0 favourites
  • 7 posts
From the Asset Store
SCI-FI Units SFX contains 456 sounds With this pack, you can voice almost all the units in your sci-fi strategy game
  • I have a game where units are spawned on lanes. Units can spawn both left and right of the screen and will always move into the layout.

    I want to prevent any 'double' units from spawning on the same lane. How would I go about programming this?

    I tried this; but it's destroying the unit sometimes when in the middle of the screen.

    Units spawn every 4 seconds.

    Some more info on the 'system' of spawning;

    I hope it is clear.

  • What's a double unit?

    If you want to prevent two units spawning on the same side consecutively, just alternate between the lanes instead of choosing left or right randomly.

    In general though, you can record where the last unit spawned in an instance variable. Check against that instance variable to make sure the next unit doesn't spawn there, then update it.

  • Thanks for your reply. Yes by 'double' unit I meant when two of the same units spawn on the same Y position. I only want one unit on that lane/Y position at a time.

    In general though, you can record where the last unit spawned in an instance variable. Check against that instance variable to make sure the next unit doesn't spawn there, then update it.

    Could you tell me how to program this? I made this but I think I already had something similar and it isn't working anyway. By the way LaneY is also the name of the instance variable on the Family 'Units';

    P.S. I want to keep the randomization for Left and Right though because that needs to be random, it's part of the gameplay.

    Thanks.

  • Well, I ended up creating an invisible tiled background for each lane.

    Then when any unit of the family is overlapping it and has as the same Y as the lane, it can create an object. I register which lanes are free and occupied.

    If the current lane is chosen but occupied, I call the function again that chooses a random lane which isn't occupied.

    It's probably not an ideal solution but it works for now.

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Your way is most likely fine. The issues would only come if you have a high chance of calling a lane that is already in use - and doing it over and over continuing to fail to find an open lane.

    You could look into creating an array. You would have the array hold all of the currently available lanes. Then grab a int(random(array.length)) position on the array. When you grab that value you remove it from the array. Once the lane opens up again you add it back to the array.

  • Array.Length doesn't exist, did you mean Array.Width? I tried to implement it, it's not working 100% yet and was wondering if there was an error because I used Array.Width?

    Also my objects move at different speeds. Trying to program this makes my mind explode!

  • Yes apologies - .Width is how you can call on the amount of total elements in an Array.

    I have a video that walks through using an array to pick marbles from a bag at random. So each time you pick you should only be able to pick the remaining marbles. You can use this as a guide to get yours going.

    youtube.com/watch

    You could also have other parts of your code effecting your outcome. You can always try building this mechanic on its own - a new program. Name all the objects the same - obviously the graphics dont matter so dont waste time on those. Once you get it working, move it into your other project and see if it still has bugs.

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)