Importing SWF Assets and casting

Hi! I have a MovieClip I am importing from a SWF asset in NME 3.2. I can cast it as nme.display.Sprite so I can use getChildByName to attach events to objects within it. The problem is that the object is returned as a swf.format.MovieClip object, which…

  • Forums
  • »
  • Bugs
  • »
  • Importing SWF Assets and casting
Viewing 1 to 9 (9 Total)
Importing SWF Assets and casting

neurofuzzy

neurofuzzy
Total Posts: 151
Joined: January 21, 2012

Hi!

I have a MovieClip I am importing from a SWF asset in NME 3.2. I can cast it as nme.display.Sprite so I can use getChildByName to attach events to objects within it. The problem is that the object is returned as a swf.format.MovieClip object, which I think extends from DisplayObject. I can't cast it to MovieClip in order to call gotoAndStop on it. What I've had to do is this:


var dobj: DisplayObject = mc.getChildByName("btn");
var mc: Dynamic = dobj;

mc.gotoAndStop(1);


This works, but I don't get cod completion, compile-time checking etc. If I tried to make mc a MovieClip and cast dobj, I get:

[Fault] exception, information=TypeError: Error #1034: Type Coercion failed: cannot convert format.swf::MovieClip@98f8161 to flash.display.MovieClip.

I'm just wondering if there's a better

Tags:
Posted on February 08, 2012 at 1:03 AM

singmajesty

singmajesty
Total Posts: 2146
Joined: August 25, 2011

Re: Importing SWF Assets and casting

Unfortunately, there are two ways to get a "real" MovieClip in Flash. The first is to instantiate one, but Flash provides no way to generate your own frames without using Loader.loadBytes. I decided it was better to keep the same method for both C++ and Flash (especially because it makes debugging and improving the library easier) so this out as an option.

The other approach is to extend MovieClip, but then the signatures need to match when you override a method like "gotoAndStop" ... the Dynamic type in Haxe is the same as the asterisk * type in Actionscript 3, and unfortunately the gotoAndPlay/gotoAndStop methods require an Object as one of the parameters. There is actually no way to create this signature in Haxe, so for now, extending MovieClip is out, too.

If you are able, I would put <haxelib name="swf" /> in your NMML, add "swf" to your Project > Properties > Compiler Options > Libraries in FlashDevelop, and type it as "format.swf.MovieClip" for now.

Posted on February 08, 2012 at 11:02 AM

neurofuzzy

neurofuzzy
Total Posts: 151
Joined: January 21, 2012

Re: Importing SWF Assets and casting

Okay, this works well enough for me. The ability to use SWF assets combined with the pixel-perfect rendering is a real game-changer for me. It will make asset development a breeze.

Posted on February 08, 2012 at 11:12 AM

singmajesty

singmajesty
Total Posts: 2146
Joined: August 25, 2011

Re: Importing SWF Assets and casting

As you move forward, I would love to hear what seems to be ideal from a performance and workflow perspective.

Bitmaps render faster than using SWF assets, but we have possible options available of either using SWF assets directly (if it runs well enough!), committing to bitmaps at runtime using bitmapData.draw (or perhaps an automated "convertToBitmap" method), or performing conversions or optimization at compile time, since the NME command-line tools have access to the same SWF parser as we're using now at runtime.

Also, I hope to resolve the "invisible" issue with mobile. It's frustrating because it worked! Just a couple weeks ago! And when I trace things out, the objects all appear to be there and working fine, so I'm hoping to nail that down so I can use this on mobile again. Or maybe it is just something on my end, and it actually works for other people? Someone else on the forums said it was working fine on his phone

Posted on February 08, 2012 at 11:22 AM

Philippe

Philippe
Total Posts: 261
Joined: September 08, 2011

Re: Importing SWF Assets and casting

Joshua, can't we find a trick to use the same class/typedef both in cpp and Flash? It doesn't matter which one if it can be consistent.

Posted on February 08, 2012 at 11:36 AM

singmajesty

singmajesty
Total Posts: 2146
Joined: August 25, 2011

Re: Importing SWF Assets and casting

Hey Philippe,

Yeah, it was difficult when the returned object had a different type, whether you compiled to Flash or C++. I changed it a while ago for consistency. The return objects is "format.swf.MovieClip", and it extends Sprite.

In the future it would be nice to extend MovieClip, but it just isn't possible right now. The same goes for some of the other classes, like FrameLabel. There's no way you can actually create a FrameLabel instance (with the values you want) in Flash, so I've had to rely on using "format.swf.FrameLabel" instead.

Posted on February 08, 2012 at 11:50 AM

neurofuzzy

neurofuzzy
Total Posts: 151
Joined: January 21, 2012

Re: Importing SWF Assets and casting

The 3.2 beta works fine for me on mobile. I pushed a test app that rendered a SWF MovieClip to the iOS Simulator, an iPod Touch, and a B&N Nook and all worked beautifully. I did notice sometimes stuff would not appear, but only after I changed the SWF file. If I deleted the bin/iphone folder and rebuilt, it worked again.

I'd be curious to know about how SWF content is pushed to OpenGL, and what kinds of things would cause the texture to re-render. Are the rules the same as the Flash Player cacheAsBitmap rules? For instance, am I limited to on-the-pixel translation, but then rotation and/or scaling will cause it to re-render?

It would be great if there were a "convertToBitmap" method, that would treat the object as a bitmap from that point forward.

Or, if we could set a flag to alter the criteria upon which the graphic data were re-rendered. Perhaps something like

mc.renderRules = RERENDER_ON_ROTATE | RERENDER_ON_SCALE;

I have certain SWF assets that are character armatures - a body with nested clips for arms, legs and head. If I could "freeze" the graphic portion of the armature while still allowing for rotation of the child clips, that would be a huge win.

Not knowing anything about the internals of this, I don't even know if this is possible.

Posted on February 08, 2012 at 11:52 AM

neurofuzzy

neurofuzzy
Total Posts: 151
Joined: January 21, 2012

Re: Importing SWF Assets and casting

I've done some testing, and I've found these results as far as SWF performance.

1. Translating an object by a float value that is not a whole number will result in re-rendering.
2. Scaling and rotation will cause re-rendering.
3. Any kind of tween will cause re-rendering.

The only type of motion that will retain the cached texture is translation on the whole pixel.

I think this falls in line with the way Flash handles cacheAsBitmap. The difference here being that, at least on iOS and Android targets, cacheAsBitmap is true by default, since thats the way the hardware works. Setting it to true would have no effect.

So, if you are using motion in your clip, it's best to do it in code. If you are using a tweening library, use proxy points to do tweening, then align your clips to those values while snapping to the whole pixel.

+1 for more control over this (at the cost of visual artifacting) in a future release.

Posted on February 08, 2012 at 2:09 PM

zenter

zenter
Total Posts: 1
Joined: March 14, 2013

Re: Importing SWF Assets and casting

Is it possible to target objects inside a swf asset? For example updating some text in a textfield with MovieClip.textField.text="String"?
I've tried but without success.

Posted on March 14, 2013 at 3:47 AM