How do I make several sprites form a formation

  • So ive been trying to figure this one out for quite some time now, from paper to construct, but it just doesnt work

    I thought about pinning but firstly i need to get those sprites in formation somehow (lets say 9 sprites 3x3) i would like to achive that without click and drag.

    The main problem is actually that i want formations to be changable like choosing how many rows i want (if i pick 4 rows,2 sprites should leave last two rows and form the fourth row)

    I ve tried creating formations by firstly spawning sprites next to each other with a help of a small equation but i quickly encourted several problems, i thought about maybe making a "squad" container which would hold spots for soldier sprites but havent had the chance to test it yet.

    So does anyone have any ideas or some refrences or examples they could point me towards, i did quite a bit of reaserch but not many helpful things have come up

    Anyway thanks in advance.

  • You can create objects in a formation by using two loops

    start of layout
    for "x" from 0 to 3
    for "y" from 0 to 3
    --- create sprite at (loopindex("x")*32, loopindex("y")*32) [/code:egxi6tbt]
    
    You can control where the topleft of the formation is by adding it in so say you wanted the top left to be (100,150) you could use (loopindex("x")*32+100, loopindex("y")*32+150).
    
    you can also make the formation get created around a center.
    [code:egxi6tbt]
    global number rows=4
    global number cols=4
    global number centerx=100
    global number centery=150
    
    start of layout
    for "x" from 0 to cols-1
    for "y" from 0 to rows-1
    --- create sprite at ((loopindex("x")-(cols-1)/2)*32, (loopindex("y")-(rows-1)/2)*32) [/code:egxi6tbt]
    
    As a final example you can store the row, column in the sprite itself and do this:
    [code:egxi6tbt]
    global number rows=4
    global number columns=4
    global number centerx=100
    global number centery=150
    global number offsetx=0
    global number offsety=0
    
    start of layout
    --- sprite: destroy
    --- set offsetx to (columns-1)/2
    --- set offsety to (rows-1)/2
    
    start of layout
    for "x" from 0 to columns-1
    for "y" from 0 to rows-1
    --- create sprite at (0,0)
    --- sprite: set column to loopindex("x")
    --- sprite: set row to loopindex("y")
    
    every tick
    --- sprite: set x to centerx-offsetx+self.column*32
    --- sprite: set y to centery-offsety+self.row*32
    [/code:egxi6tbt]
    That way you can move it around by changing the center.
  • works wonderfully, thanks

    So now i was testing something and it seems that i have encountered a bug.

    I'll post a capx

    https://www.dropbox.com/s/jugp0t8xwl2b1 ... .capx?dl=0

  • If you want to create them every tick, you'll probably wan to destroy them all every tick first. You can also change the loops to go from -2 to 2 instead.

  • Maybe i should have explained the bug first. Pay atention to the first event

    for "x" from 1 to squad.cols

    and second event

    Squad.cols=squad.units/squad.rows

    it is basically 10/5, but for some reason the result isnt 2 its 3 (it should create 2 collumns insted of three)

    EDIT: whoopsy found the issue, every tick that counts squad.units/squad.rows should go before the first event

  • Alright so is it possible to move sprites in positions instead of creating them, let me elaborate

    First i create 10 units

    and then is use the same thing but instead of create unit at (loopindex("x")*32), (loopindex("y")*32)

    i use set position on already created units.

    I tried that but it places them in the same position

  • The last code block above should work for that by changing the center variables.

    If you like you can poke around my capx I made to try it out. I got a little carried away but it might give some ideas.

    https://dl.dropboxusercontent.com/u/542 ... ation.capx

  • Thanks that worked really nice.

    So i did play around with it and this is more or less the final stage of what i am trying to do

    The formations must be flexible and that was what i was trying to do, if i had more time i would do more, but i dont (neither the time nor the skill)

    So here is what i made

    It's not done but its getting really close, there are some problems which i identified but i do not know how to solve them.

    https://www.dropbox.com/s/j69yhu63quv1p ... .capx?dl=0

  • I don't a lot of time either. I guess I'm confused with what you want to do. Do you want to re-arrange the units to be in a different formation? Like go from a 5x5 to a 4x7?

    I guess you could pick only the units that need to move like you might be trying to do? A simper way would be just to reassign all of them:

    global number x=6

    global number y=5

    For each unit

    --- unit: set col= loopindex%x

    --- unit: set row=int(loopinde/x)

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • The last code block above should work for that by changing the center variables.

    If you like you can poke around my capx I made to try it out. I got a little carried away but it might give some ideas.

    Is it possible to use pathfinding instead of move to angle?

    Center.Angle+90

    (Self.column-0.5*(columns-1))*32

    I have no idea how to make sprites rotate towards the click before moving along the path.

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