2013-10-23 2 views
0

나는 그것이 나에게 오류를 제공합니다 다음 코드 조각을 실행하려고 할 때 :루아 충돌 전무 값은

Attempt to index field 'other' (a nil value)

을하지만 난 그 이유를 모른다.

코드 : 사전에

function onCollision(event) 
if event.phase == "began" then 
    if event.other.star == "star" then 
     score = score + 1 
    elseif event.other.mine1 == "mine1" then 
     if jet.collided == false then 
     timer.cancel(tmr)  
     jet.collided = true  
     jet.bodyType = "static" 
     explode() 
     end 
    end 
    end 
end 

감사합니다 :)

+5

읽을 수있는 '인덱스 필드 시도'의 메시지에 대해 궁금해하고 있습니다. – lhf

+3

lhf가 말한 것. 즉, onCollision에 전달 된 값이 테이블 (또는 하나처럼 껍질)이지만 '기타'라는 필드가 없다는 것을 의미합니다. 아마도이 필드는 실제로 '기타'라고 이름 지어집니다. 아마도'onCollision'은 그것이 가지고 있다고 생각하는 객체를 전달하지 않았습니다. – RBerteig

답변

5

@lhf 및 @RBerteig이 때문에 star 멤버에 액세스하려고 문제가 event.othernil입니다 말했듯이 색인 전무를 시도 실패 값.

event.other 가정하면 실제로 nil, 다른 조건을 설정할 수 event.other에 의존하는 경우 모두 이후, if event.phase == "began" and event.other then 경우 이전에 전무 검사를 추가하는 것입니다 문제를 해결하는 관용적 인 방법이 될 수 있습니다.

function onCollision(event) 
if event.phase == "began" and event.other then 
    if event.other.star == "star" then 
     score = score + 1 
    elseif event.other.mine1 == "mine1" then 
     if jet.collided == false then 
     timer.cancel(tmr)  
     jet.collided = true  
     jet.bodyType = "static" 
     explode() 
     end 
    end 
    end 
end 

경우 당신은 또한 event.other`은 전무하다`을 의미 등에 대한 lua index metamethod here