GameSalad

Howdy, Stranger!

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

How to make an actor move continuously when pressing a button

Mayhem_MadnessMayhem_Madness Posts: 168Member, PRO
edited December 2014 in Help Wanted

Hi everyone, just wanted some help on movement, I wanted to know how to move an actor continuously by pressing a touch button, so I want actor to move right continuously when the right touch button is pressed, then when I press the left touch button, I want the actor to continuously move left.
Thank you for any help.

Comments

  • jamie_cjamie_c ImagineLabs.rocks Posts: 5,542Member
  • Mayhem_MadnessMayhem_Madness Posts: 168Member, PRO

    Hi, I want the actor to keep on moving when the button is released, so when I press the right button it moves right continuously, when I'm not even pressing the button. Then when I press the left button I want the actor to change direction and move left continuously by only pressing the button once.

  • Mayhem_MadnessMayhem_Madness Posts: 168Member, PRO

    Basically I just wanted to know, by pressing the right button once I want it to move right continuously, then when i press the left button once, I want it to change direction and move left continuously.

  • Soopa ArrowSoopa Arrow Posts: 7Member, PRO

    Hi Thor. I would create a Boolean variable (set to false at default) such as 'moveright'. When right button is pressed, the value will change to true. When the value is true, try using Constrain Attribute to continuously move the object to the right. If your having trouble with Constrain Attribute, you could try using a Timer and set it to 'Every' 0.1 seconds (or below). That way it will continuously update and keep moving. When left is pressed then the boolean value for 'moveright' will be false and another boolean such as 'moveleft' will become true.

  • Mayhem_MadnessMayhem_Madness Posts: 168Member, PRO

    Hi Soopa Arrow, could you please explain in detail how I would do all of that. Thanks for the help.

  • jamie_cjamie_c ImagineLabs.rocks Posts: 5,542Member

    Hi Thor, if you watch the tutorial I posted, where I use a Move Behavior in the tutorial, use a Constrain Attribute instead. For example in the Rule named Move me Right you would replace the Move Behavior with a Constrain Attribute Behavior set to:

    Constrain Attribute: self.Motion.Linear Velocity.X To: 200

    You would then change the move left rule to the above but using -200

    To move up and down you would Constrain the value of self.Motion.Linear Velocity.Y

  • Soopa ArrowSoopa Arrow Posts: 7Member, PRO

    Sorry about the late reply Thor. I agree with @jamie_c‌ but I've been thinking about this most of the day today. I think the accelerate behaviour may also do the trick in this case without constraining anything. Try this;
    Create a boolean variable called 'MoveRight'. This can be created under Game tab by pressing the + icon where it should give you a list of different types of variables such as integer, string and real. Create another boolean called 'MoveLeft'.
    On the actor you want to move, you will need to create some rules so "if MoveRight is true, do "Accelerate" in direction 90 (which is to the right) AND MoveLeft is false. Create another rule that says "if MoveLeft is true, do "Accelerate" in direction 270 (which should be left) AND MoveRight is now false.
    Ok now on your touch button (to allow the actor to move right) you will need to add a behaviour that says if touch is pressed (your probably better off with released instead of pressed) then MoveRight = true. As you've already created a rule on the moving actor, that if MoveRight is true then MoveLeft is false so the actor should move to the right. If you add the same touch code to the left arrow but change it to MoveLeft = true then your object will move to the left.
    I'm on my phone writing this so I haven't tested it but I'm 99.9% sure this will work mate. Give me a shout and I hope this has made sense to ya

  • Mayhem_MadnessMayhem_Madness Posts: 168Member, PRO

    @Soopa Arrow said:
    Sorry about the late reply Thor. I agree with jamie_c‌ but I've been thinking about this most of the day today. I think the accelerate behaviour may also do the trick in this case without constraining anything. Try this;
    Create a boolean variable called 'MoveRight'. This can be created under Game tab by pressing the + icon where it should give you a list of different types of variables such as integer, string and real. Create another boolean called 'MoveLeft'.
    On the actor you want to move, you will need to create some rules so "if MoveRight is true, do "Accelerate" in direction 90 (which is to the right) AND MoveLeft is false. Create another rule that says "if MoveLeft is true, do "Accelerate" in direction 270 (which should be left) AND MoveRight is now false.
    Ok now on your touch button (to allow the actor to move right) you will need to add a behaviour that says if touch is pressed (your probably better off with released instead of pressed) then MoveRight = true. As you've already created a rule on the moving actor, that if MoveRight is true then MoveLeft is false so the actor should move to the right. If you add the same touch code to the left arrow but change it to MoveLeft = true then your object will move to the left.
    I'm on my phone writing this so I haven't tested it but I'm 99.9% sure this will work mate. Give me a shout and I hope this has made sense to ya

    Hi Soopa Arrow, sorry for the late reply, ive tried what you have said but its not working, have you got any other ideas? Thanks for the help

  • jamie_cjamie_c ImagineLabs.rocks Posts: 5,542Member
    edited January 2015

    Hi @Thor, I've updated the project mentioned above and provided a link here. Let me know if that is what you're looking for.

  • jorkosjorkos Posts: 352Member, PRO

    just have a global attribute (index). when press the right button, set index to 1, when press the left button, set index to 2

    on the actor, when global attribute is 1, change linear velocity x to something (ie 100), when global attribute is 2, change linear velocity x to negative something (ie. -100)

    that is the easiest way to do it

  • Mayhem_MadnessMayhem_Madness Posts: 168Member, PRO

    @jorkos said:
    just have a global attribute (index). when press the right button, set index to 1, when press the left button, set index to 2

    on the actor, when global attribute is 1, change linear velocity x to something (ie 100), when global attribute is 2, change linear velocity x to negative something (ie. -100)

    that is the easiest way to do it
    @jamie_c said:
    Hi Thor, I've updated the project mentioned above and provided a link here. Let me know if that is what you're looking for.
    @Soopa Arrow said:
    Sorry about the late reply Thor. I agree with jamie_c‌ but I've been thinking about this most of the day today. I think the accelerate behaviour may also do the trick in this case without constraining anything. Try this;
    Create a boolean variable called 'MoveRight'. This can be created under Game tab by pressing the + icon where it should give you a list of different types of variables such as integer, string and real. Create another boolean called 'MoveLeft'.
    On the actor you want to move, you will need to create some rules so "if MoveRight is true, do "Accelerate" in direction 90 (which is to the right) AND MoveLeft is false. Create another rule that says "if MoveLeft is true, do "Accelerate" in direction 270 (which should be left) AND MoveRight is now false.
    Ok now on your touch button (to allow the actor to move right) you will need to add a behaviour that says if touch is pressed (your probably better off with released instead of pressed) then MoveRight = true. As you've already created a rule on the moving actor, that if MoveRight is true then MoveLeft is false so the actor should move to the right. If you add the same touch code to the left arrow but change it to MoveLeft = true then your object will move to the left.
    I'm on my phone writing this so I haven't tested it but I'm 99.9% sure this will work mate. Give me a shout and I hope this has made sense to ya

    Thankyou jorkos, Jamie and Soopa Arrow for the help.

Sign In or Register to comment.