2012-10-23 3 views
0

간단한 iPhone 게임을 만들고 있습니다 (하지만 클래스를 사용하려면 Flash를 사용해야합니다 ...). 게임에서 객체를 제거하려고 할 때 중대한 문제가 있습니다."오류 # 2025 제공된 DisplayObject는 발신자의 하위 여야합니다." removeChild();를 시도 할 때;

게임은 무한한 "새로 고침"이있는 역동적 인 시간 낭비 버블 랩 게임입니다. 사용자가 모든 거품을 튀기면보다 무작위로 생성됩니다. (배열이 비어있을 때 setup()으로 다시 전화하십시오.)

지금까지는 라이브러리에서 코드별로 끌어와 무작위로 스테이지에 배치 한 각기 다른 5 가지 색상의 버블 그래픽 중 하나를 사용했습니다. 그러나, 나는 오버랩을 좋아하지 않는다. 그래서 나는 각각의 버블에 작은 사각형 히트 공간을 주었다. 그래서 무작위로 배치되고 상당히 겹치게 될 것이다. 어떤 이유로 든 거품을 제거하려고 할 때 나에게 준다.

오류 # 2025 제공된 DisplayObject는 호출자의 자식이어야합니다.

addChildremoveChild에 스테이지를 추가하려고 시도했지만 동일한 오류가 발생합니다.

import flash.events.*; 
import flash.display.*; 

var bubbles:Array = new Array(); 

var b:BlueBubble = new BlueBubble(); 
var b2:GreenBubble = new GreenBubble(); 
var b3:PinkBubble = new PinkBubble(); 
var b4:PurpleBubble = new PurpleBubble(); 
var b5:YellowBubble = new YellowBubble(); 

// values to be tweaked later 
var bNum:uint = 1; 
var maxSize:uint = 100; 
var minSize:uint = 1; 
var range:uint = maxSize - minSize; 
var tooBig:uint = 100; 

function init():void { 
setup(); 
stage.addEventListener(Event.ENTER_FRAME, onEveryFrame); 
} 

function setup():void { 
for (var i:uint=0; i<bNum; i++) { 
    // blue 
    b.width = Math.ceil(Math.random() * range) + minSize; 
    b.height = b.width; 
    b.x = Math.random()*(stage.stageWidth - b.width); 
    b.y = Math.random()*(stage.stageHeight - b.height); 
    bubbles.push(b); 
    stage.addChild(b); 
    // green 
    b2.width = Math.ceil(Math.random() * range) + minSize; 
    b2.height = b2.width; 
    b2.x = Math.random()*(stage.stageWidth - b2.width); 
    b2.y = Math.random()*(stage.stageHeight - b2.height); 
    bubbles.push(b2); 
    stage.addChild(b2); 
    // pink 
    b3.width = Math.ceil(Math.random() * range) + minSize; 
    b3.height = b3.width; 
    b3.x = Math.random()*(stage.stageWidth - b3.width); 
    b3.y = Math.random()*(stage.stageHeight - b3.height); 
    bubbles.push(b3); 
    stage.addChild(b3); 
    // purple 
    b4.width = Math.ceil(Math.random() * range) + minSize; 
    b4.height = b4.width; 
    b4.x = Math.random()*(stage.stageWidth - b4.width); 
    b4.y = Math.random()*(stage.stageHeight - b4.height); 
    bubbles.push(b4); 
    stage.addChild(b4); 
    // yellow 
    b5.width = Math.ceil(Math.random() * range) + minSize; 
    b5.height = b5.width; 
    b5.x = Math.random()*(stage.stageWidth - b5.width); 
    b5.y = Math.random()*(stage.stageHeight - b5.height); 
    bubbles.push(b5); 
    stage.addChild(b5); 

    //add event listeners for bubbles later 

} 
} 
function onEveryFrame(e:Event) { 
for (var i:uint=0; i<bubbles.length; i++) { 

    bubbles[i].width++; 
    bubbles[i].height++; 

    if (bubbles[i].height >= tooBig && bubbles[i].width >= tooBig) { 
     bubbles[i].height = tooBig; 
     bubbles[i].width = tooBig; 
    } 

    // Blue hit testing 
    if (b2.hitGreen.hitTestObject(b.hitBlue)) { 
      removeChild(b); 
    } 
    if (b3.hitPink.hitTestObject(b.hitBlue)) { 
      removeChild(b); 
    } 
    if (b4.hitPurple.hitTestObject(b.hitBlue)) { 
      removeChild(b); 
    } 
    if (b5.hitYellow.hitTestObject(b.hitBlue)) { 
      removeChild(b); 
    } 

    // Other hit testing... 

} 
} 

init(); 

답변

0

이 문제를 해결하는 가장 좋은 방법은 다음과 같습니다

mc.parent.removeChild(mc); 

기본적으로로 removeChild 기능이 곧 제거 할 객체에 어떤 표시 객체에서 호출 할 필요가

+0

음, 일의 종류를 변경 시도에 대한 이전 오류를 교환 "오류 # 1009 :. null 객체 참조의 속성이나 메서드에 액세스 할 수 없습니다" ...하지만 아무것도 이해할 수 없다는 null입니다, 그래서 그것을 왜? – user1769210

2

당신. stageb, b2, b3 등을 추가하지만이 코드를 실행하는 모든 객체에서 제거하십시오.

addChild(b); 

stage.addChild(b); 

을 변경하려고하는 것은 또한,이 객체가 아이를 제거하기 전에 있는지 확인하는 것이 좋습니다.

if (...) 
    removeChild(b); 

if (contains(b) && ...) 
    removeChild(b); 
+0

감사합니다. 그게 효과가 있었어. – user1769210

+0

기꺼이 도와 드리겠습니다. 질문을 닫으려면 대답을 수락하십시오. –

+0

고마워요.이게 내가 가진 동일한 문제를 해결했습니다! – jdfinch3