Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Multiple Touches — Gideros Forum

Multiple Touches

ruxpinruxpin Member
edited November 2011 in General questions
I am working on a two handed pong game, but I am not understanding how to get multiple touches working. I have two paddles on the screen and was originally using mouse event functions, but figured out that you can only move one sprie at a time that way. Then I tried to simply switch the mouse event functions to touch event functions, but that didn't work either. I followed and understand the Touch Explorer example, but I can't wrap my head around how to apply it to a sprite instead of the stage.

Here is my Paddle class and their mouse event functions. Can I simply change those to touch event functions or do I need to do something else?
Paddle = gideros.class(Sprite)
 
--Input (position) = Should the paddle be on the right or the left of the screen?
function Paddle:init(position)
	bitmap = Bitmap.new(Texture.new("textures/paddle.png"))
	self:addChild(bitmap)
 
	self:setSize(64, 64)
 
	--Sets the paddle position
	if position == "right" then
		self:setX(stage:getWidth() - self:getWidth() - 24)
	end
	if position == "left" then
		self:setX(24)
	end
 
	self.isFocus = false
 
	self:addEventListener(Event.ENTER_FRAME, self.onEnterFrame, self)
 
	self:addEventListener(Event.MOUSE_DOWN, onMouseDown, self)
	self:addEventListener(Event.MOUSE_MOVE, onMouseMove, self)
	self:addEventListener(Event.MOUSE_UP, onMouseUp, self)
end
 
function Paddle:onEnterFrame(event)
	local x, y = self:getPosition()
 
	if y < 0 then
		y = 0
	end
 
	if y > application:getLogicalWidth() - self:getHeight() then
		y = application:getLogicalWidth()- self:getHeight()
	end
 
	self:setPosition(x, y)
end
 
function onMouseDown(self, event)
	if self:hitTestPoint(event.x, event.y) then
		self.isFocus = true
 
		self.y0 = event.y
 
		event:stopPropagation()
	end
end
 
function onMouseMove(self, event)
	if self.isFocus then
		local dy = event.y - self.y0
 
		self:setY(self:getY() + dy)
 
		self.y0 = event.y
 
		print(self:getY())
		print(application:getLogicalHeight())
 
		event:stopPropagation()
	end
end
 
function onMouseUp(self, event)
	if self.isFocus then
		self.isFocus = false
		event:stopPropagation()
	end
end

Comments

  • atilimatilim Maintainer
    edited November 2011
    Hi,

    In your case, each paddle should store a "touchid" instead of "isFocus" and only use the touch event whose id is equal to the paddle's touchid.

    Here is an example:
    Box = gideros.class(Sprite)
     
    function Box:init()
        local bitmap = Bitmap.new(Texture.new("box.png"))
        self:addChild(bitmap)
     
        self:addEventListener(Event.TOUCHES_BEGIN, self.onTouchesBegin, self)
        self:addEventListener(Event.TOUCHES_MOVE, self.onTouchesMove, self)
        self:addEventListener(Event.TOUCHES_END, self.onTouchesEnd, self)
        self:addEventListener(Event.TOUCHES_CANCEL, self.onTouchesCancel, self)
    end
     
    function Box:onTouchesBegin(event)
        if self.touchid == nil then    -- if there isn't any touches assigned to this object
            for i=1,#event.touches do
                local touch = event.touches[i]
                if self:hitTestPoint(touch.x, touch.y) then    -- if any of the touches hit the object
                    self.x0 = touch.x
                    self.y0 = touch.y
                    self.touchid = touch.id
     
                    event:stopPropagation()        -- don't allow some other object get the same touch id
                    do return end                -- don't test other touches
                end
            end
        end
    end
     
    function Box:onTouchesMove(event)
        for i=1,#event.touches do
            local touch = event.touches[i]        
            if touch.id == self.touchid then    -- if this is our touch, then move the object around
                local dx = touch.x - self.x0
                local dy = touch.y - self.y0
     
                self:setX(self:getX() + dx)
                self:setY(self:getY() + dy)
     
                self.x0 = touch.x
                self.y0 = touch.y
            end
        end
    end
     
    function Box:onTouchesEnd(event)
        for i=1,#event.touches do
            local touch = event.touches[i]
            if touch.id == self.touchid then    -- if our touch ended
                self.touchid = nil
            end
        end
    end
     
    function Box:onTouchesCancel(event)
        self:onTouchesEnd(event)        -- same with touchesEnd
    end
Sign In or Register to comment.