as3 starling tile数组

时间:2017-07-26 16:25:21

标签: actionscript-3 flashdevelop starling-framework

我正在尝试使用flashDevelop和starling创建一个tile数组系统。 我想我完成了,但我不明白我是如何制作边界,在瓷砖阵列和我的英雄之间相交。 请注意,我使用互联网上的教程,而不是沿着它。

以下是2个班级

public class level1 extends Sprite 
{
    public var map:Array = [
                            [1, 1, 1, 1, 1, 1],
                            [1, 0, 0, 0, 0],
                            [1, 0, 0, 0, 1],
                            [1, 0, 0, 0, 1],
                            [1, 0, 0, 0, 1],
                            [1, 0, 0, 0, 1],
                            [1, 0, 0, 0, 1],
                            [1, 0, 0, 0, 1],
                            [1, 1, 1, 1, 1],
                            ];
    //public var object:Image;
    //public var data:int;


    public function level1() 
    {
        super();
        this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAdd);
    }

    private function onAdd(event:Event):void
    {
        drawScreen(map, 90);
    }

    public function drawScreen(map:Array, cellSize:int = 90):void
    {
        for(var row:int = 0; row < map.length; row++)
        {
            for(var column:int = 0; column < map[row].length; column++)
            {
                var data:int = map[row][column];
                // Empty tile, move onto the next item.
                if(data == 0) continue;
                var object:Image;
                if (data == 1) object = new Image(Assets.getTexture("ESAYBTN"));
                if (data == 2) object = new Image(Assets.getTexture("TITLE"));
                if(object != null)
                {
                    object.x = column * 94;
                    object.y = row * 29;
                    stage.addChild(object);
                }
            }
        }
    }
}


public class inGame extends Sprite 
{
    public var Hero:heroClass;
    private var enem2:enemy2Class;
    private var enemiesArray:Vector.<enemy2Class>;
    private var posX:int;
    private var posY:int;
    private var hitpoints:int;
    private var _level1:level1;

    public function inGame() 
    {
        super();
        this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAdd);
    }

    private function onAdd(event:Event):void
    {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAdd);
        enemiesArray = new Vector.<enemy2Class>();

        drawScreen();
    }

    private function drawScreen():void
    {


        Hero = new heroClass(50, 50, 1);
        this.addChild(hero);

        _level1 = new level1();
        this.addChild(_level1);

        createenemies(450, 50, 6);
        createenemies(400, 50, 5);
        createenemies(350, 50, 4);
        createenemies(300, 50, 3);
        createenemies(250,50, 2);
    }

    public function createenemies(posX:int, posY:int, hitpoints:int):void
    {
        var enemies:enemy2Class = new enemy2Class(posX,posY,hitpoints);
        this.addChild(enemies);
        enemiesArray.push(enemies);
    }

    public function hideInGame():void
    {
        this.visible = false;
    }

    public function showInGame():void
    {
        this.visible = true;

        this.addEventListener(Event.ENTER_FRAME, gameLoop);
    }

    private function gameLoop(Event):void
    {
        var enemiestoloop:enemy2Class;
        for (var i:uint = 0; i < enemiesArray.length; i++)
        {
            enemiestoloop = enemiesArray[i];
            //enemiestoloop.x-=2;
            if (enemiestoloop.bounds.intersects(enem.bounds))
            {
                enemiestoloop.x = 400;
                enemiestoloop._hitpoints--;
            }

            if (enemiestoloop._hitpoints <= 0)
            {
                enemiesArray.splice(i, 1);
                this.removeChild(enemiestoloop);
            }
        }
        hero.y++;
        if(hero.bounds.intersects("here goes the map???"))
          {
             hero.y--;
           }

    }

}

那么如果英雄击中地图数组对象1我怎么写?

1 个答案:

答案 0 :(得分:0)

将tile数组的所有部分添加到单个显示对象,然后只调用单个显示对象上的intersects bounds。