GameSalad

Howdy, Stranger!

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

How do I make actors move to a random position without overlapping?

lollipoppy811228lollipoppy811228 Posts: 5Member
edited March 27 in Working with GS (Mac)

I'm trying to create a scene where 6 actors are gathered in the center and then scatters to random positions after 1 second without overlapping the original positions and each other.
I've figured out the timer part.
but I'm confused about the position part.
I've used change attribute - self.position.x to random
but how can I make them not overlap their original position?
Please help

Comments

  • tatiangtatiang Posts: 11,815Member, Sous Chef, PRO, Senior Sous-Chef

    One technique is to move the actor to a new random position if it overlaps or touches another actor. You can use actor tags to simplify the rule if desired.

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

  • lollipoppy811228lollipoppy811228 Posts: 5Member

    @tatiang said:
    One technique is to move the actor to a new random position if it overlaps or touches another actor. You can use actor tags to simplify the rule if desired.

    Thank you for the comment!
    I've made a rule with a collision condition and the random position behavious under the rule as you said.
    but it seems not working as it should be.
    Is there a way to repeat the repositioning part until they don't overlap?

    • How can I make them not to leave the screen area?
  • tatiangtatiang Posts: 11,815Member, Sous Chef, PRO, Senior Sous-Chef
    edited March 27

    You may need to place the overlap rule inside an every 0 seconds Timer. Theoretically, it should work without one but when I tested it (as you did), it required a timer/loop.

    When you set the actors' positions, add/subtract half of their size:

    Change attribute self.Position.X to random(0+self.Size.Width/2, 568-self.Size.Width/2)

    The 568 represents the scene width. You'll need to do the same with the Y position, adding/subtracting half the height of the actor from the scene height.

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

  • lollipoppy811228lollipoppy811228 Posts: 5Member

    @tatiang said:
    You may need to place the overlap rule inside an every 0 seconds Timer. Theoretically, it should work without one but when I tested it (as you did), it required a timer/loop.

    When you set the actors' positions, add/subtract half of their size:

    Change attribute self.Position.X to random(0+self.Size.Width/2, 568-self.Size.Width/2)

    The 568 represents the scene width. You'll need to do the same with the Y position, adding/subtracting half the height of the actor from the scene height.

    Thank you! It helped a lot!

Sign In or Register to comment.