Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

While key is pressed
Started by owlondrugs Jan 04 2020 01:15 PM

6 replies to this topic

Best Answer XeduR @Spyric , 05 January 2020 - 04:53 AM

Your title says "while" and body says "every time the button is pressed", so it's not 100% clear which you mean.

 

Simplest approach would be be to just toggle a function's listener on or off depending on whether a desired key is held down or not and then adjust the direction based on what key was last pressed. Something along these lines:

local value, change = 1000
local function updateValue()
	value = value + change
	print( value )
end

local function onKeyEvent( event )
    if event.keyName == "down" then
        if event.phase == "down" then
		change = -1
		Runtime:addEventListener( "enterFrame", updateValue )
	else
		Runtime:removeEventListener( "enterFrame", updateValue )
	end
    elseif event.keyName == "up" then
        if event.phase == "down" then
		change = 1
		Runtime:addEventListener( "enterFrame", updateValue )
	else
		Runtime:removeEventListener( "enterFrame", updateValue )
	end
    end
    return false
end

Runtime:addEventListener( "key", onKeyEvent )

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

owlondrugs

[GLOBAL: userInfoPane.html]
owlondrugs
  • Observer

  • 9 posts
  • Corona SDK

How do I make the action happen every time the button is pressed?

local function onKeyEvent( event )
    if ( event.keyName == "up" ) then
         ballvel = ballvel - 0.1;
    end  

    if ( event.keyName == "down" ) then
     ballvel = ballvel + 0.1;
    end

    return true
end


Runtime:addEventListener( "key", onKeyEvent )

function update(event)
  balloon.y = balloon.y + ballvel;
  print(ballvel);
end

Runtime:addEventListener("enterFrame", update) 


[TOPIC: post.html]
#2

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,270 posts
  • Corona SDK

  Best Answer

Your title says "while" and body says "every time the button is pressed", so it's not 100% clear which you mean.

 

Simplest approach would be be to just toggle a function's listener on or off depending on whether a desired key is held down or not and then adjust the direction based on what key was last pressed. Something along these lines:

local value, change = 1000
local function updateValue()
	value = value + change
	print( value )
end

local function onKeyEvent( event )
    if event.keyName == "down" then
        if event.phase == "down" then
		change = -1
		Runtime:addEventListener( "enterFrame", updateValue )
	else
		Runtime:removeEventListener( "enterFrame", updateValue )
	end
    elseif event.keyName == "up" then
        if event.phase == "down" then
		change = 1
		Runtime:addEventListener( "enterFrame", updateValue )
	else
		Runtime:removeEventListener( "enterFrame", updateValue )
	end
    end
    return false
end

Runtime:addEventListener( "key", onKeyEvent )

  • pixec likes this

[TOPIC: post.html]
#3

owlondrugs

[GLOBAL: userInfoPane.html]
owlondrugs
  • Observer

  • 9 posts
  • Corona SDK

 

Your title says "while" and body says "every time the button is pressed", so it's not 100% clear which you mean.

 

Simplest approach would be be to just toggle a function's listener on or off depending on whether a desired key is held down or not and then adjust the direction based on what key was last pressed. Something along these lines:

local value, change = 1000
local function updateValue()
	value = value + change
	print( value )
end

local function onKeyEvent( event )
    if event.keyName == "down" then
        if event.phase == "down" then
		change = -1
		Runtime:addEventListener( "enterFrame", updateValue )
	else
		Runtime:removeEventListener( "enterFrame", updateValue )
	end
    elseif event.keyName == "up" then
        if event.phase == "down" then
		change = 1
		Runtime:addEventListener( "enterFrame", updateValue )
	else
		Runtime:removeEventListener( "enterFrame", updateValue )
	end
    end
    return false
end

Runtime:addEventListener( "key", onKeyEvent )

 I have a balloon.

It always moves up or down depending on its ballvel variable.

when a key is pressed, this variable changes.

How do I make the variable change not in steps, but all the time while the key is pressed?



[TOPIC: post.html]
#4

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,270 posts
  • Corona SDK

How? By reading my previous answer and adopting it to yours.


  • pixec likes this

[TOPIC: post.html]
#5

pixec

[GLOBAL: userInfoPane.html]
pixec
  • Contributor

  • 222 posts
  • Corona SDK

lol :D

[TOPIC: post.html]
#6

owlondrugs

[GLOBAL: userInfoPane.html]
owlondrugs
  • Observer

  • 9 posts
  • Corona SDK

How? By reading my previous answer and adopting it to yours.

All the same, adding to the variable goes in steps.



[TOPIC: post.html]
#7

XeduR @Spyric

[GLOBAL: userInfoPane.html]
XeduR @Spyric
  • Corona Geek

  • 1,270 posts
  • Corona SDK

I'm not sure what you mean. My code increases or decreases the value as long as a key is held down. Have you even tried running it?




[topic_controls]
[/topic_controls]