Jump to content
Search Community

How to return ImageLoader from function?

failure13 test
Moderator Tag

Recommended Posts

In my old code images was just a MovieClip that contained loader with image inside, now i wanted to rewrite it for ImageLoader, with container: this, coz this is really handy for numerous reasons, but for some reason i got this error:

Scene 1, Layer 'menu actions', Frame 2, Line 975 1067: Implicit coercion of a value of type com.greensock.loading:ImageLoader to an unrelated type flash.display:MovieClip.

Question is - how to return ImageLoader with container: this property, so it wouldn't conflict?

// create new gallery instance
var gallery1:MovieClip = addGallery(
contentWidth,
contentHeight,
"system/gallery/patr/picture.jpg"
);

// position gallery
gallery1.x = 0;
gallery1.y = 0;
// name and add gallery on stage
gallery1.name = "gallery1";
addChild(gallery1);


function addGallery(gW, gH, pattern:String):MovieClip {
// path to load images
var pattern:String;
// set the size limits of gallery
var gW:int;
var gH:int;
// loader for images
var images:ImageLoader;

// create a loader with necessary loading parameters
images = new ImageLoader(pattern, {
name: "images",
container: this,
bgColor: 0x1111111,
x: 0, y: 0,
width:gW, height:gH,
scaleMode: "proportionalInside",
});
// load image with loader
images.load();

// return active objects for external functioins
return images;
}

Link to comment
Share on other sites

Do you want to refer to the ImageLoader instance or its ContentDisplay? Just change the data type of your variable and the return value of the function to be whichever you choose. Like:

 

var gallery1:ImageLoader = addGallery(...);

function addGallery(...):ImageLoader {
var images:ImageLoader = new ImageLoader(...);
...
return images;
}

Link to comment
Share on other sites

Do you want to refer to the ImageLoader instance or its ContentDisplay?

Not sure if i understand your question...

What i want is just to display my image loader, without throwing error...

http://www.sendspace.com/file/ugx3ad

Check this file out, when i have tryed to do it so

var gallery1:ImageLoader = addGallery(...);

I've got even more errors...

I guess gallery1 should always be MovieClip...

 

Strange thing is that in my original code if i'll make it just as simplified one i've sent you - ImageLoader won't even display on screen, but here in simplifyed version it gets displayed even with this error...

I'm stuck //

 

P.S. And by the way, why resize even differs when you export and watch inside flash, and if you just load the same .swf outside of flash?

I want it always to resize whole thing like it do now in flash.

So that only the container get resized, and image would proportionally fit it..

flash http://s1.ipicture.ru/uploads/20120821/I29QAZTu.png

swf http://s1.ipicture.ru/uploads/20120821/1ttTW9S1.png

Link to comment
Share on other sites

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
import com.greensock.loading.display.*;
import flash.events.Event;

// create new gallery instance
var gallery1:MovieClip = addGallery(
500, 500
);

// name and add gallery on stage
gallery1.name = "gallery1";
addChild(gallery1);

function addGallery(gW, gH):MovieClip {
var images:MovieClip = new MovieClip;
var loaderImages = new ImageLoader("Believe.jpg", {
	name: "loaderImages",
	container: images,
	bgColor: 0x1111111,
	x: 450, y: 300,
	width: gW, height: gH,
	scaleMode: "proportionalInside",
	centerRegistration:true,
	onComplete: loadImageNow
});
// load image with loader
loaderImages.load();

stage.addEventListener(Event.RESIZE, loadImageNow1);

// function that loads image
function loadImageNow(e:LoaderEvent):void {
	loaderImages.content.fitWidth = 800;
	loaderImages.content.fitHeight = 500;
}

// function that loads image
function loadImageNow1(event:Event):void {
	loaderImages.content.fitWidth = stage.stageWidth - 100;
	loaderImages.content.fitHeight = stage.stageHeight - 100;
}
return images
}

 

I guess this way it works, coz it returns movieClip named images to display it on screen, and there are no error...

But question about resize is still opened...

+ i would like to control bgAlpha: 0, parameter, after loader loaded, so it would fade container alpha from 0 to 1 slowly, how would i do this?

