Load external image for actor (3.0)

stepstream

  • Posts: 40
Anyone had success doing this? I'm looking to load an external image from a URL.

Jon

  • *
  • Posts: 17524
Currently not built in. It's not hard to load an image in (1 line of code, I think), but there are no mechanisms today for overriding what an actor's animation/image is.

Roughly, what you'd need to do is load the image (which gets you a BitmapData) and then in a drawing event, call g.drawImage(img, xPos, yPos). Not literally that code, but that's the approach you'd need to take.

You'd also want to call "hide sprite" on the actor to stop its regular drawing.

stepstream

  • Posts: 40
Thanks! I finally got the External Sprite Loader (Stencyl 2.X) to bend to my will, so I'll stick with that for now and come back to this once I port over to Stencyl 3.


stepstream

  • Posts: 40
OK I've finally gotten to look at this, and I think I'm close but I'm having a dumb moment about variable scope in Haxe. Here's what I've got:

Code: [Select]
class Design_2_2_ extends ActorScript
{         
//Expose your attributes like this
//Need further help? See: http://www.stencyl.com/help/view/code-mode/
//@:attribute("id='1' name='Display Name' desc='An Attribute'")
//public var attributeName:String;
 
override public function init()
{
var picLoader = new Loader();
var spritePic = picLoader.load(new URLRequest("https://www.stephealth.us/game/avatarsample.png"));

}

public inline function update(elapsedTime:Float)
{
}

public inline function draw(g:G)
{
g.drawImage(spritePic, 0, 0);
}

But of course the draw function doesn't know what spritePic is, since it's a local variable to the init function. Any help? Apologies for my ignorance!

captaincomic

  • *
  • Posts: 6109
Changing the variable scope is easy, just pull out the variable declaration out of the function:
Code: [Select]
//public var attributeName:String;
var spritePic:Bitmap;
(or should it be BitmapData instead of Bitmap? I'm not sure at the moment.)

But then the Loader.load function is asynchronous and doesn't return anything, so you can't do
Code: [Select]
spritePic = picLoader.load(...)
Since NME works just like Flash, you can just look up some tutorial for loading images in AS3 and copy the code from there.
http://blog.728media.com/2009/03/11/how-to-load-external-images-in-actionscript-30/

applaud

  • Posts: 370
@stepstream - I'm looking to do the same thing. Did you get this working in 3.0?

captaincomic

  • *
  • Posts: 6109
I think this is now in with the Image API.