2016-10-24 7 views
0

こんにちは、私はこの簡単な「卵をキャッチ」ゲームをGodotエンジンからコロナに変換しています。私はプログラミングに非常に新しいですし、このプロジェクトを学習の練習として使用しています。コロナSDK数字の数をゼロと比較しようとしました

私はそれに伴う障害に遭遇しました。

**

ERROR:ランタイムエラー C:コロナプロジェクト\ \ Users \ユーザーkdoug \ドキュメント\ cathchtheeggの\のmain.lua:19:nilにして数を比較しようとすると、私は次のエラーMSGを取得しておきます スタックトレース: C:\ Users \ユーザーkdoug \ドキュメント\コロナプロジェクト\ cathchtheeggの\のmain.lua:19:機能 中:?関数内

**

私がやろうとしています何があります参照してください卵wフィジックスオブジェクトとの衝突を使用する必要がなく、ある点を超えると削除されます。

ご協力いただければ幸いです!あなたはenterFrameイベントリスナーを削除するとき、私は知らない

local physics = require "physics" 
physics.start() 
local h = display.actualContentHeight 
local w = display.actualContentWidth 
local cx = display.contentCenterX 
local cy = display.contentCenterY 
local dnir = display.newImageRect 
local dnr = display.newRect 
local mr = math.random 
--local egg 
local bask 
local idx = 0 
local eggs = {} 


---------BACKGROUND--------------- 
local bg = dnir("bg.png", w,h) 
bg.x = cx 
bg.y = cy 

----------DISPLAY BASKET------------ 
bask = dnir("basket.png", 100,50) 
bask.x = cx 
bask.y = cy 
physics.addBody(bask,"kinematic") 
bask.myName = "bask" 


----- BASKET MOVE W/ MUSE FUNCTION ----- 
local function baskMove (e) 

    bask.x = e.x 
    bask.y = e.y 
end 

Runtime:addEventListener("mouse", baskMove) 


----------------GROUND--------------- 
local grd = dnr(cx,h-470,w+50,10) 
grd:setFillColor(.1, .8, .15,0) 
grd.myName = "ground" 
physics.addBody(grd, "static") 
grd.collision = collision 
grd:addEventListener("collision", grd) 

----------****DELETE EGG FUNCTION****------------ 
--function loop() 
-- if egg and egg.y > 100 then 
-- print("Delete") 
-- display.remove(egg) 
-- end 
--end 
-- 
--Runtime:addEventListener("enterFrame", loop) 


-----------COLLISIONS FUNCTIION------------- 
local function collision (s, e) 
    if e.phase == "began" then 

    if e.target.myName == "bask" 
     and e.other.myName == "egg" then 
     display.remove(e.other) 
     table.remove(eggs, idx) 
    end 

    if e.target.myName == "egg" 
     and e.other.myName == "bask" then 
    display.remove(e.target) 
    table.remove(eggs, idx) 
    end 

    if e.target.myName == "ground" 
     and e.other.myName == "egg" then 
     display.remove(e.other) 
     table.remove(eggs, idx) 
    end 

    if e.target.myName == "egg" 
     and e.other.myName == "ground" then 
    display.remove(e.target) 
    table.remove(eggs, idx) 
    end 

    end 
end 
-- 

--------------EGG--------------------- 
function theEgg() 
egg = dnir("egg.png", 50,50) 
physics.addBody(egg,"dynamic") 
egg.myName = "egg" 
idx = idx + 1 
egg.x = mr(w) 
egg.y = - 100 


transition.to (egg, {y = h + 50, time= mr(1000,8000)}) 

eggs[idx] = egg 
eggs[idx].idx = idx 
print(eggs[idx]) 


--------EGG COLLISIION CB------------- 
egg.collision = collision 
egg:addEventListener("collision", egg) 

end 
-- 

-----------Spawn EGG----------- 
function spawner() 
    theEgg() 
    print(#eggs)-- PRINT AMT IN TABLE 
end 
timer.performWithDelay(2000, spawner, 0) 
+0

「egg」変数について混乱がたくさんあります。関数 'loop()'はグローバルな 'egg'変数にアクセスし、関数' collision'はローカル変数にアクセスし、関数 'spawn()'は新しいeggをリスト 'eggs'に追加しますが、同時にローカル' egg'変数を変更します。それらの関係を整理することから始めます。 – Vlad

+0

Vladに感謝します。あなたが提案したように私は関係をきれいにしようとしました –

答えて

0

:ここ 感謝

は、コード(少しdiscombobulated)です。大事です。あなたが卵オブジェクトを削除した後、ループが再び呼び出されることがあります。したがって、egg.yが定義されていない場合(= nil)、比較(if statment)は実行できません。

私のソリューション:

function loop() 
    if egg and egg.y > 100 then 
    print("Delete") 
    display.remove(egg) 
    end 
end 

情報

all logical operators consider false and nil as false and anything else as true

またはそれはあなたのコード内のvaraible卵の私の目的での使用のための明確ではない(ローカル変数のインデックスの代わりにグローバル変数卵を使用)https://www.lua.org/pil/3.3.htmlからそうそれは間違っているかもしれません。

local index 

... 

function loop() 
    if eggs[index] and eggs[index].y > 100 then 
    print("Delete") 
    local egg = table.remove(eggs, index) 
    display.remove(egg) 
    egg = nil 
    end 
end 
+0

あなたの助けてくれてありがとうございました。あなたの提案は大変意味があり、コードに入れても同じエラーメッセージが表示されます。他の何かがここで気が散ると思う。 –

+0

これをthisEgg関数に追加すると役立ちました。関数ループ() 卵とegg.y> 100次に プリント( "削除") display.remove(卵) table.remove(卵、IDX) 端端 timer.performWithDelay(33、ループ場合、 0)それでも、時々同じエラーが発生します。 @Vladは権利を持っています。 –

+0

あなたはそれをきれいにする必要があります。 theEgg関数で変数eggをローカルとして設定し、他のローカル変数に保存して、その変数への卵表のインデックスを作成できますか? – ldurniat

関連する問題