How do I spaw random objects

Just started using Construct 2? Post your questions here

Post » Sun Mar 06, 2016 10:18 am

Hi guys,

Is there somebody that can tell me how i build a game where multiple object randomly fall down from the top of the layout on random places (but on top of the layout of course)
B
5
S
1
Posts: 17
Reputation: 409

Post » Sun Mar 06, 2016 11:28 am

create a Sprite and add a bullet behaviour

Event
system > on start of layout | objSprite > destroy
system > every 2 seconds | system > create objSprite on layer=0; x=random (WindowWidth); Y=-30
add action > objSprite > set Bullet angel of motion=90

  • System > On start of layout | system > load game from slot "brain"
  • System > On load failed | system > scroll to obj_Forum
B
17
S
8
G
5
Posts: 340
Reputation: 4,707

Post » Sun Mar 06, 2016 11:50 am

Thanks!
i'll give it a try, but is it possible this way that a sprites will be half on the screen at the sides?
B
5
S
1
Posts: 17
Reputation: 409

Post » Sun Mar 06, 2016 11:56 am

in this case yes

to change that you could make a more precise random number
random (30,(WindowWidth-30))

Now the number starts at 30 and ends 30 pixels left of your layout.

  • System > On start of layout | system > load game from slot "brain"
  • System > On load failed | system > scroll to obj_Forum
B
17
S
8
G
5
Posts: 340
Reputation: 4,707

Post » Sun Mar 06, 2016 1:08 pm

Thanks!

You made my day!
B
5
S
1
Posts: 17
Reputation: 409

Post » Mon Mar 07, 2016 11:49 pm

So the game is working pretty fine but i am now struggling with something new.

I want my objects to be spawn pretty fast (random(0.2, 0.7)) and the bullet motion pretty slow, but now sometimes the sprites are overlapping.

Is there a way to set the nextspawn at least (for example) 30 pixels to the left of right?
B
5
S
1
Posts: 17
Reputation: 409

Post » Tue Mar 08, 2016 12:03 am

Here you have, as almost always, different options

make a loop
so you don't spawn directly and run a loop where the loop checks if the new spawn point is greater than 30 (check it against a variable you set when an object gets created)

Or you could make it with an array and spawn points (invisible sprite objects where the new objects get spawned) so instead of pixels you spawn them from the boxes - this is very useful if you want the objects get spawned evenly across the screen.
Also with a loop, but here you can decide very precisely what you want to do.

Or you could make a small random number and multiply it by 30 (still you need a loop to check where the last spawn happened). Th long version would look like this floor(random (0, windowWidth/30))*30

  • System > On start of layout | system > load game from slot "brain"
  • System > On load failed | system > scroll to obj_Forum
B
17
S
8
G
5
Posts: 340
Reputation: 4,707

Post » Tue Mar 08, 2016 12:25 am

"Object": is overlapping "Object" > "Object" destroy.
Alternately: > set "object" x:self.x+somedistance (Ie self.x+32)

No loops, variables, or arrays. =) Often used for top down SHMUPs.

Also, don't forget to destroy your objects if their Y becomes greater than viewport:bottom! =)
B
17
S
7
Posts: 21
Reputation: 1,711

Post » Tue Mar 08, 2016 12:32 am

@Davemon
oops forgot about the "is overlapping" -
that's definitely the easiest one (if it's ok that som bullets are skipped)

  • System > On start of layout | system > load game from slot "brain"
  • System > On load failed | system > scroll to obj_Forum
B
17
S
8
G
5
Posts: 340
Reputation: 4,707

Post » Tue Mar 08, 2016 11:47 am

Thank you guys!

I will try them all and see which one fits best for my game! :)
B
5
S
1
Posts: 17
Reputation: 409

Next

Return to Beginner's Questions

Who is online

Users browsing this forum: No registered users and 3 guests