With the power of maths, to space
n objects which have a width of
w each evenly across a length of
L, you would need to space them:
Code:
var space = (L - (n * w)) / (n - 1)
Hope I helped
EDIT:
To place your objects:
Code:
xx = 0;
for (i = 0; i < n; i++) {
instance_create(xx + space, y, object);
xx += space;
}
That won't work.
You calculated "space" to be the distance between the rightmost edge and the leftmost edge of two subsequent instances.
You did this under the assumption that the leftmost edge of the first instance would be placed at x = 0
and the rightmost edge of the last instance would be placed at x = L.
It may be that Mallard8 wanted this, but my guess is they don't.
They probably wanted to have the same amount of space to the left of the first instance and to the right of the last instance as there is inbetween 2 subsequent instances.
Anyway, the loop you then use treats "space" as though it was defined as the distance between the origins of 2 subsequent instances.
It also assumes the origin of the sprite of those instances are placed at the left edge of the sprite.
As a final detail, instance_create doesn't work with GM:S 2.
A loop that places the instance
Code:
var xdist=(room_width-(160x10)) / 11
var xstart
var i
for (i=0 ; i<10 ; i++){
instance_create_layer(xdist+xstart, y, "layer name", object_name)
xstart+=xdist
}
Same main mistake, you define "xdist" to be something different from the way you use it.
On top of that, xstart is a built-in variable, so you cannot declare it to be in a local scope through "var".
I also don't think you can pass a string as layer id to instance_create_layer.
EDIT: You can pass a string there.
Look up the "lerp" function.
Would work.
I wouldn't personally use it though.
I have a room 3200 width
10 objects 160 width
How can I get them evenly spaced across the room width
3200 - 160 x 10 = 1600 / 11 = Place object every 145.45
I think this is what I need but how to code it correctly?
Same main mistake as the guys above.
Let's call the room width "L", the instance width "W", the amount of instances "N" and the sprite offset of your instances "OX",
the y-coordinate at which the instances should get spawned "Y", the id of the layer on which the instances sould get spawned "LAYER" and the object index of the instances "OBJ".
You'll want to calculate the "space" between consecutive instances as done above.
Then you'll want to do tis with it:
Code:
var space = (L - W * N) / (N + 1)
var dx = space + W;
for(var k = 0; k < N; ++k) {
instance_create_layer(space + OX + dx * k, Y, LAYER, OBJ);
}
EDIT: fixed mistake