0
votes

So I'm creating a platform game in Actionscript 3.0, trying to call a function that spawns blocks based on an array. The code is in a 'Game' class and is directed towards a movieclip on my .fla When it is ran I get the error: "cannot convert flash.display::Stage@2a2cdf99 to flash.display.MovieClip." Here's the code:

public function GameScreen(stageRef:Stage = null ) 
    {

        this.stageRef = stageRef;
        btnReturn.addEventListener(MouseEvent.MOUSE_DOWN, returnMainMenu,  false, 0, true);
        mcMain.addEventListener(Event.ENTER_FRAME, moveChar);
        this.addEventListener(Event.ENTER_FRAME, createLvl);
        this.stageRef.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
        this.stageRef.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
        this.stageRef.addChild(blockHolder);
    }

And

private function createLvl(event:Event):void
    {
        var lvlArray:Array = MovieClip(root)['lvlArray' +lvlCurrent];
        var lvlColumns:int = Math.ceil(lvlArray.length/16);
        for(var i:int = 0;i<lvlArray.length;i++){
            if(lvlArray[i] == 1){
                if(i/lvlColumns == int(i/lvlColumns)){
                    row ++;
                }
                var newBlock:Block = new Block();
                newBlock.graphics.beginFill(0xFFFFFF);
                newBlock.graphics.drawRect(0,0,50,50);
                newBlock.x = (i-(row-1)*lvlColumns)*newBlock.width;
                newBlock.y = (row - 1)*newBlock.height;
                blockHolder.addChild(newBlock);
            } else if (lvlArray[i] == 'MAIN'){
                mcMain.x = (i-(row-1)*lvlColumns)*newBlock.width;
                mcMain.y = (row-1)*newBlock.height;
            }
        }
        row = 0;
    }

Please help =(

Thanks!

1
What type is the stageRef property of GameScreen?net.uk.sweet

1 Answers

0
votes

First of all:

  1. Turn on "permit debugging" in your publish settings. This will give you line numbers with your errors, so you can determine the exact location of your error.

  2. Post the entire stack trace. That error by itself is not a lot to go on.

Given the error and the code you've posted, the error must be caused by your use of MovieClip(root). The root property does not always point to the main timeline in Flash, it will point to the Stage if the display object is added directly to the stage. For example:

trace(stage.addChild(new Sprite()).root) // [object Stage]

Documentation on root: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/DisplayObject.html#root