Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!


eboyeboy Posts: 239Member
How could I set-up an up and down motion for an actor with a little rotation and randomness...Thanks.


  • sdparduesdpardue Posts: 110Member
    Here's something I got working with a ball that bounces top to bottom...

    In your scene, set up an actor called TOP that stretches across the top of the screen. This is the barrier that keeps the ball from going off the top.

    Do the same with an actor called BOTTOM that stretches across the bottom of the screen.

    In the code for the Moving Ball that I spawn, there are 2 "naked" behaviors and 3 rules. By "naked", I mean they're outside of any rules.

    Behavior 1: Rotate. Set the rotation direction and speed.

    Behavior 2: Change Velocity. Set the initial direction and speed, relative to the scene.

    Now, you have it moving and rotating as soon as it's spawned.

    Rule 3: When actor receives event Overlaps or Collides with actor of type TOP, Change Velocity to whatever direction and speed you want. I use relative to screen. Since you said you want to introduce some randomness, set the direction to random (0,180). Wait, that won't really do it. You want it to move "down", so try random (225,315), or whatever fits your need.

    Rule 4: When actor receives event Overlaps or Collides with actor of type BOTTOM, Change velocity to direction random (45,135), or whatever fits, relative to screen.

    Rule 5: When Any -- attribute self.Position.X <= 0 (if it goes off screen left) or attribute self.Position.X >=480, Destroy the actor.

    Of course, you could also place two other actors LEFT and RIGHT on the scene, and code two additional rules for your actor to change its direction (using Change Velocity) and it would keep on bouncing until it ran into something that was designed to stop it. In that case, you'd replace the self.Position.X code with the appropriate Overlaps or Collides code to destroy the actor.

    This worked for me. Hope it helps you.


  • eboyeboy Posts: 239Member
    WOW! Your are the man. Do you know of any reference books that I could use to learn more about behaviors and actions using expressions? Thanks.
  • sdparduesdpardue Posts: 110Member
    The GameSalad Wiki is getting more useful and complete by the day. A very good resource.

    Plus, you can hover over the magnifying glass (like the one at the top of this page) and search for topics you're interested in learning more about in the Wiki, this Forum, etc. Very helpful feature. I learn something new every time I do a search.

    Perhaps the best way to learn is to try it. If you're not sure how a particular behavior works, build a small example, compare it to a similar behavior, etc. If you're using an expression that doesn't seem to work, assign its value to a global variable (game attribute), then use Display Text to help you debug what's happening.

    You'll get there... and soon you'll be answering questions for others on the forum!
This discussion has been closed.