Constant movement question

appletreemanappletreeman Member Posts: 5
edited November -1 in Working with GS (Mac)
Hi all,
I'm new to all this so sorry if the answer to my question seems obvious.

I've got an actor on the stage that is moved left and right. How do I make it so that (for example) if the left key is pressed once the actor will carry on moving to the left until it either reaches the edge of the stage or the player presses the right key?

Comments

  • firemaplegamesfiremaplegames Member Posts: 3,211
    Hey,

    Is the actor always in motion? does it start out moving? when it hits either the left or right wall, does it immediately go in the opposite direction?

    If the above is true, and I understand you correctly, you can do it like this:

    Create an attribute inside the actor (integer) called "myDirection".

    Create two Rules in the actor:

    ANY
    When KEY is PRESSED (left arrow)
    When self.Position.X > (480 - (self.Width/2))
    Change Attribute: self.myDirection = 1

    ANY
    When KEY is PRESSED (right arrow)
    When self.Position.X < (0 + (self.Width/2))
    Change Attribute: self.myDirection = 0

    Create a Move Behavior in the actor and set the Direction to:

    180 * self.myDirection

    (180° is to the left, and 0° is to the right)

    Hope this helps!
    Joe
  • appletreemanappletreeman Member Posts: 5
    Thanks for the prompt answer, sorry for not replying sooner but I've been away.

    You're almost right. When the player presses left or right, the actor needs to move in that direction until it either reaches the side of the stage - at which point it stops moving, or the player changes direction.

    I can see I'm gonna have to watch a few of the videos as I'm not even sure how to create an attribute :)

    Sorry for being so thick, and thanks for your help.
  • PPenguinPPenguin Member, PRO Posts: 110
    This should be easy.

    Create a boolean attribute called "left", and leave it at 0.

    Create a rule for when the player presses left, set "left" to 1. You don't need an "otherwise" behavior here because he should keep moving to the left even after the player releases the left button.

    On the character actor, if "left" is 1, move to the left.

    So the only things that interrupt this is if the character hits a wall, or if another directional button is pushed?
    No problem, for the wall, set up a rule that says on collision with the character, set "left" to 0.
    For the directional buttons, set up rules that say when the user touches a button, set "left" to 0 (and set the appropriate direction to 1).

    That should get you started.
  • appletreemanappletreeman Member Posts: 5
    Nearly right. The actor moves in whichever direction until he opposite key is pressed (changes direction) or hits the side of the stage (stops).

    I've almost got it working but when it touches the side of the screen it won't move again.
    Sorry for being so dense :)
  • GamexcbGamexcb Member Posts: 179
    Make sure its on a scrolling layer or else it will have issues like that.
  • firemaplegamesfiremaplegames Member Posts: 3,211
    just so this issue doesn't hang you up forever...

    you can also put invisible "wall" actors on the sides of the stage. uncheck their movable checkbox.
    set your main actor to collide with them.

    That way you can get up and going again and work on the game itself...

    and try to work on this issue in another test file or something.
Sign In or Register to comment.