Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Optimizing touch events — Gideros Forum

Optimizing touch events

sLKzsLKz Member
edited March 2015 in Suggestions & requests
Hello!
I'm currently trying to make a small application.
This app has a cube rendered on the screen. When the user touches the first half of the screen, the cube should move to the left. When the user touches the right half of the screen, the cube should move to the right.

The only problem I have is that the events aren't working as I expected. I mean, when I touch the left side and then immediately touch the right side, the cube stops moving.

If anyone knows how to solve this issue, I would be really grateful.

Here is the code:
local deviceWidth = application:getDeviceWidth()
local deviceHeight = application:getDeviceHeight()
 
local cube = Shape.new()
cube:setFillStyle(Shape.SOLID, 0xff0000, 1)
 
cube:beginPath()
cube:moveTo(0, 0)
cube:lineTo(50, 0)
cube:lineTo(50, 50)
cube:lineTo(0, 50)
cube:lineTo(0, 0)
cube:endPath()
 
cube:setPosition(deviceWidth / 2 - 50 / 2, deviceHeight - 100)
 
local move = false
local right = false
local left = false
 
local function onMouseDown(event)
	move = true
 
	if event.x >= deviceWidth / 2 then
		right = true
		left = false
	else
		left = true
		right = false
	end
end
 
local function onMouseUp(event)
	move = false
end
 
local function onEnterFrame()
	if move then
		if right then
			cube:setX(cube:getX() + 10)
		elseif left then
			cube:setX(cube:getX() - 10)
		end
	end
end
 
stage:addEventListener(Event.MOUSE_DOWN, onMouseDown)
stage:addEventListener(Event.MOUSE_UP, onMouseUp)
stage:addEventListener(Event.ENTER_FRAME, onEnterFrame)
 
stage:addChild(cube)

Comments

Sign In or Register to comment.