Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

How to move display object on screen when arrow keys pressed.
Started by sheikhameen123 Mar 15 2019 08:51 PM

4 replies to this topic
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

sheikhameen123

[GLOBAL: userInfoPane.html]
sheikhameen123
  • Observer

  • 16 posts
  • Corona SDK

I am trying to move my object in the screen in all different direction corresponding to the arrow keys. 


local mkbhd = display.newImageRect("MKBHD.jpg", 100, 100)
mkbhd.x = ccx
mkbhd.y = ccy
physics.addBody( mkbhd, "dynamic", {bounce=0})

local function onKeyEvent(event)
    if ( event.keyName == "right" ) then
        mkbhd.x = mkbhd.x + 10
    elseif ( event.keyName == "left" ) then
        mkbhd.x = mkbhd.x - 10
    elseif ( event.keyName == "up") then
        mkbhd.y = mkbhd.y - 10
    elseif ( event.keyName == "down" ) then
        mkbhd.y = mkbhd.y + 10
    end
end


Runtime:addEventListener( "key", onKeyEvent )

It doesn't work like I want it to. I don't want to press the key repeatedly, but instead the object should move as I hold the key. I looked up online and found some stuff called enterFrame and applyLinearImpulse. Not sure if these would help me.. If it does, how do I use it?

(...and don't mind mkbhd :D. This is just a test. Im playin' around...)



[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 674 posts
  • Corona SDK

As you said, you probably want to go with https://docs.coronalabs.com/api/event/enterFrame/index.html.

Also, seeing the frequency and the level of your questions, I'd recommend that you go through the tutorials to learn a bit more about programming and Corona in general.

Now, concerning your issue, key events in Corona only track if a button press is "up" or "down", i.e. is a button being pressed or let go.

This means taht you could create a function that is constantly checking which button is pressed down (if any). You'd use enterFrame with this function. In order to track the button, you could create a variable and assign that variable a string value in your onKeyEvent function, i.e. when the user presses down the "left" key, set the variable to "left". When the user stops pressing the button, you'd set the variable back to nil.

Then, in your function, you simply check if the variable is not nil, i.e. if some arrow button is being pressed, and then just move mkbhd via translate or just adjusting it's coordinates.



[TOPIC: post.html]
#3

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 184 posts
  • Corona SDK

hi,

 

try this :

local direction = "noMove"
Runtime:addEventListener( "enterFrame", function()
	if direction =="right" then
	    mkbhd.x = mkbhd.x + 10	
	elseif ( direction== "left" ) then
	    mkbhd.x = mkbhd.x - 10
	elseif ( direction== "up") then
	    mkbhd.y = mkbhd.y - 10
	elseif ( direction== "down" ) then
	    mkbhd.y = mkbhd.y + 10
	end
end)

Runtime:addEventListener( "key",function(event)
	if event.phase=="up" then
		direction="noMove"
	elseif event.phase=="down" then
		direction=event.keyName
	end
end)

Don't forgot to remove, pause and resume the Runtime event when the player is removed.

Same for the key event.

Yvan


  • mertalyaprak44 likes this

[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Contributor

  • 674 posts
  • Corona SDK

Your code does pretty much what I suggested, but with your method he'll have trouble removing the event listeners when he no longer has any use for them, e.g. when the scene changes or the game is paused, etc.

Still, with basic questions like this one, especially for new programmers, I personally prefer to nudge people in the right direction or tell them what they need to do instead of providing them with the code as this'll hinder their learning (and googling skills).



[TOPIC: post.html]
#5

yvandotet

[GLOBAL: userInfoPane.html]
yvandotet
  • Contributor

  • 184 posts
  • Corona SDK

Yes he looks like pretty newby till he don't know what is an enterFrame listener and linearImpulse but It will to unlock his situation.

He can learn from this.




[topic_controls]
[/topic_controls]