[Flash] - Load images dynamically

481 views
Skip to first unread message

Sunish Menon

unread,
Oct 30, 2012, 2:24:49 AM10/30/12
to haxe...@googlegroups.com
Hi,

I am trying to load images dynamically to the Flash application. This is what I want to achieve:

1. January to December - is shown.

2. When a user clicks on "April" - Images from the "April" folder should be shown (with Next and Previous Buttons).

3. When a user clicks on "October" - Images from the "October" folder should be shown, etc.

I tried following: http://blog.neurotoxic.org/post/2009/01/13/first but it throws the error "Unexpected attach" on the line flash.Lib.attach("men")
while compiling.

I am familiar with HTML, Jquery, CSS, so if it is possible to load web pages into flash, I can do them in HTML and JS and just call those pages here. If not, please point me to some links that explains how to load images correctly.


Thanks in advance.

Baluta Cristian

unread,
Oct 30, 2012, 4:25:11 AM10/30/12
to haxe...@googlegroups.com
You're trying to load from the lib.


loader = new Loader();
loader.load ( new URLRequest ( URL ), new LoaderContext ( true ) );
loader.contentLoaderInfo.addEventListener (Event.COMPLETE, completeHandler);


while compiling.

Sunish Menon

unread,
Oct 30, 2012, 4:53:29 AM10/30/12
to haxe...@googlegroups.com
Thank you for replying.

