一段时间后,申请开始变得疯狂

时间:2013-09-04 23:00:00

标签: iphone sdk lua corona

我最近开始使用电晕SDK,我正在尝试为iPhone构建一个应用程序。现在它背后的主要思想是,有鸟飞,你必须射击它们。这些鸟在屏幕上获得一个随机点作为目标,然后向它导航。该应用程序运行良好约30秒 - 1分钟,但然后突然它开始加速非常快,我不知道为什么。

并且对此表示深深的谢意。

display.setDefault("background", 246, 255, 100)
_W = display.contentWidth;
_H = display.contentHeight;
target = {}
birdPosition = {}
print(_W.." ".._H) 
--getting a random location on the screen
local x = math.random(_W)
local y = math.random(_H)

--this checks whether the image will be placed partially off the screen
if x > _W - 42 then
    x = _W - 42
end
if y > _H - 42 then
    y = _H - 42
end

birdPosition[1] = x
birdPosition[2] = y
local equation = 0
--will be used to see whether the movement will be more vertically than horizontally
local moveVertically = false
local bird = display.newImage("images/bird.png", x, y)
--when the bird is touched, it is removed
function bird:touch()
    bird:removeSelf()
end

bird:addEventListener("touch", bird)
--get a new random position
function getNewPosition()
    --loop = 50
    --getting a random next spot to move to + a check
    x = math.random(_W)
    y = math.random(_H)
    if x > _W - 42 then
        x = _W - 42
    end
    if y > _H - 42 then
        y = _H - 42
    end
    --placing the co-ordinates
    target[1] = x
    target[2] = y
    local smallest
    birdPosition[1] = bird.x
    birdPosition[2] = bird.y
    local diffY
    --this check is done so we get a positive equation
    if x > bird.x then
        diffX = x - bird.x
    else
        diffX = bird.x - x
    end
    if y > bird.y then
        diffY = y - bird.y
    else
        diffY = bird.y - y
    end
    --this check is done so that the equation will always be bigger as 1. This also checks
    --whether it will move more vertically than horizontally by putting the boolean true or false
    if diffX >= diffY then
        equation = diffX/diffY
        smallest = diffY
        moveVertically = false
    else
        equation = diffY/diffX
        smallest = diffX
        moveVertically = true
    end

    --print("birdPosition X: "..birdPosition[1].. " birdPosition Y: "..birdPosition[2])
    --print("Target X: " .. target[1].." Target Y: "..target[2])


    --[[
    if for instance diffX = 100 and diffY = 50:
    smallest will be 50, because the move will be vertically and will only have to be executed 50 times
to give it the effect that it flies quick
]]--
    tmr = timer.performWithDelay(10, moveBird, smallest)
end

function moveBird()
    if moveVertically == true then
        if target[1] >= birdPosition[1] then
            bird.x = bird.x + 1
        else 
            bird.x = bird.x - 1
        end
        if target[2] >= birdPosition[2] then
            bird.y = bird.y + equation
        else
            bird.y = bird.y - equation
        end
    else
        if target[1] >= birdPosition[1] then
            bird.x = bird.x + equation
        else 
            bird.x = bird.x - equation
        end
        if target[2] >= birdPosition[2] then
            bird.y = bird.y + 1
        else
            bird.y = bird.y - 1
        end
    end
    --print("Bird X: "..bird.x .. " Bird Y: " .. bird.y)

    --this checks every possibility to get a new position
    if bird.x == target[1] or bird.y == target[2] or bird.y < 0 or bird.x > 640 or bird.x < 0 or bird.y > 960  then
        getNewPosition()
    end
end    

getNewPosition()

1 个答案:

答案 0 :(得分:0)

timer.performWithDelay(10, moveBird, smallest)中的一个错误。

定时器的调用方式不正确:第三个参数应该是迭代次数(1),而不是smallest

请参阅文档here