Random actor spawn after one gets destroyed.

I'm very new to GameSalad, as of right now I'm just learning some of the basics. One thing I'd like to do in a basic game I'm making for the sake of learning is have coins spawn in a random spot on the scene as they are collected.

Example: There is a Player and Coin actor
-Player and Coin are initially visible (can you make an actor [coin in this case] appear in a random location when loaded?)
-Player navigates and collides with coin
-Coin gets destroyed, Coin count (Score) goes up one
-Another coin is generated in a random spot when previous coin is destroyed.

I hope that made sense. I'll clarify as best as I can if it didn't. I've gotten down how to control the "player" with the keyboard (i.e. WASD) and how to make the "coin" get destroyed when the "player" collides into it, as well as how to make the "coin count" increase, but I don't know how to make another coin respawn in a random location.

Any help is appreciated, thanks!

Comments

  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited December 2012
    Welcome to GameSalad! When you spell out what you're trying to do so well, it makes it easy to help you.

    Do you want the easy way or the hard way? ;)

    EASY WAY
    Recycle the coin actor. On your coin actor, add a rule that says:
    When actor collides with actor [player]
    .....Change Attribute self.position.X to random(0,1024)
    .....Change Attribute self.position.Y to random(0,768)

    If you click the expression editor (the little 'e' symbol) for the x and y position of the spawn actor behavior, you can either type in or select the random function from the drop-down menu. 1024 and 768 are iPad maximum dimensions, but you can adjust as necessary.

    HARD WAY
    On your coin actor, you'll need a rule that says:
    When actor collides with actor [player]
    .....Change Attribute game.readyToSpawn (type: boolean; set to false initially) to true
    .....Timer after 0.1 seconds
    ..........Change Attribute game.readyToSpawn to false
    .....Destroy actor

    Make a coin spawner actor and add this rule:
    When attribute game.readyToSpawn is true
    .....Spawn actor [coin] at random(0,1024) random(0,768)

    The 0.1 timer is just a bit of a hiccup to allow the spawner to catch the change in that boolean value from false to true (and then back to false to wait for the next coin to be destroyed).

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

  • carlblanchetcarlblanchet Member Posts: 755
    edited December 2012
    Had written an answer but then saw tatiang's answer after posting and refreshing. :P

    Welcome to gamesalad!
  • mrajflemingmrajfleming Member Posts: 1
    Ah! Thanks so much @tatiang !!

    The only thing I'm having trouble with is the initial spawn and respawn of the 'coin' actor using the -easy- method. Any elaboration would again be much appreciated.

    Thank you!!
  • tatiangtatiang Member, Sous Chef, PRO, Senior Sous-Chef Posts: 11,949
    edited December 2012
    Here's an example of rules for the coin actor. Any behaviors that are listed at the top of an actor's rules without a condition (e.g. When _____) run immediately. So if you set the actor's x/y position to a random number, it will "spawn" to that location immediately. The "re-spawn" happens when the collision occurs. It's the exact same two Change Attribute behaviors just wrapped in a rule. If you want the initial spawn to occur after some time or after an attribute has changed value (e.g. When game.pressedStart button is true) then you can wrap those two first behaviors in a timer or rule.

    image

    New to GameSalad? (FAQs)   |   Tutorials   |   Templates   |   Greenleaf Games   |   Educator & Certified GameSalad User

Sign In or Register to comment.