سؤال

I'm relatively new to flash and as3. Basically I'm trying to load a movieclip to the stage and then click a button within that movieclip to remove the child again from the stage. I have the following movieclip:

var myResultBox:ResultBox = new ResultBox();
addChild(myResultBox);

I have a button placed within the movieclip called closeButton1. I am trying to click the close button which in turn removes the movieclip.

the code within the MovieClip is - //Event Listener for Close button within my results box

closeButton1.addEventListener(MouseEvent.CLICK, closeBMI);

function closeBMI(evt:MouseEvent):void
{
    removeChild(myResultBox);
}

Following error

code: 1120: Access of undefined property closeButton1.

Any help would be most appreciated

هل كانت مفيدة؟

المحلول

Below is a simple program which i think has the functionality of what you are asking for. As far as I understand, you have button A on the stage. When you click button A, movie clip B is added to the stage. Movie clip B has button B in it. When you click button B, Movie clip B and button B are removed.

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.MouseEvent;


// This creates a movie clip that contains a button.
// This button will remove the movie clip that contains
// it when it is clicked.
var MovieClipB = new MovieClip();
MovieClipB.graphics.lineStyle(1,0);
MovieClipB.graphics.beginFill(0x0000FF,1);
MovieClipB.graphics.drawRect(0,0,50,50);

var ButtonB:MovieClip = new MovieClip();
ButtonB.buttonMode = true;

ButtonB.graphics.lineStyle(1,0);
ButtonB.graphics.beginFill(0xFFFFFF,1)
ButtonB.graphics.drawCircle(0,0,10);
ButtonB.x = ButtonB.y = 25;

MovieClipB.addChild(ButtonB);

ButtonB.addEventListener(MouseEvent.CLICK, removeButtonClickHandler, false, 0, true);
function removeButtonClickHandler(event:MouseEvent):void
{
    var button = MovieClip(event.currentTarget);    
    var container = button.parent;
    container.parent.removeChild(container);
}

// This creates a button that starts on the stage.
// When clicked, it adds the movie clip defined above to the stage

var ButtonA:MovieClip = new MovieClip();
ButtonA.buttonMode = true;

ButtonA.graphics.lineStyle(1,0);
ButtonA.graphics.beginFill(0xFF0000,1)
ButtonA.graphics.drawRect(0,0,50,50);

addChild(ButtonA);
ButtonA.x = ButtonA.y = 20;

ButtonA.addEventListener(MouseEvent.CLICK, addButtonClickHandler, false, 0, true);
function addButtonClickHandler(event:MouseEvent) : void
{
    addChild(MovieClipB);
    MovieClipB.x = 200;
    MovieClipB.y = 20;
}

نصائح أخرى

Within the button? But you can't reference button in such way. You should put your code within movieclip that holds button, where you add result addChild(myResultBox); So your event handler will be able reference myResultBox

For code within a button:

this.addEventListener(MouseEvent.CLICK, closeBMI);

function closeBMI(evt:MouseEvent):void
{
    //removeChild(myResultBox);
    //Sadly, you don't have reference on myResultBox within a button...
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top