How do I Have random objects fly in

Get help using Construct 2

Post » Wed Feb 17, 2016 8:54 am

I am trying to have it so that every "so and so" seconds a certain items flys in from off the layout and into the actual game screen, I have been spent a couple of hours now following tutorials on random objects but every single one I try just ends in failure or I end up doing it correctly but with no clue as to how to proceed to get the result I am looking for.

I.E I tried this tutorial
Image

And it works, kinda..
But I have no idea how to stop the spawned object from de-spawning. and I want it to look like it came in from outside of the layout, not just appearing..

Anyways... Could someone please point me in the right direction ?
B
55
S
21
G
16
Posts: 82
Reputation: 12,030

Post » Wed Feb 17, 2016 10:01 am

What do you mean with 'de-spawning'?
The easiest way is to create the object outside the viewport and move it into it.
For example:

System| Every x seconds -> System | create object Sprite on layer 0 at (-sprite.width-1, y)
This will spawn an object on the left. -Sprite.width-1 pixel outside of the left border.
If your sprite has the width of 32 it will be created at x=-33, y.

Now you have to move it:
System| every tick -> Sprite| Move forward -1 pixel.

If you want to spawn it at the right side:
System| Every x seconds -> System | create object Sprite on layer 0 at (OriginalWindowWidth+Sprite.width+1, y)
System| every tick -> Sprite| Move forward 1 pixel.

OriginalWindowWidth is the Windows width in the project settings.

This will work as long as the screen doesen't scroll, then you have to use viewportleft and right
B
24
S
3
G
1
Posts: 422
Reputation: 2,107

Post » Wed Feb 17, 2016 10:16 am

Asmodean wrote:What do you mean with 'de-spawning'?
The easiest way is to create the object outside the viewport and move it into it.
For example:

System| Every x seconds -> System | create object Sprite on layer 0 at (-sprite.width-1, y)
This will spawn an object on the left. -Sprite.width-1 pixel outside of the left border.
If your sprite has the width of 32 it will be created at x=-33, y.

Now you have to move it:
System| every tick -> Sprite| Move forward -1 pixel.

If you want to spawn it at the right side:
System| Every x seconds -> System | create object Sprite on layer 0 at (OriginalWindowWidth+Sprite.width+1, y)
System| every tick -> Sprite| Move forward 1 pixel.

OriginalWindowWidth is the Windows width in the project settings.

This will work as long as the screen doesn't scroll, then you have to use viewportleft and right



Thanks for the response!

With the code I tried in the picture in my OP the object spawns randomly on the viewport but than just vanishes, I am trying to have it so that multiple objects randomly fly in from outside the viewport onto the viewport and do not de-spawn upon landing in the viewport.

I understand how to have a sprite randomly appear in different places inside the viewport, what I do not understand is how to have a random object fly in from outside the viewport and land inside the viewport and stay there, I am also trying to do this for all directions, left, right, bottom & top.

I.E I do not want the object to just magically appear inside the viewport lol.
B
55
S
21
G
16
Posts: 82
Reputation: 12,030

Post » Wed Feb 17, 2016 10:52 am

Ahh, now I understand. :)
I'm afraid I can't really help you with that, that it really look good. There are people here I believe are fare more competent for this kind of problem.
But I've tried something. :)

Image

It works, but it doesn't really looks like something 'flies in'.
B
24
S
3
G
1
Posts: 422
Reputation: 2,107

Post » Fri Feb 19, 2016 9:43 pm

Asmodean wrote:Ahh, now I understand. :)
I'm afraid I can't really help you with that, that it really look good. There are people here I believe are fare more competent for this kind of problem.
But I've tried something. :)

Image

It works, but it doesn't really looks like something 'flies in'.


Haha, well I tried your code and that just ends up making the objects act like leaves blowing into the viewport :P
I'm looking to get 1 object thrown into the viewport from a random direction every (random) seconds.
I appreciate you trying though!
B
55
S
21
G
16
Posts: 82
Reputation: 12,030

Post » Fri Feb 19, 2016 10:52 pm

You could give the object you want to spawn in the bullet behavior then you could do this:

every 1 seconds
--- create sprite at (random(640),random(480))
--- sprite: set angle to random(360)
--- sprite: move forward -640 pixels

sprite: bullet distance traveled > 640
--- sprite: set speed to 0



There's also the third party move to behavior that may be useful here.
B
87
S
29
G
89
Posts: 5,103
Reputation: 60,192

Post » Fri Feb 19, 2016 11:01 pm

R0J0hound wrote:You could give the object you want to spawn in the bullet behavior then you could do this:

every 1 seconds
--- create sprite at (random(640),random(480))
--- sprite: set angle to random(360)
--- sprite: move forward -640 pixels

sprite: bullet distance traveled > 640
--- sprite: set speed to 0



There's also the third party move to behavior that may be useful here.



Actually that is somewhat close to what I am currently trying :P
My real issue is having the object created off-screen first, than launching the object at an angle on the veiwport than having that object stop randomly on the veiwport.

I have managed to get this kinda working, I used create object at (spawner-sprite) off screen than using bullets angle of motion at random(0, 90), this will effectively launch the object from the spawner-sprite onto the viewport on a horizontal angle, I wish I could do random(0, 90, 140) and encompass a larger area for the objects bullet to launch, the real problem is that some of the objects get launched on the border of the window and are half in/half out of the screen, of course this is because I am using a random angle as well as a random speed for the bullet launch, and I am not sure how to prevent it.

Edit : I managed to get the angles correctly working, now my only issue is destroying the object after it leaves the play screen without interfering with the object being created at the spawner-sprite.
B
55
S
21
G
16
Posts: 82
Reputation: 12,030


Return to How do I....?

Who is online

Users browsing this forum: Burvey and 11 guests