0 Favourites

Randomly spawn sprite out of range of target

  • hi,

    anyone have a good idea how to randomly spawn a sprite with turret behavior..but make sure when it spawns it's not in range of the target?

    since the target is moving I assume you need to do some sort of calculation based on target.x and target.y

    I also have wrap behavior for both turret sprite and the target which both move...

  • Just ensure that it's always spawned target.x(+range.x),target.y(+range.y) away from the target? Is the range always the same distance?

  • yes, the range is static. (200 let's say)

    but my layout is 800x800 and if the Target happens to be at pos 780,780 then I can't say Create Object (Turret, Target.x+300, Target.y+300) because it will spawn off screen.

    I need it to calculate going back to zero after the position goes past the layout.x and layout.y

  • The way I would do it.

    Spawn the object off screen. The object would have variables prototypeX and prototypeY. You would perform your placement calculations on those variables and check if they would be acceptable(on screen and out of range of the target).

    If the prototype variables are acceptable then set turret.x and turret.y to the prototype variables. If not redo the placement calculations on the prototype variables until you have acceptable values.

    You might have a system to note why the values were unacceptable and seed the recalculation accordingly.

    You could bypass the prototype variables and just move the turret around until placement is correct, but on the off chance the game slowed down during that moment, the moving turret might be seen.

  • You could also pin an invisible object in front of your player. At the start of layout SpawnerObject.X = Player.X + 300 (or whatever the distance is you want it to be)

    SpawnerObject Pin to Player

    Now the spawner was created the correct number of pixels in front of the player and will stay in that position. You then have your random enemies spawn at the .X position of the spawner object. This ensures they will always be the correct distance from the player. You can then set their Y coordinates to whatever you want (random, or on the floor/ceiling, etc...).

  • Construct 3

    Buy Construct 3

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

    Buy Now Construct 3 users don't see these ads
  • You can then set their Y coordinates to whatever you want (random, or on the floor/ceiling, etc...).

    thanks for the reply

    the only problem with this is my player rotates, so both player coordinates have to be modified.

  • If not redo the placement calculations on the prototype variables until you have acceptable values.

    thanks for the reply..

    so you are saying to create a loop and keep checking until you have acceptable parameters?

    but what about the calculation?

    player.x + turret.range + random(layout.width)

    player.y + turret.range + random(layout.height)

    say my layout is 800x800

    and range is 300

    and the player is at 700,750

    the generated values would have to be approx between 0-400,0-450

    if the player is at pos 20,100 then of course the regular calculation would work...

    I probably have to do a bunch of checking I guess...like:

    if player x is > (layout.width - range) then subtract range instead of adding range etc...

    just wondering if there's an easier way to do it.

  • I would do it something like this.

    Function placeTurret

    Set prototypeX to random(layout.width)

    Set prototypeY to random(layout.height)

    If distance(prototypeX, prototypeY, player.x, player.y) <= turret.range

    ------->call placeturret

    else if prototype variables are outside layout

    ------->call placeTurret

    else set turret x/y to prototypeX/Y

    This way the function calls itself until an acceptable value is found. You would initiate it by calling this function upon creating the turret.

  • I would do it something like this.

    If distance(prototypeX, prototypeY, player.x, player.y) <= turret.range

    ahh using the 'distance' function.. I haven't used that before, cool thanks..

    but about your: "If, else if, else"

    I wasn't aware you could do "else ifs" and then another nested "else" from the same event.. I actually tried, but maybe I am doing something wrong? I thought it was a limitation of C2...

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