【问题标题】:Executing functions one after another properly in Corona?在 Corona 中一个接一个地正确执行功能?
【发布时间】:2012-05-18 12:46:19
【问题描述】:

所以我已经尝试了几天来解决这个问题。我正在用 Corona 制作一种简单的块代码游戏,但我无法让播放按钮的功能按顺序实际播放。我有九个不同的盒子,它们应该一个接一个地播放放入其中的动作,但相反,所有的东西都在一个大杂烩中一起播放。 它的代码有点长,因为它应该检测哪些块函数存在或不存在。

---------------------------------------------

-----button image------

local play = display.newImage ("GoButton.png")
    play.x = display.contentWidth /2 - 140
    play.y = display.contentHeight /2 + 20

------later used Variables---

local b1 = 0
local b2 = 0
local b3 = 0
local b4 = 0
local b5 = 0
local b6 = 0
local b7 = 0
local b8 = 0
local b9 = 0

-----Play button code-----


function play:tap ( event ) 


function play1 ()
-------------------------Box 1------------------------------------------
    if b1 == 0 then
        Mascot.alpha = 1
        end

    if b1 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b1 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b1 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b1 == 4 then
        Mascot.alpha = 0.5
    end

    if b1 == 5 then
            Mascot.xScale = -1
        end
    end


function play2 ()
-------------------------Box 2------------------------------------------
    if b2 == 0 then
        Mascot.alpha = 1
        end

    if b2 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b2 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b2 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b2 == 4 then
        Mascot.alpha = 0.5
    end

    if b2 == 5 then
            Mascot.xScale = -1
        end


end
timer.performWithDelay( 5000, play2)

function play3 ()
-------------------------Box 3------------------------------------------
    if b3 == 0 then
        Mascot.alpha = 1
        end

    if b3 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b3 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b3 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b3 == 4 then
        Mascot.alpha = 0.5
    end

    if b3 == 5 then
            Mascot.xScale = 1
        end
end
timer.performWithDelay( 5000, play3)

function play4 ()
-------------------------Box 4------------------------------------------
    if b4 == 0 then
        Mascot.alpha = 1
        end

    if b4 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b4 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b4 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b4 == 4 then
        Mascot.alpha = 0.5
    end

    if b4 == 5 then
            Mascot.xScale = -1
        end

end
timer.performWithDelay( 5000, play4)

function play5 ()
-------------------------Box 5------------------------------------------
    if b5 == 0 then
        Mascot.alpha = 1
        end

    if b5 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b5 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b5 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b5 == 4 then
        Mascot.alpha = 0.5
    end

    if b5 == 5 then
            Mascot.xScale = -1
        end

end
timer.performWithDelay( 5000, play5)

function play6 ()
-------------------------Box 6------------------------------------------
    if b6 == 0 then
        Mascot.alpha = 1
        end

    if b6 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b6 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b6 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b6 == 4 then
        Mascot.alpha = 0.5
    end

    if b6 == 5 then
            Mascot.xScale = -1
        end
end
timer.performWithDelay( 5000, play6)

function play7 ()
-------------------------Box 7------------------------------------------
    if b7 == 0 then
        Mascot.alpha = 1
        end

    if b7 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b7 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b7 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b7 == 4 then
        Mascot.alpha = 0.5
    end

    if b7 == 5 then
            Mascot.xScale = -1
        end

end
timer.performWithDelay( 5000, play7)

function play8 ()
-------------------------Box 8------------------------------------------
    if b8 == 0 then
        Mascot.alpha = 1
        end

    if b8 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b8 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b8 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b8 == 4 then
        Mascot.alpha = 0.5
    end

    if b8 == 5 then
            Mascot.xScale = -1
        end
end
timer.performWithDelay( 5000, play8)

function play9 ()
-------------------------Box 9------------------------------------------
    if b9 == 0 then
        Mascot.alpha = 1
        end

    if b9 == 1 then
        Mascot:applyLinearImpulse( 1, 0, Mascot.x, Mascot.y )
        end

    if b9 == 2 then
        Mascot:applyLinearImpulse( -1, 0, Mascot.x, Mascot.y )
    end

    if b9 == 3 then 
        Mascot:applyLinearImpulse( 0, -1, Mascot.x, Mascot.y )
    end

    if b9 == 4 then
        Mascot.alpha = 0.5
    end

    if b9 == 5 then
            Mascot.xScale = -1
        end 

 b1 = 0
 b2 = 0
 b3 = 0
 b4 = 0
 b5 = 0
 b6 = 0
 b7 = 0
 b8 = 0
 b9 = 0

    if walk == 1 then
        walkSpawn:removeSelf()
        walk = 0
    end

    if attack == 1 then
        attackSpawn:removeSelf()
        attack = 0
    end

    if jump == 1 then
        jumpSpawn:removeSelf()
        jump = 0
    end

    if flip == 1 then
        flipSpawn:removeSelf()
        flip = 0
    end

    if face == 1 then
        faceSpawn:removeSelf()
        face = 0
    end

end
timer.performWithDelay( 5000, play9)

print ("derp")

Runtime:addEventListener("enterFrame", play1)
Runtime:addEventListener("enterFrame", play2)
Runtime:addEventListener("enterFrame", play3)
Runtime:addEventListener("enterFrame", play4)
Runtime:addEventListener("enterFrame", play5)
Runtime:addEventListener("enterFrame", play6)
Runtime:addEventListener("enterFrame", play7)
Runtime:addEventListener("enterFrame", play8)
Runtime:addEventListener("enterFrame", play9)

end

play:addEventListener("tap", play)


---------------------------------------

如果有人能帮忙,那将是一个巨大的帮助,谢谢。

【问题讨论】:

    标签: function coronasdk


    【解决方案1】:

    好的!首先,我认为您不了解“enterFrame”侦听器的作用。 添加代码时

    Runtime:addEventListener("enterFrame", play1)
    

    对于游戏的每一帧(大约每秒执行 30 帧),函数 play1 被执行。在上面的代码中,您添加了一个“enterFrame”侦听器 FOR EACH FUNCTIONON EVERY TAP EVENT 。因此每个函数将在每一帧中多次调用。除非我错了,否则这可能不是您想要的。

    因为你已经调用了函数

    timer.performWithDelay( 5000, play<n>)
    

    您应该删除 Enterframe 侦听器。

    您究竟想在这里实现什么?如果你能更具体一些,帮助你会更容易

    【讨论】:

    • 感谢您的回复。当我删除 EventListeners 时,该函数根本没有响应。我正在尝试创建一种类似于 Scratch 编码的编码模拟游戏,并且我已经有了可以拖到盒子中的“代码”块。根据用户拥有块的顺序,我希望机器人在按下“播放”按钮时采取适当的行动。只有九个可能的位置可以拖动块和五个不同的功能,因为我想让它保持相当简单。
    猜你喜欢
    • 2013-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多