-
Image.height to Main.as from Imageloader.as
Hi,
Could you help me please? I have a very simple AS3 question which I can't solve by myself:
How can I read image.height and image.width to Main.as from Imageloader.as?
I am doing a photo gallery and I want to put a text field to the bottom of every image which heights are not the same. Thus the text field has to relocate every time the image is changed. I can relocate the textfield when navigating but in the beginning when the gallery comes to the stage the textfield is in above of the first image because image.height is '1' for some reason I can't figure out. The images are in a imagecontainer in the Main.as and I am calling the first image's height like this:
Flashconnect.trace(kuvaContainer.getChildAt(0).hei ght); //gives '1'
My IMAGELOADER.AS:
public function loadImage():void {
var address:URLRequest = new URLRequest(filename);
image= new Loader();
image.load(address);
addChild(image);
image.contentLoaderInfo.addEventListener(Event.COM PLETE, ready);
}
//Here image.width and image.height are work fine
public function ready(e:Event):void {
var proportion:Number = image.width / image.height;
image.height = stage.stageHeight * 0.65;
image.width = proportion * image.height
}
-
Since the image isn't loaded until the loader finishes, there's no way for Flash to know what the dimensions are. You'll need to use an event to let the main.as know when it's safe to read the height:
PHP Code:
// IMAGELOADER.AS
public function ready(e:Event):void {
image.height = stage.stageHeight * 0.65;
image.scaleX = image.scaleY;
image.dispatchEvent(new Event('imageLoaded'));
}
PHP Code:
// MAIN.AS
kuvaContainer.addEventListener('imageLoaded', function(e:Event):void{
trace(e.target.height);
});
-
Ok, but I was not able to get it work. It does not trace anything. So, could you still help me? I did it like this:
MAIN.AS:
public function MakeImagegallery():void
{
addChild(kuvaContainer);
//load Photos one by one
for (var i:int = 1 ; i < 6 ; i++) {
var yksikuva:Loader_Kuvagalleria = new Loader_Kuvagalleria();
yksikuva.tiedostonimi = "kuva0" + i + ".jpg";
yksikuva.lataaKuva();
kuvaContainer.addChild(yksikuva); }
kuvaContainer.addEventListener('imageLoaded', function(tapahtuma:Event):void{
FlashConnect.trace(tapahtuma.target.height); });
IMAGELOADER.AS:
public function ready(tapahtuma:Event):void {
bitmap = Bitmap(kuva.content);
bitmap.smoothing = true;
bitmap.height = stage.stageHeight * 0.65;
bitmap.scaleX = bitmap.scaleY;
bitmap.dispatchEvent(new Event('imageLoaded'));
}
-
I managed to solve this by myself. Or actually of course with the help of neznein9. Your code was just missing ,true from the end of the
image.dispatchEvent(new Event('imageLoaded', true));
But anyway many thanks!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|