AS3访问父方法和变量时出错

时间:2013-05-19 16:53:28

标签: actionscript-3

我正在学习AS3,我知道这里有很多相关的问题,但我似乎无法弄明白。

我收到了这个错误:

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()

我必须上课。课程Board和课程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();
    }
     }

以及一些BoardTimer方法:

    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);
    }

在一个框架中我有这个:

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

在另一个我有这个:

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

BoardTimer正在执行Board所做的一切,但我无法访问Board方法和变量。我已经尝试了trace(Board(parent).boardSide);trace(Board(this.parent).boardSide);trace(Board(this.parent.parent).boardSide);而没有尝试。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

parent不引用基类或派生自的父类。它指的是舞台上的置换列表中的父级。 (阅读this

使用super关键字来引用基类。此外,当您继承该类时,所有受保护的&公共方法和变量将在派生类中可用。