++ i also would like to controls loaded image alpha separately, to switch between images gently...Is it possible and how would you write such a thing?

Link to comment
Share on other sites

The following code will run fine without errors. Paste it into the file you uploaded.

import com.greensock.*;
import com.greensock.easing.*;
import com.greensock.events.LoaderEvent;
import com.greensock.loading.*;
import com.greensock.loading.display.*;
import flash.events.Event;
// create new gallery instance


//make sure you declare the proper type
var gallery1:ContentDisplay = addGallery(
500, 500
);
// name and add gallery on stage
gallery1.name = "gallery1";
addChild(gallery1);


//make sure your return type is proper
function addGallery(gW, gH):ContentDisplay {
var loaderImages = new ImageLoader("pic.jpg", {
 name: "loaderImages",
 container: this,
 bgColor: 0x1111111,
 x: 450, y: 300,
 width: gW, height: gH,
 scaleMode: "proportionalInside",
 centerRegistration:true,
 onComplete: loadImageNow
});
// load image with loader
loaderImages.load();

stage.addEventListener(Event.RESIZE, loadImageNow1);

// function that loads image
function loadImageNow(e:LoaderEvent):void {
 loaderImages.content.fitWidth = 800;
 loaderImages.content.fitHeight = 500;
}

// function that loads image
function loadImageNow1(event:Event):void {
 loaderImages.content.fitWidth = stage.stageWidth - 100;
 loaderImages.content.fitHeight = stage.stageHeight - 100;
}
//return the ImageLoader's ContentDisplay object
return loaderImages.content
}

Link to comment
Share on other sites

You are welcome. Understanding ContentDisplay will definitely make your time with LoaderMax much more enjoyable. Furthermore wrapping your head around content and rawContent will also open your eyes to many possibilities.

 

In short, ContentDisplay is just a juiced-up Sprite that the visual loaders use to contain the assets that you load. The ContentDisplay controls how items are scaled and also allows you to place mouse event listeners on your content before it is fully loaded among other thins. Check the docs:

 

http://www.greensock.com/as/docs/tween/com/greensock/loading/display/ContentDisplay.html

 

tip 1 and 6

http://www.greensock.com/loadermax-tips/

 

enjoy

  • Like 1
Link to comment
Share on other sites

GreenSock

P.S. And by the way, why resize even differs when you export and watch inside flash, and if you just load the same .swf outside of flash?

I want it always to resize whole thing like it do now in flash.

So that only the container get resized, and image would proportionally fit it..

flash http://s1.ipicture.r...21/I29QAZTu.png

swf http://s1.ipicture.r...21/1ttTW9S1.png

 

+ i would like to control bgAlpha: 0, parameter, after loader loaded, so it would fade container alpha from 0 to 1 slowly, how would i do this?

I mean i want to control alpha of cotainer only (not content), outside of var loaderImages

This is a great deal, for example in normal state of picture i want bgAlpha to be 0, but when picture is in fullscreen maxed i want it to be 1. to make background on top of everything (previously i have just to draw black rectangle and swap depths), but i guess it's kinda same for container of ImageLoader...

 

carl schooff

Ok, so it's just a type of content, which actually is container, for example if you got container: images, thanimage which is MovieClip type, will become automatically ContentDisplay type, and will react on each inside ImageLoader commands, right?

Link to comment
Share on other sites

[EDIT] please see GreenSock's solution below[/EDIT]

 

Currently I'm pretty sure that you can not target the rectangle shape that is drawn when you specify the bgAlpha and bgColor of the ContentDisplay object. So there will not be a way of tweening the alpha of that shape.

 

---

 

As for the question about why the resize works please stand by. I will need to test the code.

Link to comment
Share on other sites

RESIZE ISSUE

 

I put a textfield on stage to display the stage.stageWidth on resize. In the stand-alone player no values were displayed so that lead me to believe the resize event wasn't firing.

 

To have the resize work the same way in the Flash preview and Stand Alone versions add this to your code:

 

stage.scaleMode = StageScaleMode.NO_SCALE;

  • Like 1
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...