Control of the character on the keyboard

Hello. I am new in corona SDK. How to do character management on the keyboard? I use the following code:

Runtime:addEventListener("key", walkPerson); 

I use the following code in the walkPerson function:

local function walkPerson(event) if (event.keyName == 'd' and event.phase == 'down') then person.x = person.x + 15 return true end if (event.keyName == 'a' and event.phase == 'down') then person.x = person.x - 15 return true end if (event.keyName == 'w' and event.phase == 'down') then person.y = person.y - 64 person:setLinearVelocity( 0, 128 ) return true end if (event.keyName == 's' and event.phase == 'down') then person:setLinearVelocity( 0, 32 ) return true end return false end

This code allows you to move the character 1 time. How do I move the character while the key is pressed?

Tried to do it through the cycle, but the program starts to get hung up. 

Sorry for my English.

We  have a tutorial on continuous actions that can help with this:  https://docs.coronalabs.com/tutorial/events/continuousActions/index.html

I would look at either the “Frame Based movement” or the “Physics based movement” section.

But to sum it up, you don’t actually move the character with the key strokes. Instead, you use those actions when you detect the key is down to set a flag indicating the direction of movement and when you detect the key has been released (“up”) then you set the movement flag to 0.

Then you have a “moveCharacter()” function that is part of an enterFrame listener that applies the current direction of movement to your character. So when the key is down, there is a direction of movement to be applied, when the key is up, it’s zero so your character won’t move.

Rob 

Thank you very much for the answer!

We  have a tutorial on continuous actions that can help with this:  https://docs.coronalabs.com/tutorial/events/continuousActions/index.html

I would look at either the “Frame Based movement” or the “Physics based movement” section.

But to sum it up, you don’t actually move the character with the key strokes. Instead, you use those actions when you detect the key is down to set a flag indicating the direction of movement and when you detect the key has been released (“up”) then you set the movement flag to 0.

Then you have a “moveCharacter()” function that is part of an enterFrame listener that applies the current direction of movement to your character. So when the key is down, there is a direction of movement to be applied, when the key is up, it’s zero so your character won’t move.

Rob 

Thank you very much for the answer!