Quick Links: Download Gideros Studio | Gideros Documentation | Gideros Development Center | Gideros community chat | DONATE
Super Basic Question — Gideros Forum

Super Basic Question

joethemojojoethemojo Member
edited February 2014 in General questions
I'm very new to Gideros and I have looked around for this answer but I'm getting a little frustrated.

I'm starting off very basic.

Id like to move a sprite with the arrow keys on the keyboard.
and after you release to key the sprite still moves a little in that direction.

I have no idea how to do this.
Ive looked in the reference and I think I somehow merge these 2 codes?

function onMouseDown(event)
print(event.x, event.y) <--- but a translate function?
end
mysprite:addEventListener(Event.MOUSE_DOWN, onMouseDown)

with

stage:addEventListener(Event.KEY_DOWN, function(event)
if event.keyCode == KeyCode.BACK then
application:exit() <----- with translate function?
end
end)

Then I think another event for when I let go of the key?


If anyone could help me Id greatly appreciate this.

Joe

Comments

  • Here's an example of how act upon key up/down. As far as moving a sprite... that would be separate functionality.
     
    -- The sprite that will listen for key up/down events
     
    local sprite = Sprite.new()
    stage:addChild(sprite)
     
     
     
    -- Assign Up arrow as our move key
     
    local moveKey = KeyCode.UP
     
     
     
     
    -- Our function when key is pressed
     
    local function move()
     
    	print("begin moving")
    end
     
    -- Our funcion when key is released
     
    local function stop()
     
    	print("begin stop moving")
    end
     
     
     
     
    -- Our listener function for when key is Down
     
    local function onKeyDown(event)
     
    	if event.keyCode == moveKey then
     
    		move()
     
    	end
    end
     
    -- Our listener function for when key goes Up
     
    local function onKeyUp(event)
     
    	if event.keyCode == moveKey then
     
    		stop()
     
    	end
    end
     
     
     
    -- Our event listeners to start the actions
     
    sprite:addEventListener(Event.KEY_DOWN, onKeyDown, event)
    sprite:addEventListener(Event.KEY_UP, onKeyUp, event)
  • For moving sprites...

    There's probably a bunch of examples in these forums... but there are basically two ways.

    The first way is to use box2d if you require sprites to interact and collide with each other. Box2d at first can be a bit overwhelming, but extremely valuable once you get the hang of it.

    The second is something like this (using my example above):
    -- The sprite that will listen for key up/down events
     
    local sprite = Sprite.new()
    stage:addChild(sprite)
     
     
    -- Define attributes of move action for sprite
     
    local fps = 60
    local speed = 200
    local drag = 0.05
    local stopAtSpeed = 5
     
     
    -- The moving function that will run on every frame
     
    local sx = stopAtSpeed * (1 / fps)
    local dx, dsx, x = 0, 0, 0
     
    local function moving()
     
    	-- get next amount to move
    	dx = dx * (1 - dsx)
     
    	-- new position = last position + next amount to move
    	x = x + dx
    	sprite:setX(x)
     
    	-- if next amount to move is less than this, kill listener
    	if math.abs(dx) < sx then
     
    		if sprite:hasEventListener(Event.ENTER_FRAME) then
    			sprite:removeEventListener(Event.ENTER_FRAME, moving)
    		end
    	end
     
    	print(x, dx)
    end
     
    --------------------------------------------------
     
    -- Assign Up arrow as our move key
     
    local moveKey = KeyCode.UP
     
     
     
    -- Our function when key is pressed
     
    local function move()
     
    	-- No drag when moving
    	dsx = 0
     
    	-- Set our amount to move to full
    	dx = speed * (1 / fps)
     
    	-- Get starting position
    	x = sprite:getX()
     
    	-- Start listening at every frame
    	if not sprite:hasEventListener(Event.ENTER_FRAME) then
    		sprite:addEventListener(Event.ENTER_FRAME, moving)
    	end
    end
     
    -- Our funcion when key is released
     
    local function stop()
     
    	-- Apply drag
    	dsx = drag
    end
     
     
     
     
    -- Our listener function for when key is Down
     
    local function onKeyDown(event)
     
    	if event.keyCode == moveKey then
     
    		move()
     
    	end
    end
     
    -- Our listener function for when key goes Up
     
    local function onKeyUp(event)
     
    	if event.keyCode == moveKey then
     
    		stop()
     
    	end
    end
     
     
     
    -- Our event listeners to start the actions
     
    sprite:addEventListener(Event.KEY_DOWN, onKeyDown, event)
    sprite:addEventListener(Event.KEY_UP, onKeyUp, event)
  • @joethemojo,
    you are aware that not all mobile devices would have and respond to the arrow keys.
    twitter: @ozapps | http://www.oz-apps.com | http://howto.oz-apps.com | http://reviewme.oz-apps.com
    Author of Learn Lua for iOS Game Development from Apress ( http://www.apress.com/9781430246626 )
    Cool Vizify Profile at https://www.vizify.com/oz-apps
  • john26john26 Maintainer
    A KEY_DOWN event only fires when the user first presses the key down. When the user releases the key, a KEY_UP event occurs. But in between there are no events. It's important to realize the system will not constantly send you a series of KEY_DOWN events. So you need a boolean variable eg keypressed which you set to true when you get KEY_DOWN and false when you get KEY_UP. Then, you use an ENTER_FRAME listener to update the sprite position, eg

    if keypressed then sprite:setX(sprite:getX()+1) end.

    Same deal for MOUSE_DOWN, MOUSE_UP.
Sign In or Register to comment.