0
votes

I'm doing a simple bit of code. I have a class Plugin that is meant to be extended, with a function initialise():

package me.bleachisback.cocmod {
    import me.bleachisback.cocmod.CoCMain;

    public class Plugin {
        private var _main:CoCMain;
        private var _name:String;
        private var _desc:String;
        private var _ver:String;
        private var _auth:String;
        internal var _enabled:Boolean = false;

        internal function initialise(main:CoCMain, name:String, description:String, version:String, author:String):void {
            _main = main;
            _name = name;
            _desc = description;
            _ver = version;
            _auth = author;
        }
    }
}

And my main class which loads the plugins, and then initialises them:

package me.bleachisback.cocmod {
    import me.bleachisback.cocmod.Plugin;

    public class CoCMain extends MovieClip {
        private function pluginLoadingComplete(e:Event):void {
            //This class extends Plugin
            var plugin:Object = new e.target.content.mainClass();
            var desc:XML = new XML(e.target.content.description);
            trace(this); //outputs: [object CoCMain]
            plugin.initialise(this, desc.name[0], desc.description[0], desc.version[0], desc.author[0]);
        }
    }
}

But at the end, when I try to use the initialise function, it gives me this error:

TypeError: Error #1034: Type Coercion failed: cannot convert me.bleachisback.cocmod::CoCMain@4dfc041 to me.bleachisback.cocmod.CoCMain.
    at me.bleachisback.cocmod::CoCMain/pluginLoadingComplete()

Implying I cannot convert a class to itself???

1
have you tried doing something along the lines of... var plugin:e.target.content.mainClass() = new e.target.content.mainClass(); or setting a class variable... var newClass:Class = e.target.content.mainClass(); and then var plugin:newClass = new newClass(); - Jonny Henly
Yes, that is what I did at first, and then it gave me this error: 1046: Type was not found or was not a compile-time constant: newClass. I have also tried var plugin:Plugin = new e.target.content.mainClass(); but then it just gives me this error: Error #1034: Type Coercion failed: cannot convert plugin.bleachisback.testPlugin::TestMain@afc6309 to me.bleachisback.cocmod.Plugin even though TestMain does extend Plugin. - user2885503
in your initializing function, try initializing _main and then setting it equal to main, i.e. _main = new CoCMain(); and then _main = main. The error is saying that your trying to convert the object CoCMain@4dfc041, which is a CoCMain Object at memory location 4dfc041, to a Class. - Jonny Henly
Nope, doesn't work. The code never even gets to that point, because the error occurs in CoCMain/pluginLoadingComplete(), and not Plugin/initialise(). - user2885503
This usually happeing when you have the same class compiled by different SWF's - are you loading SWF with the CoCMain to another SWF? - You may try to exclude this class from this SWF (not so easy when Flash and fla is used). - Lukasz 'Severiaan' Grela

1 Answers

0
votes

when you load an external SWF, you can specify the applicationDomain in which class definitions will be loaded. by default, an external loaded SWF will place its class definition in a new application domain, so classes named equally won't be replaced in the original application domain.

Try to load the SWF into the current application domain, like this:

var ldrContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain); 

Note the ApplicationDomain.currentDomain when creating the LoaderContext.

You can check an example here: http://help.adobe.com/en_US/as3/dev/WSd75bf4610ec9e22f43855da312214da1d8f-8000.html