Problem with Image Flip

I set up my character to flip its image once the “a” key is pressed in this function:

function onKeyEvent( event ) -- Print which key was pressed down/up to the log. --local message = "'" .. event.keyName .. "' is " .. event.phase --print( message ) -- Display the key event's information onscreen. --if event.device then -- message = event.device.displayName .. "\n" .. message --end -- If the "back" key was pressed, then prevent it from backing out of the app. -- We do this by returning true, telling the operating system that we are overriding the key. --if (event.keyName == "back") then -- return true --end -- Return false to indicate that this app is \*not\* overriding the received key. -- This lets the operating system execute its default handling of this key. --return false if event.keyName == "a" then if event.phase == "down" then transition.to(character, {time = 3000, x = character.x - (screenW / 2) - 10}) character:scale(-1, 1) elseif event.phase == "up" then transition.cancel() character:scale(1, 1) end end if event.keyName == "d" then if event.phase == "down" then transition.to(character, {time = 3000, x = character.x + (screenW / 2) + 10}) elseif event.phase == "up" then transition.cancel() end end end

So, I have this function running on an enterFrame event listener and whenever I press ‘a’ the character glitches out and continuously flips its image as I hold down the “a” key. I understand why this is happening, since it is on an enterFrame it would run the code multiple times. I want to know an alternative to doing this. Thank you.

It looks like this:

https://www.youtube.com/watch?v=_2LDpndH_6k&feature=youtu.be

So, I have this function running on an enterFrame event listener 

Could you show your code?

Do you use Runtime:addEventListener( “key”, onKeyEvent )?

I don’t know you mean. That is why I want see your code.

Yes.

Well, it was not an eventFrameListener… but it was Runtime:addEventListener(“key”, onKeyEvent)

Just to be clear, I want to have the same functionality, but only run the character:scale() code once. Also, is there another method of flipping the image instead of object:scale()? I remember there was, but I forgot it.

I only try help you and understand where is problem:)

whenever I press ‘a’ the character glitches out and continuously flips its image as I hold down the “a” key. I understand why this is happening

So tell me. I don’t get how onKeyEvent and enterFrame listener are “connected” in your code.

It seems character:scale() runs multiple times but it should not. I tested key event listener and I get only one event (event.phase == “down”) as I hold down the “a” key. Maybe you add more than one keyEventListnere instruction.   

You can use character.xScale = -1 instead of character:scale(-1, 1)

It seems the xScale code worked, thanks for the help. Also, I had not made myself very clear, I apologize. Could you help in my other topic please: 

https://forums.coronalabs.com/topic/66996-invoking-movement-based-on-which-key-is-pressed/

Thanks for the help.

The xScale was the other method I was looking for.

So, I have this function running on an enterFrame event listener 

Could you show your code?

Do you use Runtime:addEventListener( “key”, onKeyEvent )?

I don’t know you mean. That is why I want see your code.

Yes.

Well, it was not an eventFrameListener… but it was Runtime:addEventListener(“key”, onKeyEvent)

Just to be clear, I want to have the same functionality, but only run the character:scale() code once. Also, is there another method of flipping the image instead of object:scale()? I remember there was, but I forgot it.

I only try help you and understand where is problem:)

whenever I press ‘a’ the character glitches out and continuously flips its image as I hold down the “a” key. I understand why this is happening

So tell me. I don’t get how onKeyEvent and enterFrame listener are “connected” in your code.

It seems character:scale() runs multiple times but it should not. I tested key event listener and I get only one event (event.phase == “down”) as I hold down the “a” key. Maybe you add more than one keyEventListnere instruction.   

You can use character.xScale = -1 instead of character:scale(-1, 1)

It seems the xScale code worked, thanks for the help. Also, I had not made myself very clear, I apologize. Could you help in my other topic please: 

https://forums.coronalabs.com/topic/66996-invoking-movement-based-on-which-key-is-pressed/

Thanks for the help.

The xScale was the other method I was looking for.