Savvas Ant
Savvas Ant

Reputation: 25

as3 starling tile array

Im trying to make a tile array system using flashDevelop and starling. I think i complete that but i dont understand how i make bounds,intersects between the tile array and my hero. Note that i make it using tutorials around the internet and not along.

Here are the 2 classes

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

    }

}

So how i write if the hero hit the map array object 1?

Upvotes: -1

Views: 64

Answers (1)

Sephiroth88
Sephiroth88

Reputation: 3

Add all the parts of the tile array to a single display object then just call intersects bounds on the single display object.

Upvotes: 0

Related Questions