# SOLVED / Set 3+ objects to exact locations in random order?

Get help using Construct 2

### » Mon Apr 13, 2015 5:36 am

I have searched the tutorials and forums for an answer to this question and I could not find a solution.
So if you know of a tut, or an answer elsewhere, kindly post it in the reply.

I have 3 objects in a layout.

Sprite 1 at 100, 200
Sprite 2 at 300, 200
Sprite 3 at 500, 200

as seen below,
[img]
https://dl.dropboxusercontent.com/u/139 ... pic_eg.jpg
[/img]

I just want to arrange them in random order on start of layout.

If you are able, please provide a capx.

Much much appreciated.

Thanks

--------
--------------------------------
------------------------------------------------

Just in case the capx is lost in space at some time in the future I decided to post a tutorial,
because I think this info is very useful
see:
https://www.scirra.com/tutorials/1436/s ... ndom-order
Last edited by producermark11 on Tue Apr 14, 2015 1:20 am, edited 4 times in total.
B
36
S
10
G
4
Posts: 250
Reputation: 8,033

### » Mon Apr 13, 2015 9:49 am

For sure there another ways to do this, but what in my mind right now is like this:

on start of layout set global variable to choose(1,2,3,4,5)

global variable = 1 --- set sprite 1 Y to x position - set sprite 2 Y to x position - set sprite 3 Y to x position
trigger once

global variable = 2 --- set sprite 1 Y to x position - set sprite 2 Y to x position - set sprite 3 Y to x position
trigger once

Etc...
B
60
S
10
G
10
Posts: 1,013
Reputation: 9,165

### » Mon Apr 13, 2015 9:54 am

Thanks A0Nasser,

I was writing this ( below) as you were posting an answer....

Alright, so I thought I had to use an Array
but I found a way to do it without and Array.

If there is someone out there who knows how to do this with an Array,
please post that method below.

Non Array method: Lets call it THE BRUTE FORCE METHOD :
[img] https://dl.dropboxusercontent.com/u/139 ... ic_eg2.jpg
[/img]

RE:
Image:
Event #2 = System> Trigger Once /// <<<or this could be put into the 'On start of layout' line as well >>>
Action = System > (Global & local variables) Set value
Event #3, 4, 5 = System> (Global & local variables) Compare variable ///
Action = (Sprite) > Set position

Again,
if someone has a method via an Array, please post.
Last edited by producermark11 on Mon Apr 13, 2015 3:15 pm, edited 4 times in total.
B
36
S
10
G
4
Posts: 250
Reputation: 8,033

### » Mon Apr 13, 2015 10:32 am

Move the action on event #2 inside event "on start of layout" ("on start of layout" run once)
B
28
S
17
G
159
Posts: 894
Reputation: 82,761

### » Mon Apr 13, 2015 1:08 pm

Sure that would work too. :-p

Any Array methods........ anyone
B
36
S
10
G
4
Posts: 250
Reputation: 8,033

### » Mon Apr 13, 2015 2:17 pm

August 2015 - I misplaced a lot of links - I will try to find backups and repost. If You find a post that interests please reply to post with @rampackwobble and I will get a nudge!

If you find my posts useful please consider donating something to a local charity or to the http://www.mndassociation.org/
B
64
S
34
G
47
Posts: 1,194
Reputation: 30,912

### » Mon Apr 13, 2015 3:27 pm

Solved with and Array.

Ok, I have done more research and have come up with an answer.
Thanks to kittiewan and the tutorial she provided here:
https://www.scirra.com/tutorials/315/generating-random-numbers-without-duplicates

The reason you want to use an Array is simple.
To reduce the amount of code and increase the number of variations when a sequence is generated.

If you happened look at the first example using The Brute Force Method
I coded in only 3 variations based on the random number that was generated,
but
in actuality there are 6 different possibilities. being : 1,2,3 ,, 1,3,2 ,, 2,3,1 ,, 2,1,3 ,, 3,2,1 ,, 3,1,2
Could I have coded those different possibilities in? of course, but what a hassle.

Anyway:
Here is the image of how I adjusted her code to my needs.
https://dl.dropboxusercontent.com/u/139486255/Image/array_set_objects_random.jpg

and here is the capx:
https://dl.dropboxusercontent.com/u/139486255/Image/array_set_objects_random.capx
B
36
S
10
G
4
Posts: 250
Reputation: 8,033

### » Mon Apr 13, 2015 3:37 pm

Here's another option using some placeholder position sprites at the different locations. For each sprite you want to place, pick a random unused position sprite and place it there.

random_position.capx (r200)
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,860

### » Mon Apr 13, 2015 3:41 pm

@ramones
Wow, That is an interesting method, and an easy method too!
Thanks for sharing.

@ramones
I tried to PM you but not sure if it went thru.
I have a question if that's ok.

What if there are 3 sprites with no animation frames?

How easy is that ?
Last edited by producermark11 on Mon Apr 13, 2015 4:23 pm, edited 1 time in total.
B
36
S
10
G
4
Posts: 250
Reputation: 8,033

### » Mon Apr 13, 2015 4:20 pm

Same thing, just swap Sprite for Family.
B
55
S
29
G
19
Posts: 1,520
Reputation: 25,860

Next