Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
How to make movement buttons work with events — Gideros Forum

How to make movement buttons work with events

ZizanymanZizanyman Member
edited January 2016 in General questions
I am making a game with three buttons to control a rocket. They are left, right and shoot (the screenshot is attached). However, I can't figure out how to get the movement to work. With my current method, whenever you lift any finger off the screen, it stops the ship entirely. This is bad if you tap left, then right, then release your finger off of left (or vice versa), because it should just keep moving to the right. Instead, the ship stops moving :((. It also happens when you press the fire button, once you release your finger, the ship just stops. Here is my code (I haven't programmed in the firing yet):
function game:onEnterFrame()
 
if MoveLeft == true then
		Ship:setPosition(Ship:getX()-2,Ship:getY())
		Ship:setRotation(-30)
end
if MoveRight == true then
		Ship:setPosition(Ship:getX()+2,Ship:getY())
		Ship:setRotation(30)
end
if MoveLeft == false and MoveRight == false then
	Ship:setRotation(0)
end
end
 
function game:MoveShip(event)
 
if Left:hitTestPoint(event.touch.x,event.touch.y) then
	MoveLeft = true
	MoveRight = false
end
if Right:hitTestPoint(event.touch.x,event.touch.y) then
	MoveRight = true
	MoveLeft = false
end
if Fire:hitTestPoint(event.touch.x,event.touch.y) then
	print("fire")
end
end
 
end
 
function game:StopShip(event)
 
if MoveLeft == true then
	MoveLeft = false
end
if MoveRight == true then
	MoveRight = false
end
 
end
Does anybody know how to fix this issue?

Comments

  • well so far it's not clear. probably you call StopShip somewhere else which stops the movement.
  • I know why it's stopping it, I just don't know how to work around it. The reason is that when I put my finger on left, MoveLeft becomes true, but when I put my finger on right, MoveRight becomes true. In my StopShip function, which is called when you release your finger (event listener), it thinks that because MoveRight = true that it should make it false, which in turn stops the ship.
  • Just in case it helps, here are the event listeners I have in my init function:
    self:addEventListener(Event.TOUCHES_BEGIN,self.MoveShip,self)
    self:addEventListener(Event.TOUCHES_END,self.StopShip,self)
    self:addEventListener(Event.ENTER_FRAME,self.onEnterFrame,self)
  • piepie Member
    edited January 2016
    @Zizanyman if you're right it just needs to know when to set the MoveLeft/Right to false, according to the button that has been released. Maybe you could save this info somewhere and check it in stopShip()
    Another option could be to check the hitTestPoints of the buttons directly in enterframe and act accordingly.

    If you can share your project I will try to make it work but I am too tired to setup a dummy project to make a test right now :)
  • keszeghkeszegh Member
    edited January 2016
    you could add checking which button is released in stopship too, like:
    function game:onEnterFrame()
     
    if MoveLeft == true and MoveRight==false then
    		Ship:setPosition(Ship:getX()-2,Ship:getY())
    		Ship:setRotation(-30)
    end
    if MoveRight == true and MoveLeft==false  then
    		Ship:setPosition(Ship:getX()+2,Ship:getY())
    		Ship:setRotation(30)
    end
    if MoveLeft == false and MoveRight == false then
    	Ship:setRotation(0)
    end
     
    if MoveLeft == true and MoveRight == true then
    	Ship:setRotation(0)
    end
     
    end
     
    function game:MoveShip(event)
     
    if Left:hitTestPoint(event.touch.x,event.touch.y) then
    	MoveLeft = true
    end
    if Right:hitTestPoint(event.touch.x,event.touch.y) then
    	MoveRight = true
    end
    if Fire:hitTestPoint(event.touch.x,event.touch.y) then
    	print("fire")
    end
    end
     
    end
     
    function game:StopShip(event)
     
    if Left:hitTestPoint(event.touch.x,event.touch.y) then
    	MoveLeft = false
    end
     
    if Right:hitTestPoint(event.touch.x,event.touch.y) then
    	MoveRight = false
    end
     
    end
  • this solution will stop the ship if both buttons are pressed. if you want that the button pressed later should be the important one then just add a variable lastMove and set it to left/right in moveship and then in onenterframe if both MoveLeft and MoveRight are true then set your move according to lastMove.
  • antixantix Member
    edited January 2016
    Here is a simple solution.. Just add another flag called "Moving". If it is true the rocket will move every frame. As an added bonus you only need one flag for the direction too.
    function game:onEnterFrame()
     if Moving then
       if MoveLeft then
        Ship:setPosition(Ship:getX()-2,Ship:getY()) -- MOVE ROCKET LEFT
        Ship:setRotation(-30)
       else
        Ship:setPosition(Ship:getX()+2,Ship:getY()) -- MOVE ROCKET RIGHT
        Ship:setRotation(30)
       end
     end
    end
     
    function game:MoveShip(event)
     
      if Left:hitTestPoint(event.touch.x,event.touch.y) then
        Moving = true
        MoveLeft = true
      end
     
      if Right:hitTestPoint(event.touch.x,event.touch.y) then
        Moving = true
        MoveLeft = false
      end
     
      if Fire:hitTestPoint(event.touch.x,event.touch.y) then
        print("fire")
      end
    end
     
    function game:StopShip(event)
     Moving = false
    end
    If you don't want the rocket to ever stop moving left or right then you should comment out the Moving = false line in StopShip(event)
Sign In or Register to comment.