05 September 2006

MovieClip Casting Fails with attachMovie()? Flash 8

Written by Richard Leggett ( Contact the author of this post )
Published on September 5th, 2006 @ 04:50:58 am, using 321 words, 791 views
Categories: Flash

I seem to be getting casting failing (returning null) when I have a class that extends a class that extends MovieClip. Here's an example...

MyWidget extends SimpleButton
SimpleButton extends View
View extends MovieClip

That's the inheritance chain, SimpleButton and View are some base classes I have written for use in various projects and are not part of the v2 components. Here's what happens when I try to cast an attachMovie():

var aWidget:MyWidget = MyWidget( attachMovie( MyWidget.LINKAGE,
                    "aWidget", getNextHighestDepth() ) );
trace( aWidget ); // null

That should not be happening of course. Now if we change MyWidget to extend View instead of SimpleButton...

var aWidget:MyWidget = MyWidget( attachMovie( MyWidget.LINKAGE,
                    "aWidget", getNextHighestDepth() ) );
trace( aWidget ); // _level0.blah.aWidget

Now there's nothing wrong with SimpleButton per se, it does this in other situations, with different inheritance chains (and using v2 component architecture), but it does seem to be failing when the chain gets to a certain length.

The quick fix is to return an instance of MovieClip and cast that afterwards (cheers Dan):

var tempMC:MovieClip = attachMovie( MyWidget.LINKAGE, "aWidget",
                      getNextHighestDepth() );
var aWidget:MyWidget = MyWidget( tempMC );
trace( aWidget ); // _level0.blah.aWidget

I don't remember this happening in the last few years, before Flash 8 and even whilst Flash 8 has been out, yet it has hit a few of us all at once just recently, it's as if some update has been released that causes this behaviour, and I know I've never had this problem before with the same code methodology. Is anyone else experiencing this, or do you have some extra information on why this happens?

Comments, Pingbacks:

Comment from: Jermaine Anderson
Hi Richard,

I have bumped into this recently - not a clue why it happens but just I would say you not alone on this ;)
PermalinkPermalink 15/09/06 @ 02:36
Comment from: Jermaine Anderson
Sorry about the bad grammar!!
PermalinkPermalink 15/09/06 @ 02:39
Comment from: Richard Leggett [Member] Email
Thank goodness :)
PermalinkPermalink 15/09/06 @ 05:47
Comment from: Richard Leggett [Member] Email
Swapping the above code above or below other attachMovie's fixes the problem. So I believe it is a class availability issue. We need something like #initclip, but for AS2:

http://www.friendsofed.com/books/1590591658/as_ref_demo/htm/(initclip).htm
PermalinkPermalink 20/11/06 @ 08:18
Comment from: Scottae
I don't ever have this problem, and I have classes that extend MovieClip that are more than 2 subclasses deep. Maybe extending the V2 components has something to do with it? I try not to use the component stuff if possible.
PermalinkPermalink 27/11/06 @ 14:59
Comment from: Richard Leggett [Member] Email
Hey Scottae, good to hear from you. Unfortunately this occurs when extending some very simple classes that aren't part of the v2 component set. What's funny is as you know I've been using AS2 for quite a few years now since it was released, and this problem has only just started to manifest itself (I haven't changed the way I work as far as I'm aware). I'm not sure how this happened but it feels like the compiler has changed.

Check out this for more info:

http://richardleggett.co.uk/blog/index.php/2006/11/20/movieclipcastingfails_2
PermalinkPermalink 27/11/06 @ 15:06

Comments are closed for this post.

About

Richard Leggett is a Freelance RIA and Web Developer (download CV, skills/portfolio coming soon). He is co-author of Foundation Flash Applications for Mobile Devices (Friend of ED), an Adobe Community Expert and speaker at industry conferences and user groups.



July 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Categories

Miscellany

XML Feeds

Information

Contact the admin  /   b2evo template by Two18 Media