Changing direction every time?

stackpoolestackpoole Melbourne, Victoria, AustraliaMember, PRO Posts: 473
edited July 2012 in Working with GS (Mac)
Hello,
Im hoping its just a simple ques:)

I have an actor recycling when it hits something back to the top of the scene
It is simply moving down at the random direction (200,320)..
Basically I want it to change direction every time it hits the "base" when it goes back to the top of the scene.

right now, it is moving at the same direction as it first moves at the start of the scene..

Hope you know what i mean

Sydney Swans 2014!!

Best Answers

  • MotherHooseMotherHoose Posts: 2,456
    edited July 2012 Accepted Answer
    coding is easier when you know when/what you want to do

    drag in a Note Behavior … they are really notes to yourself!
    type in:
    when: hits or collides
    what: go back to top of scene (Y)
    … and change direction (X) to random

    then writing the rule is easier … as you know what triggers what behaviors
    EX:
    Rule: when
    Event: overlaps or collides with something
    -changeAttribute: self.Position.Y To: 300
    -changeAttribute: self.Position.X To: random(200,300)

    ==
    making notes/comments is not a newbie's thing

    open any good codeScript and you will see many lines of:
    … // or /* … comments by the programmer

    before you delete your note:
    doubleClick on the titleBar of your rule … and type in what that rule does
    EX: Rule: collide > change X;Y

    when you need to debug or want to change an action
    you will know where the problem is or where you want to make changes

    yes, it takes a few seconds to type these comments/notes
    but using that time … will save you lots of frustration and keep you focused!

    @};- MH
  • KodeRiter722KodeRiter722 Posts: 42
    Accepted Answer
    If I understood your question correctly, you are looking to have this actor return to the same location that it started, but to move in a random direction between 200 and 300 degrees, correct?

    @motherhoose's suggestion will start you at a random position somewhere near the top of the screen (if you are in landscape...closer to middle if portrait), but you will continue to move in the same direction every time...

    If you would like to fire off in a random direction each time you are reset, then what you want is to create a rule that says:

    when actor overlaps/collides with ground (or reaches some y position where you want it to reset):
    - Change Attribute: self.position.X to (Reset location's X position)
    - Change Attribute: self.position.Y to (Reset location's Y position)
    - Change Velocity: Direction: random(200,300) / Speed: Whatever speed you want

    @motherhoose is absolutely correct about writing out your logic in what is formally known as pseudocode before you attempt things like this, and also on the importance of commenting. Comments (or Notes as they are referred to in GS) allow you to come back a year or two after creating your "code" and still understand what the heck is going on. In addition to their ability to facilitate your debugging process,they also make bringing others in on a project infinitely easier... Research more on those topics if you are interested in writing cleaner "code"...
Sign In or Register to comment.