让孩子从其父AS3调用一个函数

时间:2015-04-09 00:24:46

标签: actionscript-3

所以我一直在阅读很多关于继承和循环依赖的内容,我认为没有办法解决这个问题。对多个班级来说还是有点新意。我的文档类创建了Field。我的Field类创建播放器并从Tile MovieClip推送bitmapData。我希望玩家能够将该bitmapData绘制到该字段中。 Engine.as(文档类):

package  {
import flash.display.MovieClip;

public class Engine extends MovieClip {

    private var field:Field;
    public static var _tilesData:Array = [];

    public function Engine() 
    {
        field = new Field();
        field.x = 0;
        field.y = 0;
        addChild(field);
    }
}

Field.as:

package  {
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;

public class Field extends MovieClip{

    private var player:Player;
    private var sampleTile:Tile = new Tile();

    public function Field() 
    {
        player = new Player();
        player.x = 0;
        player.y = 0;
        addChild(player);

        GetSampleTiles();
    }

    public function GetSampleTiles()
    {
        for (var i:int = 0;i < 3; i++)
        {
            sampleTile.gotoAndStop(i);

            var graphicData:BitmapData = new BitmapData(32,32);
            graphicData.draw(sampleTile);

            Engine._tilesData.push(graphicData);
        }
    }

    public function DrawATile(tileToDraw:BitmapData)
    {
        var newTile:Bitmap = new Bitmap(tileToDraw);
        newTile.x = player.x;
        newTile.y = player.y;
        addChild(newTile);
    }
}

} Player.as:

package  {

import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.display.BitmapData;
import flash.display.Bitmap;

public class Player extends MovieClip 
{
    public var _inp:Input = new Input();

    public function Player() 
    {
        addChild(_inp);
        addEventListener(Event.ENTER_FRAME, HandleKeys);
    }

    public function HandleKeys(e:Event)
    {
        if(_inp.keyUp)
        {
            y -= 32;
        }
        if(_inp.keyDown)
        {
            y += 32;
        }
        if(_inp.keyLeft)
        {
            x -= 32;
        }
        if(_inp.keyRight)
        {
            x += 32;
        }
        if(_inp.keySpace)
        {
            parent.DrawATile(Engine._tilesData[0]);
        }
    }
}

}

有没有比我看到更好的方法呢?当我在Player.as中放置DrawATile函数时,它可以工作......但是磁贴随着播放器一起移动。

1 个答案:

答案 0 :(得分:0)

我将DrawATile函数放在Player.as中,然后在其中将newTile添加到父类 - parent.addChild(newTile)。就这样,它将它添加到它的父级而不是它自身。