spawn object on specific places randomly

For questions about using Classic.

Post » Thu Aug 04, 2011 2:04 pm

i will try to explain what i want to achieve...
i have five objects and when i click on specific button, i want on one of these object to spawn another object, but randomly and only on one of them. i cant figure it how to do it. i am sure it will not be hard, but for me it is hard to find a solution
B
4
S
2
G
1
Posts: 143
Reputation: 1,211

Post » Thu Aug 04, 2011 2:30 pm

random spawn

Hope this helps

Edit:
+ Button: On Button clicked
+ Items: Pick one at random
-> Sprite3: Destroy
-> Items: Spawn object Sprite3 on layer 1 (image point 0)chrisbrobs2011-08-04 14:34:06
B
19
S
6
G
7
Posts: 1,206
Reputation: 7,300

Post » Thu Aug 04, 2011 2:44 pm

Another appoach:

Given you have 5 instances, create a pv for the object (e.g. "selector")
Give the pv the value 0 for the first instance, value 1 for the second, etc, up to the fifth instance, where "selector" gets the value 4

Then

+ Button: On Button clicked
++ Item: "selector" equal to random(5)
--> Item: Spawn object ...
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242

Post » Thu Aug 04, 2011 4:15 pm

chrisbros: fantastic :) works perfect
tulamide: i tried also make it this way, it works, but it does not work well, sometimes if i click on button, nothing happens

thanks much guys for help!
B
4
S
2
G
1
Posts: 143
Reputation: 1,211

Post » Fri Aug 05, 2011 1:35 pm

[QUOTE=joxer] tulamide: i tried also make it this way, it works, but it does not work well, sometimes if i click on button, nothing happens[/QUOTE]
You are absolutely right. The reason for this is a mistake I made. By comparing sprite('selector') with random(5), every single instance is compared to that function, so a new random number is generated per instance, which contradicts the original purpose.

The right code would involve another variable, e.g. a global var "myRandom"

+ Button: On Button clicked
-> System: Set global variable 'myRandom' to random(5)
++ Item: "selector" equal to random(5)
--> Item: Spawn object ...

where ++ Item:... is a subevent to + Button:

Sorry!
Image
B
23
S
8
G
10
Posts: 1,820
Reputation: 8,242


Return to Help & Support using Construct Classic

Who is online

Users browsing this forum: No registered users and 3 guests