2010-12-14 5 views
1

안녕하십니까. 고마워요. 나는 너무 많은 시간을 보냈다.actionscript를 사용하여 로더 언로드 3

아래 코드는 네 이미지의 슬라이드 쇼와 이미지의 미리보기 이미지를로드합니다. 그것은 잘 작동합니다.

"invis_button"이라는 버튼을 추가했습니다. 눌렀을 때 각 로더에 대해 removeChild 명령을 사용하여 슬라이드 쇼를 구성하는 3 개의 로더를 제거해야합니다.

하지만 문제는 슬라이드 쇼에 3 명의 로더가 관련되어 있다는 것입니다. removeChild 명령은 다른 하나 ("container3"및 "thumbLoader3")가 아닌 "loader3"이라는 이름의 로더 중 하나를 성공적으로 제거합니다. "정의되지 않은 속성 thumbLoader3 액세스"또는 "컨테이너 3"액세스 오류를 반환합니다.

누군가이 이유를 말해 줄 수 있습니까? 또는 더 나은 방법으로, 해당 버튼 (invis_button)을 전체 슬라이드 쇼를 언로드하는 방법. 이것은 당신이 관찰하는지 뒤에 전체 이유는 ...하지만 우선, "thumbloader3"와 "container3는"당신이 완료되면 의미 loadThumbs3() 메소드에 로컬로 범위 경우

var images3:Array = ["ad_bona1.jpg", "ad_bona2.jpg", "ad_darkhawk1.jpg", "ad_darkhawk2.jpg"]; 

var thumbX3:Number = -375; 
var thumbY3:Number = 220; 

var loader3:Loader = new Loader(); 
loader3.load(new URLRequest("assets/ad_bona1.jpg")); 
addChild(loader3); 
loader3.alpha = 0; 

loadThumbs3(); 

function loadThumbs3():void 
{ 
var thumbLoader3:Loader; 
var container3:Sprite = new Sprite(); 
addChild(container3); 
container3.buttonMode = true; 
for(var i3:uint = 0; i3 < images3.length; i3++) 
{ 
thumbLoader3 = new Loader(); 
thumbLoader3.load(new URLRequest("assets/thumbs/" + images3[i3])); 
thumbLoader3.x = thumbX3; 
thumbLoader3.y = thumbY3; 
thumbX3 += 85; 
container3.addChild(thumbLoader3); 
thumbLoader3.addEventListener(MouseEvent.CLICK, thumbClicked3); 
} 
} 
function thumbClicked3(event:MouseEvent):void 
{ 
var path3:String = event.currentTarget.contentLoaderInfo.url; 
path3 = path3.substr(path3.lastIndexOf("/") + 1); 
loader3.load(new URLRequest("assets/" + path3)); 
} 


///PROBLEM BELOW, button removes only "loader3" and not the other two for some reason 

invis_button.addEventListener(MouseEvent.CLICK, unload_loaders); 

function unload_loaders(event:MouseEvent):void{ 
    removeChild(loader3); 
    removeChild(thumbLoader3); 
    removeChild(container3); 
} 

답변

2

확실하지 않음 함수를 실행하면 해당 객체에 대한 Flash의 핸들이 손실됩니다 (완전히 다른 범위에있는 것은 말할 것도 없습니다).이 두 클래스에 대한 클래스 수준 속성을 만들어 봅니다. 완료되면 나중에 스테이지에서 성공적으로 제거 할 수 있어야합니다.

여러분이 여러분의 객체를 적절하게 파괴하기를 바랍니다. 위의 코드를 생략하기를 간략하게하기 위해서입니다.

나는 위의 코드를 편집하여 & 속성을 적절한 범위에 넣었습니다. (thumbLoader3의 여러 사본이 이제 벡터 (특수 배열) 내부에 수집되므로이를 파괴 할 때 적절히 해결할 수 있습니다.)

또한 적절한 파괴 방법을 작성했습니다. ;)

나는 내 컴퓨터에서 해보지 않았지만 스핀을 줄 수 있습니다. & 어떻게되는지보십시오.

var images3:Array = ["ad_bona1.jpg", "ad_bona2.jpg", "ad_darkhawk1.jpg", "ad_darkhawk2.jpg"]; 

var thumbX3:Number = -375; 
var thumbY3:Number = 220; 

// begin new instance properties.. 
// created a new property, allowing you to group (and hold on to) the multiple thumbLoaders 
var thumbLoader3Vector:Vector.<Loader> = new Vector.<Loader>(); 
var container3:Sprite; 
// end new instance properties 

var loader3:Loader = new Loader(); 

loader3.load(new URLRequest("assets/ad_bona1.jpg")); 
addChild(loader3); 
loader3.alpha = 0; 

loadThumbs3(); 

function loadThumbs3():void 
{ 

    // this is where container3 used to be declared 

    container3 = new Sprite(); 
    addChild(container3); 
    container3.buttonMode = true; 
    for(var i3:uint = 0; i3 < images3.length; i3++) 
    { 
     var tPtr:int = thumbLoader3Vector.length; 
     thumbLoader3Vector.push(new Loader()); 
     // this is where thumbLoader3 used to be declared & instantiated 

     thumbLoader3Vector[tPtr].load(new URLRequest("assets/thumbs/" + images3[i3])); 
     thumbLoader3Vector[tPtr].x = thumbX3; 
     thumbLoader3Vector[tPtr].y = thumbY3; 
     thumbX3 += 85; 
     container3.addChild(thumbLoader3Vector[tPtr]); 
     thumbLoader3Vector[tPtr].addEventListener(MouseEvent.CLICK, thumbClicked3); 

    } 
} 
function thumbClicked3(event:MouseEvent):void 
{ 
    var path3:String = event.currentTarget.contentLoaderInfo.url; 
    path3 = path3.substr(path3.lastIndexOf("/") + 1); 
    loader3.load(new URLRequest("assets/" + path3)); 
} 


///PROBLEM BELOW, button removes only "loader3" and not the other two for some reason 

invis_button.addEventListener(MouseEvent.CLICK, unload_loaders); 

function unload_loaders(event:MouseEvent):void{ 

    // since the thumbLoader3 Loaders are children of container3 in the display list, we need to remove them first 
    for(var $i:uint = 0;$i<thumbLoader3Vector.length;$i++) 
    { 
     removeChild(thumbLoader3Vector[$i]); 
     // also make sure you remove the listener, so that the object will be picked up by garbage collection 
     thumbLoader3Vector[$i].removeEventListener(MouseEvent.CLICK, thumbClicked3); 
    } 
    // and then just set the entire vector to null 
    thumbLoader3Vector = null; 

    // remove the loader3 object & set it to null 
    removeChild(loader3); 
    loader3 = null; 

    // remove the container3 object & set it to null 
    removeChild(container3); 
    container3 = null; 
} 
+0

응답을 얼마나 좋아하는지 말할 수 없습니다. 나는 as3에 상당히 익숙하다.이 객체들을위한 클래스 레벨 속성을 생성하는 데 사용할 수있는 코드가 있는가? – Steve

+0

위의 내 게시물을 수정 한 버전의 코드를 포함하도록 수정했습니다. – greatdecay

관련 문제