Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Vertical plane movement with touch events — Gideros Forum

Vertical plane movement with touch events

DikkesnoekDikkesnoek Member
edited December 2015 in General questions
Hello Gideros friends,

At I am working on a horizontal shooter. Controlling the plane smootly in vertical direction is quiet a challenge. This is what I want to accomplish. When I move my thumb vertically (up/down) over a part of the screen, the plane must be controlled from top to bottom. Has anyone some tips/hints to do this? Below you can find a piece of my code wich will do a part of the job:
 
function ShootGame:onTouchesMove(event)
 
	local planeMinY = 0 -- Plane can reach the top.
	local planeMaxY = resolution:getGameHeight() - plane:getHeight()	-- Plane can reach the bottom.
	local planeMovement = 15
 
	if (plane.touchId == event.touch.id) and plane.dragging then
 
		local dx = event.touch.x - startTouchX
		local dy = event.touch.y - startTouchY		
 
		-- Get the absolute movement in x and y direction.
		local fingerMovementXY = math.floor(math.sqrt(math.pow(dx, 2) + math.pow(dy, 2)))
 
		-- Finger up more than 1px.
		if (dy < - 1)  then
			targetPlaneY = plane.y - (fingerMovementXY * planeMovement) -- New plane position.
			if targetPlaneY < planeMinY then
				targetPlaneY = planeMinY
			end
 
			startTouchX = event.touch.x	-- Set new startX.
			startTouchY = event.touch.y	-- Set new startY.
		end
		-- Finger down more than 1px.
		if (dy > 1) then
			targetPlaneY = plane.y + (fingerMovementXY * planeMovement)
			if targetPlaneY > planeMaxY then
				targetPlaneY = planeMaxY
			end
 
			startTouchX = event.touch.x	-- Set new startX.
			startTouchY = event.touch.y	-- Set new startY.
 		end
	end
end
 
function ShootGame:onEnterFrame()
// THIS IS A PART OF THE COMPLETE FUNCTION
 
		-- Move the plane in vertical direction.
		if (plane ~= nil) then
			if (plane.y > targetPlaneY) then	-- Move down.
				plane.y = plane.y - 2
			end
			if (plane.y < targetPlaneY) then	-- Move up.
				plane.y = plane.y + 2
			end		
			plane:setPosition(plane.x, plane.y)
		end
end
It will work, but it is not moving in a nice way. Do think I need to use some math functions to move the plane more smoothly? Thanks in advance,

Marc

Comments

  • @Dikkesnoek do you really want to move it with constant +2/-2 units?
    Wouldn't you want to move it to the amount fingers have moved vertically?
  • DikkesnoekDikkesnoek Member
    edited December 2015
    Hi ar2rsawseen,

    I was just experimenting. The plane is in a fixed horizontal position. The background is scrolling. By placing your thumb on the right side of the screen you can move the plane vertically. Challenge is to let the plane move smoothly from top to bottom by moving your finger over a small range. So for example if I have a resolution of 1500x500, when I move my thumb 1 pixel the plane has to move 10 pixels and when I move it 25 pixels it will be move to top or bottom (250 pixels). I am not shure what the optimum values are. See it as an analog joystick.

    Best regards,

    Marc
  • Hi.
    the onTouchesMove method is not a proper place for moving spaceship because event.x and event.y is not consistent and smooth.
    Try onEnterFrame and place your logic there.
    you can obtain starting place from onTouchesDown.
    Create a table with 10-20 member and save (currentPlace - startingPlace) there and update it in onTouchesMove.
    The average of this table can be the input for onEnterFrame.
  • Hi,

    Thanks for your reply. In the code above the fingermovement will be determined in the onTouchedMove. The planemovement will be done in the onEnterFrame.

    Regards,

    Marc
Sign In or Register to comment.