After giving the code, (replacing URL with "http://www.google.com"), the file compiles successfully, but the swf is not working. Even my existing functions, click events, etc. are not working, and messed up the whole alignment of objects :-(

I want this application to run from a CD, without Internet or a local web server. I hope URLRequest can accept local relative paths.

Thanks.

Postite

unread,
Oct 30, 2012, 5:12:35 AM10/30/12
to haxe...@googlegroups.com
I think you are messing with a lot of things .
First,flash doesnt allow to load an entire web page (like an iframe).
You have to point your loader to a specific image.
Then when loading images from urlrequest , you cannot use flash.Lib.attach.
But you have to add them to the displaylist using .addChild().
--

Sunish Menon

unread,
Oct 30, 2012, 5:16:43 AM10/30/12
to haxe...@googlegroups.com
Hi David,

This is what I did:

var loader = new Loader();
loader.load ( new URLRequest ( "http://haxe.org/img/haxe2/logo.png" ), new LoaderContext ( true ) );
Lib.current.addChild(loader);

This is also not working.

Baluta Cristian

unread,
Oct 30, 2012, 5:55:13 AM10/30/12
to haxe...@googlegroups.com
Listen for all the events, you might get an error depends where you run the app from. Flash wont load files from a server to another for example. 
Or easier, use your own images in a relative path.

--

Sunish Menon

unread,
Oct 30, 2012, 6:00:52 AM10/30/12
to haxe...@googlegroups.com
Hi Baluta,

Thank you for the help. Relative path was what I really wanted, but I thought your reply was to load an html page as an iframe.

loader.load ( new URLRequest ( "myfolder/myfile.jpg" ), new LoaderContext ( true ) ) works fine.

Thank you once again.

Regards,
Sunish

david quertelet

unread,
Oct 30, 2012, 6:09:03 AM10/30/12
to haxe...@googlegroups.com
for distant image you can add " -D network-sandbox " in your build.hxml file

Sunish Menon

unread,
Oct 30, 2012, 6:12:20 AM10/30/12
to haxe...@googlegroups.com
Thanks.

The image is now scaled up too large. What sets the height of loader? loader.height, loader.style.height, loader.content.height - all fails.

tom rhodes

unread,
Oct 30, 2012, 6:20:41 AM10/30/12
to haxe...@googlegroups.com
have a look at the API.

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Loader.html

basically the loaded content is loader.content. so if you add that in a holder or directly on the display list you can manipulate the width, height, scaleX, scaleY, rotation etc.


--

Baluta Cristian

unread,
Oct 30, 2012, 6:35:43 AM10/30/12
to haxe...@googlegroups.com
For efficiency you'd like to resize the images before loading, in Photoshop or something.
In software they'll get pixelated and needs more work to fix it, you can see the onComplete in the link i gave you.

Sunish Menon

unread,
Oct 30, 2012, 6:37:52 AM10/30/12
to haxe...@googlegroups.com
Yes, I saw. The images are all pre-set. So I will optimize them in GIMP to reduce size.

I was able to resize the loader from onComplete function. Is there a way I can get the height or width of the image? (not the loader)

tom rhodes

unread,
Oct 30, 2012, 6:42:32 AM10/30/12
to haxe...@googlegroups.com
loader.content IS the image. check the API out for loader on the adobe site that i just sent through, there are various examples on how to use it. 

--

Sunish Menon

unread,
Oct 30, 2012, 6:47:50 AM10/30/12
to haxe...@googlegroups.com
I am reading it, thanks.

j...@justinfront.net

unread,
Oct 30, 2012, 8:57:02 AM10/30/12
to haxe...@googlegroups.com
Sunish Menon

Yep if you wait till it's loaded you can tell the size this is a snipit of some of my code in the past, it also deals with making the image a button.

    /**
     *  wraps the loaded movie or image and creates a hitarea 
     *  maps the Mouse down to the class
     *  @param e Event
     *  @return wrapped movie
     */
    private function wrapLoaded( e: Event ): MovieClip
    {
        
        var mc:             MovieClip           = new MovieClip();
        _container                              = new MovieClip();
        e.target.content.smoothing              = true;
        _container.addChild( e.target.content );
        
        var w:              Int                 = cast( _container.width );
        var h:              Int                 = cast( _container.height );
        var rollover:       Bitmap              = new Bitmap( new BitmapData( w, h, true, 0x00000000 ));
        
        if( isButton )
        {
            
            rollover.smoothing                      = true;
            
            _container.addEventListener( MouseEvent.MOUSE_DOWN, pressed );
            _container.buttonMode     = true;
            _container.mouseChildren  = false;    
            
        }
        _container.addChild( rollover );
        mc.addChild( _container );
        //mc._container                           = _container;
        
        //mc.x = -1000;
        //mc.y = -1000;
        
        return mc;
        
    }
( http://haxe.org/forum/thread/4322 )
If you want to check ahead image sizes there are some tricks you can try where you start streaming and then cancel for example see this as3:
http://www.anttikupila.com/flash/classes/as3/com/anttikupila/utils/JPGSizeExtractor.as

If you want to load from an external site you need to look into Cross Domain Policies ( you need the external site to host one a small xml file saying that it allows you to use it ). The rules change all the time and so I always end up rechecking when ever I need to do this. Hopefully this link is current..


Or you can use a PHP/Neko shim to get the image and pass it to flash, I have used them to get facebook profile images but I no longer have the code and quick google did not turn up a shim.

It should be possible to resize images on the fly using imagemagik, but I have never bothered because its better to resize them in photoshop/gimp before use.


Hope that helps.

Best 

Justin

Sunish Menon

unread,
Oct 31, 2012, 2:02:05 AM10/31/12
to haxe...@googlegroups.com
Thanks Justin. Yeah, I was checking image size in the same function, and not in onComplete function of loader, which caused the problem. Now it works fine, thanks.

I hope I can read CSV files the same way. (BTW, is there any built-in function for processing CSV or JSON, or do I have to manually process the contents?

Baluta Cristian

unread,
Oct 31, 2012, 2:14:51 AM10/31/12
to haxe...@googlegroups.com
I ported once a lib for that, you can easily take it because has no dependency https://github.com/ralcr/sdk.ralcr/blob/master/Utils/Csv.hx

For json there's a built in function.
Reply all
Reply to author
Forward
0 new messages