Jump to content
Search Community

Multiple buttons for loading/unloading

jtvalles test
Moderator Tag

Recommended Posts

Here's another one.

 

I am designing an application where I load different images into the same Movie Clip. The code works fine, but I don't know how to unload without errors (a child is added the Movie Clip for every click). For that matter, I don't know if this is the best solution.

 

Any advice?

 

Object(root).mc_news_heds.addEventListener(MouseEvent.MOUSE_DOWN, newsMenuHandler);

function newsMenuHandler(event:MouseEvent):void
{

if (event.target.name == "mc_newsItem1"){

var mainImage:ImageLoader = new ImageLoader("http://ts-ses-2/lv/images/" + newsInfo.News[0].img_preview[0], {container:Object(root).mc_newsStage.mc_newsPhoto,  width:465, height:580, scaleMode: 'proportionalOutside', alpha: 0, crop:"true", estimatedBytes:60000, onComplete:imageCompleteHandler});
mainImage.load();
}

if (event.target.name == "mc_newsItem2"){

var mainImage:ImageLoader = new ImageLoader("http://ts-ses-2/lv/images/" + newsInfo.News[1].img_preview[0], {container:Object(root).mc_newsStage.mc_newsPhoto,  width:465, height:580, scaleMode: 'proportionalOutside', alpha: 0, crop:"true", estimatedBytes:60000, onComplete:imageCompleteHandler});
mainImage.load();
}

}

Link to comment
Share on other sites

you're sort of on the right track. once you define your ImageLoader inside your newsMenuFunction, you really get access it outside that function. Although LoaderMax does store a reference to it but you would have to remember the "name" or "url" property. its a longer story.

 

for now you can declare your ImageLoader and its special properties once and then use the button to control which url to load. This way each time you load() a new image, the previous one will automatically be replaced.

 

consider the following code:

 

//define the ImageLoader and its special props
var myImage:ImageLoader = new ImageLoader("", {container:this,alpha:.5, x:0, y:0, estimatedBytes:60000, onProgress:progressHandler});

crab_btn.addEventListener(MouseEvent.CLICK, loadImage);
whale_btn.addEventListener(MouseEvent.CLICK, loadImage);

function loadImage(e:MouseEvent):void{

//assign the proper url based on button clicked
if(e.target.name =="crab_btn"){
myImage.url = "images/crab.png";
}

if(e.target.name == "whale_btn"){
myImage.url = "images/whale.png";
}

myImage.load();
}

Link to comment
Share on other sites

Thanks, Carl. The code works well. Here's how I coded my project:

 

var mainImage:ImageLoader = new ImageLoader("", {container:Object(root).mc_newsStage.mc_newsPhoto,  width:465, height:580, scaleMode: 'proportionalOutside', alpha: 0, crop:"true", estimatedBytes:60000, onComplete:imageCompleteHandler});

//Buttons are nested in the mc:
Object(root).mc_news_heds.addEventListener(MouseEvent.MOUSE_DOWN, newsMenuHandler);

function newsMenuHandler(event:MouseEvent):void
{
if (event.target.name == "mc_newsItem1"){
LoaderMax.getLoader("mainPhoto").url = "image1"
}
if (event.target.name == "mc_newsItem2"){
LoaderMax.getLoader("mainPhoto").url = image2;
}

 

The only snag is that the onComplete doesn't fire after the first click, so my alpha tween is not applied.

Link to comment
Share on other sites

onComplete doesn't fire after the first click

not so sure why that would happen. your code doesn't show the onCompleteHandler or where you are invoking a load().

if you want to show all the code or provide files I will look at them.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...