Random Actor Generation

AlloyJ

  • Posts: 82
I'd like to make a random actor from a group of actors be created at a varying time in a set window at the top of the scene, to fall down the screen. Anyone know how to do this?

Ceric

  • *
  • Posts: 610
There are a few ways to do this, but how many actors do you plan to have in your group of actors? Also, when you say "window," will each actor be created in the same position each time, i.e. the exact same X and Y values, or do you want each actor to be created at a different spot within set boundaries inside the window?

« Last Edit: February 08, 2014, 05:53:52 pm by Ceric »

AlloyJ

  • Posts: 82
They need to be in different X,Y each time, like from a pool of X,Y's

And 5-10 actors preferably.

Ceric

  • *
  • Posts: 610
Edit 2: For some reason I had an extra block that was unnecessary in there. You don't need to create 1 actor for each item in the list, just generate a random actor from the list to create. Sorry about that.

I put the new pic of the revised Behavior below.

Edit: The second pic shows blocks that generate the random number for the YPosition. Forgot to put that in my original Behavior.

You could do it as shown in the following Behavior. How familiar are you with Stencyl's Design Mode? Does this make sense to you or do you want me to explain it?

FYI for the X position, I just used example values in a specific range because I don't know the size of your game's window, its resolution, etc. You would want to make the random numbers being generated be between the bounds of the on-screen area that you're using for your game where you would like the actors to appear.

Also, this Behavior doesn't destroy the actors or make them move - you could make them move by using some kind of movement Behavior or you could just set up gravity and mass for the actors in your game and have them fall.

 

« Last Edit: February 08, 2014, 06:42:51 pm by Ceric »

AlloyJ

  • Posts: 82
Looks great and I will be testing it in the morning. I'm pretty familiar with the program, but some things just don't make sense. Randomizing always had my head spinning.

Innes

  • *
  • Posts: 1960
It looks like you have what you need, but I have another suggestion that might be useful, either to you or someone else who stumbles across this thread...

In the past, when I have wanted something similar (actualy; almost identical, because I wanted random actors dropping from the top of the scene), I have used a single actor with multiple animations. I just selected the required animation depending upon a random number. If the purpose of the random actors within the game, this is a nice and easy way to do it. Even if (for example, in a catching game), the random actor needs to have a random value for giving points to the player, you can set the value based on the current animation. e.g. [if current animation = 1] [set value of points to 4].

I've also used a similar process for random star-fields. I have just a single star actor with multiple animations (small, medium, large stars). The stars are displayed randomly, and then I move them across the screen with the speed of the star dependent upon the animation number (which also affects the size of the star). This is an easy way to create a parallax star-field with a single actor and one event.

This solution simplifies coding and also substantially cuts down the number of actors. Of course, it's not ideal for every game, but I've used it a few times.

Hope that helps.
Visit www.TheStencylBook.com - the only published book for learning Stencyl.

AlloyJ

  • Posts: 82
It's not allowing me to import the PNG for some reason. Any one else able to?
And, how do I even put actors on the list? All I can put on the list is text or a number.

« Last Edit: February 09, 2014, 10:17:22 am by AlloyJ »

ggiersdorf

  • Posts: 52
how are you creating this "List" I have 6 objects I want randomly placed at a position. I cant figure out how to make a list that pulls random OBJ1-OBJ2-OBJ3 etc ever 3 seconds and scrolls them from left to right


Ceric

  • *
  • Posts: 610
Quote
It's not allowing me to import the PNG for some reason. Any one else able to?
And, how do I even put actors on the list? All I can put on the list is text or a number.

Hm, not sure what the issue with importing PNGs might be. What's the size of the image? Sounds like it might be a bug of some kind.

FYI regarding lists, are you using a Game Attribute or a regular Attribute?

I used a regular Attribute. You create them via the Palette in Design Mode. Click the Attributes button and then the Create Attribute button. See the attached screenshot.

FYI Game Attributes are accessible by any Behavior in your game - think of them as global variables. Regular Attributes are specific to a Behavior - you have to reference them explicitly in one Behavior if your Attribute is in another - they're kind of like local/private variables, scope-wise.

I'm pretty sure you can add anything to a List Attribute (Game Attribute or a regular one) but you have to do that via the List manipulation blocks (under Attributes -> Lists).

Does that make sense?

Ceric

  • *
  • Posts: 610
Quote
how are you creating this "List" I have 6 objects I want randomly placed at a position. I cant figure out how to make a list that pulls random OBJ1-OBJ2-OBJ3 etc ever 3 seconds and scrolls them from left to right

As for this question, how are you adding objects to your game in the first place? Have you created Attributes to store them in? Basically you create a single Actor Type Attribute for each Actor you want to show up in your game, then you add each one to a List Attribute (via the List blocks, again, they're under Attributes -> Lists), then you use the other blocks (as shown above) to spawn each one at random X and Y coordinates.

As for motion, I would create a separate Behavior that moves them from left to right. Attach that Behavior to each Actor Type. Then, once your Behavior that spawns the Actors creates them, your motion Behavior kicks in and they start moving from left to right.

Ceric

  • *
  • Posts: 610
Also, Innes' solution is really good, too. Instead of creating a list, each animation frame for your single Actor would use a different image/sprite. Then you can use the Actor -> Draw blocks to change which animation frame is shown and the Random block (under Numbers & Text -> Math) to select a random animation frame.

ggiersdorf

  • Posts: 52
Ceric thanks for the input I think I almost got it, but im a bit lost still. Here is what I have done.

I created an actor than added 2 frames to it not looping. I than went to my main scene and created
the following.




I than added OBJ1 to the scene on the right but it doesnt work.

Can someone please help me along here :) thanks!

Ceric

  • *
  • Posts: 610
Hm, when you say "it's not working," can you describe what's happening? Just by looking at your blocks there, I would guess that you've got the blocks in the wrong order - you have to create the object first before you can have it switch frames, but I'm not sure what you have set up (i.e. have you placed an Actor in the Scene to start or are you using some other Behavior to create the Actor in your Scene).

I'll test things out and get back to you.

Ceric

  • *
  • Posts: 610
Also, I think the problem is you're switching animation frames, not from one animation to the next.

Ceric

  • *
  • Posts: 610
I made 2 separate Behaviors to achieve what Innes suggested to keep things simple. The first is a Scene Behavior that creates Actors of the type you specify at random positions.

The second is an Actor Behavior that switches which animation is being displayed. You attach this to the Actor Type you're creating in the Scene. Note I said "animation" and not "frame." Each animation can have multiple frames and to do what Innes suggested you need to switch animations. I gave each separation animation for the Actor Type a number as its name, so the first animation is 0, the second is 1, the third is 2, etc., so they correspond to the different values within the range specified in the random number block. You can use any range that you like as long as you name your animations appropriately.

I also tested these and they work correctly. Let me know if you have any questions about how to make these.