最近コロナ 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()