Flash AS3 - Objects of same base class in library - Type coercion failed
This happens to me a lot and I've yet to find a good solution. Say you have two classes, Tree (com.company.Tree) and Fruit (com.company.Fruit). On the stage in Flash, the Tree has an instance of Fruit (class=fruit1, base class=com.company.Fruit), and it's instance name is fruit. Everything is fine, until you duplicate the tree and fruit movie clips to create a 2nd tree, which has a DIFFERENT instance of Fruit (class=fruit2, base class=com.company.Fruit), but whose instance name in the 2nd tree is also fruit. This is necessary because the code for the Tree class references "fruit" (fruit.x += 20, for example).
I get this:
Warning: All instances na开发者_如何转开发med 'fruit' will be automatically declared as fruit1 in symbols that use fruit as their base class. In symbols that are linked to user-defined base classes, instances with the same name must be of the same type.
And of course this:
TypeError: Error #1034: Type Coercion failed: cannot convert fruit2@4be85871 to fruit1.
Are you using the compile option to "Automatically declare stage instances"? In this case, you may have to disable that and add:
public var fruit:Fruit;
to your Tree class. This would assign "fruit" to your MovieClip on the stage with an instance name of "fruit" and avoid the conflict. Check this post out for more.
typeoneerror's answer does work. Flash will set the type of fruit2 to fruit1 which is essentially the same thing as actually setting it's Base to Fruit.
A pretty annoying warning but everything works fine
http://jongrant.wordpress.com/2012/01/10/as3-warning-all-instances-named-will-be-automatically-declared-as-in-symbols-that-use-as-their-base-class-in-symbols-that-are-linked-to-user-defined-base-classes-instances-with-the-same-name/
This solution works perfect for me, just uncheck the box and everthing is ok.
File->Publish Settings->Flash->Settings… you have Automatically declare stage instances
精彩评论