Search code examples
arraysactionscript-3flashflash-cs6

Actionscript 3 - How to Check if an Array Position Already Exists


Hello my question is as stated. I have a randomly generated dungeon with a player and random blocks. All the rooms in the dungeon are saved in the ROOMS 2D array which contains the data for the rooms. You have a current Row and current Col which is where the player is at. What i need to know is how to say IF there is no room above the current position then change the outside wall graphic to close the exits/doors where there is no room. I have this somewhat working but everyway i change it there is always one room which just will not work if i try to add the code. What i have now is abunch of if statements saying IF ROOMS[currentRow + 1][currentCol](< that would be equal to down) so if one row up exists then change the graphic by doing gotoAndStop. So how or which is the best way to determine if a position exists because with this, it will randomly comeback with errors like "term is undefined and has no properties." Also i have really dirty code, sorry im new, ill try to clean it up later but if any of you feel like it, i wont stop you haha!

Im grateful for any replies! Here is my room class

package {
import flash.display.MovieClip;
import flash.events.Event;



public class Room extends MovieClip{

    var room1:Array = new Array();
    var room2:Array = new Array();
    var room3:Array = new Array();
    var room4:Array = new Array();
    var room5:Array = new Array();
    var room6:Array = new Array();
    var room7:Array = new Array();
    var room8:Array = new Array();
    var room9:Array = new Array();
    var room10:Array = new Array();

    var currentRow:int = 0;
    var currentCol:int = 0;

    var box:Box;
    var boxes:Array;
    var ROOMS:Array;
    var onTop:Boolean;
    var moved:Boolean;
    private var player:Player;
    private var walls:Walls;
    private var blocker1:Blocker;
    private var blocker2:Blocker;
    private var blocker3:Blocker;
    private var blocker4:Blocker;
    private var arrowImage:ArrowSymbol;

    public function Room(){
        init();

        createRooms();//add the walls + boxes of first room to the first array value // later make floors array that contains all the rooms and room array that contains all the boxes and enemies + events
        stage.addChild(ROOMS[currentRow][currentCol]);
        Constants.wallsRef = ROOMS[currentRow][currentCol];
        addEventListener(Event.ENTER_FRAME, update);
        stage.addChild(arrowCount);
        stage.addChild(arrowImage);

    }
    function init(){
        Constants.stageRef=stage;
        player = new Player();
        //add walls 
        walls = new Walls();
        Constants.wallsRef=walls;


        blocker1 = new Blocker();//BLOCKER WHEN PLAYER TOUCHES IT CHANGES ROOM
        blocker1.x = 350;
        blocker1.y = 1;
        stage.addChild(blocker1);

        blocker2 = new Blocker();
        blocker2.x = 350;
        blocker2.y = 619;
        stage.addChild(blocker2);

        blocker3 = new Blocker();
        blocker3.x = -30;
        blocker3.y = 300;
        blocker3.rotation = 90;
        stage.addChild(blocker3);

        blocker4 = new Blocker();
        blocker4.x = 700;
        blocker4.y = 300;
        blocker4.rotation = 90;
        stage.addChild(blocker4);

        Constants.blockerRef1 = blocker1;
        Constants.blockerRef2 = blocker2;
        Constants.blockerRef3 = blocker3;
        Constants.blockerRef4 = blocker4;
        //add player

        player.x = 300;
        player.y = 200;
        stage.addChild(player);

        arrowImage = new ArrowSymbol();
        arrowImage.x = 630;
        arrowImage.y = 30;



        box = new Box();
        boxes = new Array();
        ROOMS = new Array([room2],
                          [room6, room1, room5], /// THIS IS THE MAP OF THE FLOOR /// GOING UP ON THE GAME IS GOING DOWN ON IT
                          [room7, room8],
                          [room3, room9],
                          [room4]);//THIS WILL EVENTUALLY BE COMPLETELY RANDOMIZED//

        onTop = false;
        moved = false;

    }

    function update(e:Event){
        arrowCount.text = " " + Constants.arrowNumRef;//arrow amount left
        closeUnnecessaryExits();

        //UP
        if(Constants.blockerRef1.hitTestPoint(player.x,player.y) && moved != true){
            stage.removeChild(ROOMS[currentRow][currentCol]);//remove the room you are in so the new room doesnt overlap
            currentRow++;//change where the player is in
            stage.addChild(ROOMS[currentRow][currentCol]);//add new room
            Constants.wallsRef = ROOMS[currentRow][currentCol];//add colision
            player.y = 600;
            stage.addChild(arrowCount);
            stage.addChild(arrowImage);
            trace();
            moved = true;
        }else if(Constants.playerRef.hitTestObject(Constants.blockerRef1) == false && moved == true){
            moved = false;
        }

        //DOWN
        if(Constants.blockerRef2.hitTestPoint(player.x,player.y) && moved != true){
            //this will be where i want to change rooms
            stage.removeChild(ROOMS[currentRow][currentCol]);
            currentRow--;
            Constants.wallsRef = ROOMS[currentRow][currentCol];
            stage.addChild(ROOMS[currentRow][currentCol]);
            player.y = 10;//change to 600
            moved = true;
            trace("changed rooms");
            stage.addChild(arrowCount);
            stage.addChild(arrowImage);
        }else if(Constants.playerRef.hitTestObject(Constants.blockerRef1) == false && moved == true){
            moved = false;
        }


        //LEFT
        if(Constants.blockerRef3.hitTestPoint(player.x,player.y) && moved != true){
            stage.removeChild(ROOMS[currentRow][currentCol]);//remove the room you are in so the new room doesnt overlap
            currentCol--;//change where the player is in
            stage.addChild(ROOMS[currentRow][currentCol]);//add new room
            Constants.wallsRef = ROOMS[currentRow][currentCol];//add colision
            player.x = 600;
            stage.addChild(arrowCount);
            stage.addChild(arrowImage);
            moved = true;
        }else if(Constants.playerRef.hitTestObject(Constants.blockerRef1) == false && moved == true){
            moved = false;
        }

        //RIGHT
        if(Constants.blockerRef4.hitTestPoint(player.x,player.y) && moved != true){
            //this will be where i want to change rooms
            stage.removeChild(ROOMS[currentRow][currentCol]);
            currentCol++;
            Constants.wallsRef = ROOMS[currentRow][currentCol];
            stage.addChild(ROOMS[currentRow][currentCol]);
            player.x = 10;//change to 600
            moved = true;
            trace("changed rooms");
            stage.addChild(arrowCount);
            stage.addChild(arrowImage);
        }else if(Constants.playerRef.hitTestObject(Constants.blockerRef1) == false && moved == true){
            moved = false;
        }
    }

    function createRooms(){
        for(var r = 0; r <ROOMS.length; r++){
            walls = new Walls();
            addRandomBlocks();
            for(var c = 0; c < ROOMS[r].length; c++){
                walls = new Walls();
                addRandomBlocks();
                ROOMS[r][c] = walls;
            }



            trace(ROOMS[r][c]);
        }
    }
    //                    [room2, NaN],
    //                    [room6, room1, room5], /// THIS IS THE MAP OF THE FLOOR /// GOING UP ON THE GAME IS GOING DOWN ON IT
    //                    [room7, room8],
    //                    [room3, room9],
    //                    [room4]);
    function closeUnnecessaryExits(){
        trace("ROW: " + currentRow + "  COL: " + currentCol);

        var up = ROOMS[currentRow + 1];
        var down = ROOMS[currentRow - 1];
        var right = ROOMS[currentRow][currentCol + 1];
        var left = ROOMS[currentRow][currentCol - 1];
        //check to see which outside wasall to use
        if(ROOMS[currentRow + 1] == null && up && right && left){
            ROOMS[currentRow][currentCol].gotoAndStop(2);
        }else if(down == null && left == null && right && up){
            ROOMS[currentRow][currentCol].gotoAndStop(3);
        }else if(down == null && left == null && up == null && right){
            ROOMS[currentRow][currentCol].gotoAndStop(4);
        }else if(left == null && down && right && up){// IF HAVING PROBLEMS THEN MAKE THIS MAKE SURE ALL OTHER SIDES ARE TRUE
            ROOMS[currentRow][currentCol].gotoAndStop(5);
        }else if(down == null && left == null && right == null && up){
            ROOMS[currentRow][currentCol].gotoAndStop(6);
        }else if(down && up == null && right == null && left == null){
            ROOMS[currentRow][currentCol].gotoAndStop(7);
        }else if(ROOMS[currentRow + 1][currentCol] == null && ROOMS[currentRow - 1][currentCol] && left && right){
            ROOMS[currentRow][currentCol].gotoAndStop(8);
            trace("works 1");
        }else if(left && right && ROOMS[currentRow - 1][currentCol] == null && ROOMS[currentRow + 1][currentCol] == null){
            ROOMS[currentRow][currentCol].gotoAndStop(9);
            trace("works 2");
        }else if(left && ROOMS[currentRow - 1][currentCol] && right == null && ROOMS[currentRow + 1][currentCol] == null){
            ROOMS[currentRow][currentCol].gotoAndStop(10);// LEFT DOWN
            trace("works 3");
        }else if(left && ROOMS[currentRow + 1][currentCol] && ROOMS[currentRow - 1][currentCol] == null && right == null){
            ROOMS[currentRow][currentCol].gotoAndStop(11);//BROKEN left up
            trace("working 4");
        }else if(left && ROOMS[currentRow + 1][currentCol] == null && ROOMS[currentRow - 1][currentCol] == null && right == null){
            ROOMS[currentRow][currentCol].gotoAndStop(12);
            trace("works 5");
        }else if(right == null && left && up && down){
            ROOMS[currentRow][currentCol].gotoAndStop(13);
            trace("works 6");
        }
    }

    function addRandomBlocks(){
        for(var e=0; e <Math.random() * 10; e++){
            //trace("started block");
            box = new Box();
            box.x = Math.random() * (615 - 100) + 100;
            box.y = Math.random() * (500 - 120) + 120;
            //colision for block to block
            for(var col = 0; col < boxes.length; col++){
                if(box.hitTestObject(boxes[col])){
                    onTop = false;/// THIS NEEDS TO BE TRUE FOR THE DETECTION TO WORK
                    //trace("THIS BOX IS ON TOP OF ANOTHER");
                }
            }
            if(onTop == false){
                boxes.push(box);
                walls.addChild(box);
                trace("BOX CREATED " + onTop);
                //trace(boxes);
            }

        }
    }

}

}


Solution

  • You could streamline your code by creating some simple functions that deal with assigning and accessing values in your 2D array. One that checks if an element within a 2D array exists might look like:

    function cellExists(array:Array, x:int, y:int):Boolean {
        return array[y] !== undefined && array[y][x] !== undefined;
    }
    

    Used like, in your example:

    if (cellExists(ROOMS, currentCol, currentRow)) {
        //
    }
    

    Although with this type of task you would benefit greatly from implementing a class that handles grid + cell data, something along the lines of this to get you started:

    public class Grid {
        private var _content:Vector.<Vector.<Cell>>;
    
        public function Grid(columns:int, rows:int) {
            // Fill _content with empty slots based on columns, rows.
        }
    
        public function getCell(x:int, y:int):Cell {}
        public function cellExists(x:int, y:int):Boolean {}
    }