Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Example for left right / jump controls please — Gideros Forum

Example for left right / jump controls please

Tom2012Tom2012 Guru
edited November 2012 in General questions
Been struggling with this a week and getting in a right muddle.

I want to have a character run left right and jump. I've been looking at the button class but can't get it working with multitouch.

Any examples much appreciated!

Comments

  • Tom2012Tom2012 Guru
    edited November 2012
    I did consider that but it's just buttons I want really as opposed to joystick. Think it will do that?

    Thanks for the suggestion...

    I'll download it and try out. GregBUG is a genius.
  • Tom2012Tom2012 Guru
    edited November 2012
    Here's how I think it should work... someone please correct me if wrong. :-)

    - you store each touch in a table
    - when the touch ends, check if the touch id matches one in a table
    - remove from the table
    - use the info to get # touches on each button

    ... or am I making this too complicated?
    Buttons = Core.class(Sprite)
     
    function Buttons:init(scene,upImage,downImage)
     
    self.scene = scene;
     
    local button = Bitmap.new(self.scene.atlas2:getTextureRegion(upImage));
    local buttonPressed = Bitmap.new(self.scene.atlas2:getTextureRegion(downImage));
    self:addChild(button);
     
     
    self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
    self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
    self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
     
    end
     
     
    function Buttons:onTouchesBegin(event)
     
    	if(self:hitTestPoint(event.touch.x, event.touch.y)) then
     
    		if(self.type=="upButton") then
    		table.insert(self.scene.upButtonTouches, event.touch.id)
    		end
     
    		if(self.type=="leftButton") then
    		table.insert(self.scene.leftButtonTouches, event.touch.id)
    		end
     
    		if(self.type=="rightButton") then
    		table.insert(self.scene.rightButtonTouches, event.touch.id)
    		end
     
    print("number of touches on leftButton:", #self.scene.leftButtonTouches)
    print("number of touches on rightButton:", #self.scene.rightButtonTouches)
    print("number of touches on upButton:", #self.scene.upButtonTouches)
     
    	end
     
    end
     
    -- Touch was released on one of the buttons
     
    function Buttons:onTouchesEnd(event)
     
    	if(self.type=="upButton") then
    		for i,v in pairs(self.scene.upButtonTouches) do
    			if(event.touch.id == v) then
    				table.remove(self.scene.upButtonTouches,i) -- remove this touch from table
    			end
    		end
    	end
     
    	if(self.type=="leftButton") then
    		for i,v in pairs(self.scene.leftButtonTouches) do
    			if(event.touch.id == v) then
    				table.remove(self.scene.leftButtonTouches,i) -- remove this touch from table
    			end
    		end
    	end
     
    	if(self.type=="rightButton") then
    		for i,v in pairs(self.scene.rightButtonTouches) do
    			if(event.touch.id == v) then
    				table.remove(self.scene.rightButtonTouches,i) -- remove this touch from table
    			end
    		end
    	end
     
    print("number of touches on leftButton:", #self.scene.leftButtonTouches)
    print("number of touches on rightButton:", #self.scene.rightButtonTouches)
    print("number of touches on upButton:", #self.scene.upButtonTouches)
     
    end
     
     
     
    function Buttons:onTouchesMove(event)
     
    	if(not self:hitTestPoint(event.touch.x, event.touch.y)) then
     
    		if(self.type=="upButton") then
    			for i,v in pairs(self.scene.upButtonTouches) do
    				if(event.touch.id == v) then
    					table.remove(self.scene.upButtonTouches,i) -- remove this touch from table
    				end
    			end
    		end
     
    		if(self.type=="leftButton") then
    			for i,v in pairs(self.scene.leftButtonTouches) do
    				if(event.touch.id == v) then
    					table.remove(self.scene.leftButtonTouches,i) -- remove this touch from table
    				end
    			end
    		end
     
    		if(self.type=="rightButton") then
    			for i,v in pairs(self.scene.rightButtonTouches) do
    				if(event.touch.id == v) then
    					table.remove(self.scene.rightButtonTouches,i) -- remove this touch from table
    				end
    			end
    		end
     
    	end
     
    print("number of touches on leftButton:", #self.scene.leftButtonTouches)
    print("number of touches on rightButton:", #self.scene.rightButtonTouches)
    print("number of touches on upButton:", #self.scene.upButtonTouches)
     
     
     
     
    -- A new touch moved over this button
     
    	if(self:hitTestPoint(event.touch.x, event.touch.y) and self.type=="upButton") and #self.scene.upButtonTouches==0 then
    	table.insert(self.scene.upButtonTouches, event.touch.id)
    	end
     
    	if(self:hitTestPoint(event.touch.x, event.touch.y) and self.type=="leftButton") and #self.scene.leftButtonTouches==0 then
    	table.insert(self.scene.leftButtonTouches, event.touch.id)
    	end
     
    	if(self:hitTestPoint(event.touch.x, event.touch.y) and self.type=="rightButton") and #self.scene.rightButtonTouches==0 then
    	table.insert(self.scene.rightButtonTouches, event.touch.id)
    	end
     
    print("number of touches on leftButton:", #self.scene.leftButtonTouches)
    print("number of touches on rightButton:", #self.scene.rightButtonTouches)
    print("number of touches on upButton:", #self.scene.upButtonTouches)
     
     
     
    end
  • I think that with Vpad you can use any combination of buttons ad joysticks, like two joysticks or only buttons, etc
  • ScouserScouser Guru
    edited November 2012
    @ar2rsawseen: You are correct, you use it as
    local vPad = CTNTVirtualPad.new(stage, "tntskinpad",  PADMODE.NO_STICK , PADBUTTONS.THREE, 20)
    which will give you a 3 button control (maybe left, right & jump)
  • Ah, thank you both. You just saved my remaining hair from being pulled out.
Sign In or Register to comment.