Actionscript 3 - 如何检查数组位置是否已存在

时间:2015-07-03 04:08:41

标签: arrays actionscript-3 flash flash-cs6

您好我的问题如上所述。我有一个随机生成的地牢与玩家和随机块。地牢中的所有房间都保存在ROOMS 2D阵列中,其中包含房间的数据。你有一个当前的Row和当前Col,这是玩家所在的位置。我需要知道的是如何说如果当前位置上方没有空间则更改外墙图形以关闭没有空间的出口/门。我有点工作,但无论如何我改变它总是有一个房间,如果我尝试添加代码将无法工作。我现在所拥有的是if if语句中的IF ROOMS [currentRow + 1] [currentCol](<等于down),因此如果存在一行,则通过执行gotoAndStop来更改图形。那么如何或哪个是确定某个职位是否存在的最佳方式,因为这样,它会随机回归,如“术语未定义且没有属性”。我也有非常脏的代码,对不起我新的,生病尝试稍后清理它但如果你们有任何感觉,我不会阻止你哈哈!

我很感激任何回复!这是我的房间类

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

        }
    }

}

}

1 个答案:

答案 0 :(得分:2)

您可以通过创建一些处理分配和访问2D数组中值的简单函数来简化代码。检查2D数组中的元素是否存在的方法可能如下所示:

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

在您的示例中使用类似:

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

虽然使用这种类型的任务,你可以从实现一个处理网格+单元格数据的类中受益匪浅,但这样做可以帮助你入门:

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