How to spawn a random object from a set

Get help using Construct 2

Post » Tue Jan 03, 2012 6:42 pm

How do I spawn a random object from a set of multiple objects? For example...

When right arrow key is pressed, spawn a yellow OR red OR blue OR green car at position (0,0).

Thank you in advance for any help you can offer.
B
2
G
1
Posts: 4
Reputation: 558

Post » Tue Jan 03, 2012 6:54 pm

Yann would probsbly suggest useing "Choose" I'd go for an array to map an integer to the "Name" of the instance to spawn.
B
42
S
19
G
12
Posts: 723
Reputation: 13,911

Post » Tue Jan 03, 2012 7:44 pm

And why not simply a random(0,car.AnimationFrameCount) ?

Example capx
New to Construct ? Where to start

Image Image
Image Image

Please attach a capx to any help request or bug report !
Moderator
B
247
S
85
G
40
Posts: 6,999
Reputation: 57,793

Post » Tue Jan 03, 2012 8:26 pm

Ill give you a bit more information to see which is the best route to take. The goal is to have a continuous level which generates the next room (object) by taking a random one from a list. Each room will have its own different collision and the rooms will be spawned on the location of another object.

Thank you for your help so far, just wondering what you think suits the above goal the best?SheepGoMOO2012-01-03 20:27:25
B
2
G
1
Posts: 4
Reputation: 558

Post » Wed Jan 04, 2012 12:15 am

Weishaupt > not choose, more what kyat said (:

Sheep > still need more information, what kind of gameplay, how would you use these rooms, Even without the randomness, how do you create these room? We have to see the big picture to give you an usable answer.
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Jan 04, 2012 10:31 am

It will be a sideways scrolling platformer game. The level will be never ending. The level will be randomly generated from a set of preset level sections rather than the game actually generating random content.

Example...

Player starts in section 'StartOfLevel'. This section is 1000px wide. At some point the game needs to decide what the next section of the level will be, to attach to the end of the 'StartOfLevel' section. These add-on sections will be smaller (maybe 300px wide) and are pre-designed.

The purpose of the add-on sections is to give the level variation as the level progresses.

If you need any more information then just ask me :)
Thank you
B
2
G
1
Posts: 4
Reputation: 558

Post » Wed Jan 04, 2012 12:17 pm

I see, if it wasn't a continuous scrolling thingy, the answer would be easy just a got to layout "room"&random(roomcount) but here it's something else.
Hmmm I know
You need to use two dummy sprite which would be used as a start and end point
You need to use the same objectType for ALL the elements of your room.
Just use differents animation Frame with an animation speed set to 0
And set the initial frame (in property panel) to the corresponding frame for the object you want to use

This unique sprite will have an instance variable named 'room' of type number which will hold the number of the room.

You will have the startDummy positionned at the connexion point between two room at the far left, and the endDummy positionned at the connexion point between two rooms at the far right

Each Dummy object will also have an instance variable named 'room'

The idea is that at start of frame you'll create an array with all the positions relative to startDummy (this dummy will drive all the room) and all the corresponding animation (to have the right image showing) if you use different angles you'll also have to store them.

In practice that should look like :
[code]System: on start of layout
System: foreach startDummy
    Sprite: room = startDummy.room
    System: foreach sprite
      -> Array: Set value at (startDummy.room,loopindex+1,0) to sprite.X-startDummy.X
      -> Array: Set value at (startDummy.room,loopindex+1,1) to sprite.Y-startDummy.Y
      -> Array: Set value at (startDummy.room,loopindex+1,2) to sprite.animationFrame
      // Storing the number of object+1
      -> Array: Set value at (startDummy.room,0,3) to loopindex+1
    endDummy: room = startDummy.room
      -> Array: Set value at (startDummy.room,0,0) to endDummy.X-startDummy.X
      -> Array: Set value at (startDummy.room,0,1) to endDummy.Y-startDummy.Y
// Cleaning up
-> startDummy: destroy
-> endDummy: destroy
-> sprite: destroy
[/code]
Basically the Array will then contain a map of each room with the startDummy as an origin. And in the first index, you'll have the X and Y position where you have to attach the next room.

Now to spawn rooms, you'll have to keep in check the position of the next room to spawn in two variable (for X and Y) (maybe if it's all at the same height level you can avoid storing/using Y)
And You'll have to loop through all the values of a room stored in the array, create Sprite, position it from the current new room to spawn, and set the proper animation frame.
Let see

[code]Global Variable nextX = 0 //X position of the nextRoom
Global Variable nextY = 0 //Y position of the nextRoom
// next room is near the border of the window
// spawn another room
// (I assume you always scroll to player and I add 100 as a security margin)
System: nextX < Player.X+WindowWidth/2+100
Local Variable randRoom=0 // which room?
// random(5) will return a random number from 0 to 4
-> Set randRoom to random(5)
// create the room
System: for "" from 1 to Array(randRoom,0,3)
    -> System: Create Sprite on layer "rooms" at nextX+Array(randRoom,loopindex,0),nextY+Array(randRoom,loopindex,1)
    -> Sprite: set animation frame to Array(randRoom,loopindex,2)
// store the end point of the new room
-> System: set nextX to Array(randroom,0,0)
-> System: set nextY to Array(randroom,0,1)[/code]
And that should basically be it

Now there's still 2 issues :
- The first one is zIndex dunno how the script will behave... maybe pretty well, else you might have to use layer and also store layer position to create in the right one.
- The second one is collisions, you might create invisible collisions and store their position to the startDummy the same way... But in another Array.

Tell me if it worked, I just wrote it as it came :D
B
60
S
22
G
14
Posts: 1,479
Reputation: 16,346

Post » Wed Jan 04, 2012 12:31 pm

Thank you very much for your help. I will give this a go when I get off work and post my findings!

B
2
G
1
Posts: 4
Reputation: 558

Post » Wed Jul 18, 2012 12:12 pm

Hey SheepGo I am doing a smiler thing to you a constant side scroller with randomly created maps. Did yours end up working?
B
14
S
3
G
4
Posts: 32
Reputation: 2,654


Return to How do I....?

Who is online

Users browsing this forum: 99Instances2Go, jul, nicklowkcmagic and 17 guests