How do I make several sprites form a formation

Get help using Construct 2

Post » Fri Feb 05, 2016 4:26 pm

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.
B
7
S
2
Posts: 102
Reputation: 975

Post » Fri Feb 05, 2016 10:02 pm

You can create objects in a formation by using two loops

Code: Select all
start of layout
for "x" from 0 to 3
for "y" from 0 to 3
--- create sprite at (loopindex("x")*32, loopindex("y")*32)


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: Select all
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)


As a final example you can store the row, column in the sprite itself and do this:
Code: Select all
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

That way you can move it around by changing the center.
B
91
S
31
G
99
Posts: 5,199
Reputation: 65,684

Post » Sat Feb 06, 2016 4:23 pm

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
B
7
S
2
Posts: 102
Reputation: 975

Post » Sat Feb 06, 2016 9:07 pm

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.
B
91
S
31
G
99
Posts: 5,199
Reputation: 65,684

Post » Sat Feb 06, 2016 10:58 pm

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
B
7
S
2
Posts: 102
Reputation: 975

Post » Sun Feb 07, 2016 10:49 am

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
B
7
S
2
Posts: 102
Reputation: 975

Post » Sun Feb 07, 2016 10:36 pm

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
B
91
S
31
G
99
Posts: 5,199
Reputation: 65,684

Post » Mon Feb 08, 2016 6:21 pm

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
B
7
S
2
Posts: 102
Reputation: 975

Post » Mon Feb 08, 2016 7:43 pm

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)
B
91
S
31
G
99
Posts: 5,199
Reputation: 65,684

Post » Thu Jun 23, 2016 1:52 pm

R0J0hound wrote: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.
Image
B
17
S
4
Posts: 49
Reputation: 1,025


Return to How do I....?

Who is online

Users browsing this forum: istero, Neiroe and 9 guests