Question

This is my problem: I have a Shared class which extends Main class. In my Shared class i am instancing other classes and adding them to this. class

package 
{
import flash.display.Sprite;
public class Shared extends Main
{
    public var m:Mirror = new Mirror(span, i*span+j, _x, _y, size, d);
    public var g:Grid = new Grid(i*span+j,size, addl, span, _x, _y);
    public var b:Ball = new Ball(30); 
    public var mc:MovieClips = new MovieClips(30, _x, _y, size, span); 
    public var l:Level = new Level(levelCount); 
    public function Shared(){
        this.addChild(m); 
        this.addChild(g);
        this.addChild(b);
        this.addChild(mc);
        this.addChild(l);
    }
}

}

Now, in my main i also use an instance of Shared like so:

private function init(e:Event = null):void 
    {
        var sh:Shared = new Shared();

        stage.addChild(sh.b);
        stage.addChild(sh.mc);
        stage.addChild(sh.l);

        for (i = 0; i < span; i++) 
        {
           for (j = 0; j < span; j++) 
           {
               stage.addChild(sh.g);
               if(addl.indexOf(i*span+j)==true){
                    stage.addChild(sh.m);
                }
            }
        }
    }

I don't get any errors, but none of the objects appear on stage, i can't find the problem. I think my problem is Shared class is not extending Sprite, but then i would have to pass a huge amount of arguments to Shared, is there any way to bypass this? So i can still extend Main...

Était-ce utile?

La solution

I don't think you want to have your Shared class inherit from your Main class, remember that by using the extends Main after your class, you inherit all the code from it, which means you're inheriting the function that creates an instance of your Shared class, which may cause a stack overflow.

Extending a class is not the same as getting an instance of your class. All the i/j/size etc properties of the main class are a completely separate instance than the ones that will be in your Shared class that inherits Main.

Here is probably what you want to do:

public class Shared extends Sprite { //extend Sprite instead of Main
    public var m:Mirror; //don't instantiate these objects yet, just declare them
    public var g:Grid;
    public var b:Ball; 
    public var mc:MovieClips; 
    public var l:Level; 

    public function Shared(main:Main){  //pass in a reference to your main class instance
        //instanciate these objects in your constructor
        m = new Mirror(main.span, main.i*main.span+main.j, main._x, main._y, main.size, main.d); //all these vars (span, i,j,_x,_y, size, d, addl) are not defined anywhere that you've shown, I'm assuming they are part of your main class
        g = new Grid(main.i*main.span+main.j,main.size, main.addl, main.span, main._x, main._y);
        b = new Ball(30); 
        mc = new MovieClips(30, main._x, main._y, main.size, main.span); 
        l = new Level(main.levelCount); 

        this.addChild(m); 
        this.addChild(g);
        this.addChild(b);
        this.addChild(mc);
        this.addChild(l);
    }
}

In your main class:

private function init(e:Event = null):void 
{
    var sh:Shared = new Shared(this); //pass a reference to this Main class instance

    //stage.addChild(sh.b); // just add the Shared instance to the stage directly
    //stage.addChild(sh.mc);
    //stage.addChild(sh.l);

    addChild(sh); //you only need to add the Shared class instance, since you've already added those other objects in the Shared class

    /* no idea what your trying to do here, but I don't think it's what you're expecting
    for (i = 0; i < span; i++) 
    {
       for (j = 0; j < span; j++) 
       {
           stage.addChild(sh.g);
           if(addl.indexOf(i*span+j)==true){
                stage.addChild(sh.m);
            }
        }
    }
    */
}

Please explain your code a bit more and I can update this answer to be more relevant.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top