0

I'm learning AS3 and I understand that there are a bunch of related questions here with this type of error but I can't seem to figure it out.

I'm getting this error:

TypeError: Error #1034: Type coercion failed: cannot convert bej_cs5_fla::MainTimeline@330ae041 in Board.
at BoardTimer/gameOver()
at BoardTimer/countdown()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

I have to classes. Class Board and class BoardTimer.

Board:

public class Board extends MovieClip {
    //Attributes
    public var boardSide:uint;

    public function Board(dimention:uint) {
                 boardSide = dimention;
        // Code goes here
    }
   }

BoardTimer:

    public class BoardTimer extends Board{

    public function BoardTimer(dimention:uint)
    {
        boardSide2 = dimention;
        super(dimention);
        gameTimerBox = new TextField();
        myTimer = new Timer(1000,count);
        myTimer.addEventListener(TimerEvent.TIMER, countdown);
        myTimer.start();
    }
     }

And some BoardTimer methods:

    function countdown(event:TimerEvent):void
    {
        gameTimerBox.x = 700;
        gameTimerBox.y = 200;
        gameTimerBox.textColor = 0xFFFFFF;
        gameTimerBox.text = String((count)-myTimer.currentCount);
        if (gameTimerBox.text == "0")
        {
            gameOver();
            gameTimerBox.text = String("Game Over");
        }
        addChild(gameTimerBox);
    }

    function gameOver()
    {
        trace(Board(parent).boardSide);
    }

In one frame I have this:

dimention=10;
var boardTimer_mc= new BoardTimer(dimention);
boardTimer_mc.x=25;
boardTimer_mc.y=25;
addChild(boardTimer_mc);

and in another I have this:

var dimention:uint=10;
var board_mc: Board = new Board(dimention);
board_mc.x=25;
board_mc.y=25;
addChild(board_mc);

BoardTimer is doing all that Boardis doing but I'm failing to get access to Board methods and variables. I've tried trace(Board(parent).boardSide);, trace(Board(this.parent).boardSide); and trace(Board(this.parent.parent).boardSide); and nothing.

What am I doing wrong?

1 Answer 1

1

parent doesn't refer to base class or the parent class you derived from. It refers to the parent in the displaylist on the stage. (read this)

Use super keyword for referring to the base class. Also, when you are inheriting the class, all the protected & public methods & variables would be available as it is in the derived class.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.