How do I create random objects

Get help using Construct 2

Post » Tue Apr 04, 2017 6:42 pm

Hi,

I have two objects to create and want to spawn(or create) them randomly. This will be a infinite runner game so my objects has bullet behavior. Player has to jump over them. Each object to create must be positioned on different Y axes. And finally objects must come randomly. (random space between them but not too close or same posisioned)

First i tried this:

system > every random (1,2) sec > system create object Sprite1 on layer 1 at (720,275)

system > every random (1,2) sec > system create object Sprite2 on layer 1 at (720,300)

and tried different combinations of time values. But it didnt worked for me well. Objects come randomly but sometimes it comes too close to each other or same location.

Then i tried to put them in a family folder and made them to spawn. It comes randomly but the Y coordinate must be the same for all family. So it wont work for me.

I need help about that. Is there a proper way to do this?

Thanks,
B
3
Posts: 5
Reputation: 185

Post » Tue Apr 04, 2017 7:02 pm

Hey there! Can you explain a little more thoroughly what you're looking for? Do you need enemies to spawn at random times, or random enemies to spawn at the same time interval? Do you need random positioning, or just the set position?

From what I understood, you have two enemies you want to spawn at random intervals. I would do this:

1. Global variable: NextTimer = 1
2. Local variable: EnemyChance
3. Every Next Timer Seconds > Set EnemyChance to random(1,2)
4. ^Subevent to 3 - If 1, Spawn enemy 1, If 2, Spawn enemy 2.
5. ^Subevent to 3 - Set NextTimer to Random (0.5, 2)

Only one of the enemies will spawn at a time, the interval between spawns will change every spawn, and you can play with the values to fine tune the effect.
B
18
S
3
G
3
Posts: 82
Reputation: 2,543

Post » Tue Apr 04, 2017 7:42 pm

keatingb wrote:Hey there! Can you explain a little more thoroughly what you're looking for? Do you need enemies to spawn at random times, or random enemies to spawn at the same time interval? Do you need random positioning, or just the set position?

From what I understood, you have two enemies you want to spawn at random intervals. I would do this:

1. Global variable: NextTimer = 1
2. Local variable: EnemyChance
3. Every Next Timer Seconds > Set EnemyChance to random(1,2)
4. ^Subevent to 3 - If 1, Spawn enemy 1, If 2, Spawn enemy 2.
5. ^Subevent to 3 - Set NextTimer to Random (0.5, 2)

Only one of the enemies will spawn at a time, the interval between spawns will change every spawn, and you can play with the values to fine tune the effect.


Hi,

I think you understand right. But i dont understand the script you wrote:) I am new to C2. I need obstacles to come randomly to jump over them. The random thing here is the type of obstacle (there are 2 of them) and space between them. (this can be done with timing i think). Does your script work for that?
B
3
Posts: 5
Reputation: 185

Post » Tue Apr 04, 2017 8:30 pm

keatingb, i understand what you suggest now. I made it but there is a problem.

3. Every Next Timer Seconds > Set EnemyChance to random(1,2)

it makes enemychance to randomly between 1 and 2. (1.1,1.2,1.0156156 etc.) so i never be 1 or 2. So objects never created. is there a way of set value 1 or 2 exactly? ( i searched and "|" means or but it wont work again)

Thanks
B
3
Posts: 5
Reputation: 185

Post » Tue Apr 04, 2017 8:44 pm

I finally do it:)

I used choose(1,2) to replace random(1,2) and it works.

Thanks again.
B
3
Posts: 5
Reputation: 185

Post » Thu Apr 06, 2017 4:22 pm

AHH my bad! Sorry I'm just getting back in to Construct after way too long away, so I'm glad you were able to fix that one. I'm glad I was able to set you on the right path, though! Happy coding. :)
B
18
S
3
G
3
Posts: 82
Reputation: 2,543

Post » Thu Apr 06, 2017 4:36 pm

gkntrn wrote:Hi,

I have two objects to create and want to spawn(or create) them randomly. This will be a infinite runner game so my objects has bullet behavior. Player has to jump over them. Each object to create must be positioned on different Y axes. And finally objects must come randomly. (random space between them but not too close or same posisioned)

First i tried this:

system > every random (1,2) sec > system create object Sprite1 on layer 1 at (720,275)

system > every random (1,2) sec > system create object Sprite2 on layer 1 at (720,300)

and tried different combinations of time values. But it didnt worked for me well. Objects come randomly but sometimes it comes too close to each other or same location.

Then i tried to put them in a family folder and made them to spawn. It comes randomly but the Y coordinate must be the same for all family. So it wont work for me.

I need help about that. Is there a proper way to do this?

Thanks,


You can create a function "constructor" for each object like:
On function (create_object_1) -> Create object object_1 at X Y
On function (create_object_2) -> Create object object_2 at X Y

then:

Every (X seconds) -> call function "create_object_"&int(random(1,3))
B
6
S
2
Posts: 61
Reputation: 1,099


Return to How do I....?

Who is online

Users browsing this forum: No registered users and 28 guests