• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 529
  • Last Modified:

Remove Images Loaded from Library on Radio Button Selection

I've tried implementing this code with my own as3 script but I am still having issues getting the loaded images to be removed. I am calling different images from the library with radio buttons, and I'd like to have it so that clicking a radio button unloads whatever is currently in the holder and then loads the image associated with that radio button. I already have a function (removeImage) associated with the buttons that change the frame to remove the holder -- which works, but only if each radio button is only clicked once. If a radio button is clicked twice the image is loaded twice but only one instance is removed. Please help!
var holder:MovieClip = new MovieClip();
var childName:String = new String();

week1.addEventListener(MouseEvent.CLICK, fl_ClickToLoadImageFromLibrary_1win);
function fl_ClickToLoadImageFromLibrary_1win(event:MouseEvent):void
{
	var libImage:weekonewindow = new weekonewindow(0,0);
	var holder:Bitmap = new Bitmap(libImage);
	addChild(holder);
	holder.x = 173;
	holder.y = 117;
	
	holder.name = "holder1";
	childName = holder.name;
}

week4.addEventListener(MouseEvent.CLICK, fl_ClickToLoadImageFromLibrary_2win);

function fl_ClickToLoadImageFromLibrary_2win(event:MouseEvent):void
{
	var libImage:weekfourwindow = new weekfourwindow(0,0);

	var holder:Bitmap = new Bitmap(libImage);
	addChild(holder);
	holder.x = 173;
	holder.y = 117;
	
	holder.name = "holder2";
	childName = holder.name;
}


week12.addEventListener(MouseEvent.CLICK, fl_ClickToLoadImageFromLibrary_3win);

function fl_ClickToLoadImageFromLibrary_3win(event:MouseEvent):void
{
	var libImage:weektwelvewindow = new weektwelvewindow(0,0);

	var holder:Bitmap = new Bitmap(libImage);
	addChild(holder);
	holder.x = 173;
	holder.y = 117;
	
	holder.name = "holder3";
	childName = holder.name;
}


week52.addEventListener(MouseEvent.CLICK, fl_ClickToLoadImageFromLibrary_4win);

function fl_ClickToLoadImageFromLibrary_4win(event:MouseEvent):void
{
	var libImage:weekfiftytwowindow = new weekfiftytwowindow(0,0);

	var holder:Bitmap = new Bitmap(libImage);
	addChild(holder);
	holder.x = 173;
	holder.y = 117;
	
	holder.name = "holder4";
	childName = holder.name;
}


//Removing the Image by clicking a button to change the frame

back_arrow.addEventListener(MouseEvent.CLICK, removeImage);
function removeImage(event:MouseEvent):void
{

		var img1:DisplayObject = getChildByName("holder1");
		if (img1) removeChild(img1);

		var img2:DisplayObject = getChildByName("holder2");
		if (img2) removeChild(img2);

		var img3:DisplayObject = getChildByName("holder3");
		if (img3) removeChild(img3);
		
		var img4:DisplayObject = getChildByName("holder4");
		if (img4) removeChild(img4);
} 

home.addEventListener(MouseEvent.CLICK, removeImage1);
function removeImage1(event:MouseEvent):void
{

		var img1:DisplayObject = getChildByName("holder1");
		if (img1) removeChild(img1);

		var img2:DisplayObject = getChildByName("holder2");
		if (img2) removeChild(img2);

		var img3:DisplayObject = getChildByName("holder3");
		if (img3) removeChild(img3);
		
		var img4:DisplayObject = getChildByName("holder4");
		if (img4) removeChild(img4);
} 


back_arrow.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_41);

function fl_ClickToGoToAndStopAtFrame_41(event:MouseEvent):void
{
	gotoAndStop(2);
}

home.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_42);

function fl_ClickToGoToAndStopAtFrame_42(event:MouseEvent):void
{
	gotoAndStop(1);
}

Open in new window

0
possiBri
Asked:
possiBri
1 Solution
 
dgofmanCommented:
Please use this code it's clean and correct

var holder:Sprite = new Sprite();
addChild(holder);

week1.addEventListener(MouseEvent.CLICK, onClickEventHandler);
week4.addEventListener(MouseEvent.CLICK, onClickEventHandler);
week12.addEventListener(MouseEvent.CLICK, onClickEventHandler);
week52.addEventListener(MouseEvent.CLICK, onClickEventHandler);
home.addEventListener(MouseEvent.CLICK, onClickEventHandler);

function onClickEventHandler(event:MouseEvent):void
{
      if(holder.numChildren >0)
            holder.removeChildAt(0);

      var bitmap:Bitmap;
      switch(event.target){
            case week1:
                  bitmap = new Bitmap(new weekonewindow(0,0));
                  break;
            case week4:
                  bitmap = new Bitmap(new weekfourwindow(0,0));
                  break;
            case week12:
                  bitmap = new Bitmap(new weektwelvewindow(0,0));
                  break;
            case week52:
                  bitmap = new Bitmap(new weekfiftytwowindow(0,0));
                  break;
            default:
                  return;
      }
      holder.addChild(bitmap);
      bitmap.x = 173;
      bitmap.y = 117;
}
0
 
possiBriAuthor Commented:
Thank you so much! Works like a charm =]]
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now