简体   繁体   中英

Corona SDK, temporary disable eventListeners while object is moving

I'm trying to make fifteen like game on Corona. I creating a tiles and assign to every tile an eventListener which detect swipe and swipe's detection and then performing a call to function which moves a tile. I encountered a problem, if I make swipe during my tile's moving it changes its direction and its behaviour unacceptable. For example I need to move it 90px down but when it moved 45px down If I swipe right it'll move 45px down and 45 px right. How can I temporary disable eventListener to avoid this behaviour? I have asked yet question regarding this project and code is here , therefor I don't repost it. Thank you very much.

You are better off to have a flag that determines if your box accepts event while flag is true (some of the following is pseudocode):

function swipeListener(event)
    if moving then return end -- ignore swipe while moving
    setup motion 
    moving = true
end

You will need a way of knowing when the motion has completed, such as an event indicating "animation complete" or perhaps an enterFrame event handler in which you check if your box has reached distination, if yes you set moving = false:

function swipeListener(event)
    if moving then return end -- ignore swipe while moving
    setup motion 
    Runtime:addEventListener(enterFrame, "enterFrame")
    moving = true
end

function enterFrame(event)
    if moving then 
        check if motion done
        if yes then 
            moving = false
            stop motion 
            Runtime:removeEventListener(enterFrame, "enterFrame")
        end
    end
end

You might have other way of knowing when motion is done so some of above may not apply but you get the idea.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM