2D Flashdevelopment game; src\Main.as(301): col: 27 Error: Access of undefined property mapIndex.loadMap(doors[d].dest,mapIndex);

2D Flashdevelopment game; src\Main.as(301): col: 27 Error: Access of undefined property mapIndex.loadMap(doors[d].dest,mapIndex);

我目前正在 FlashDevelop 中制作一个 2d 游戏,我似乎无法弄清楚我的代码中的问题所在。当我尝试 运行 游戏时收到这些错误消息:

src\Main.as(94): col: 4 Error: Access of undefined property mapIndex. mapIndex = 0;

src\Main.as(218): col: 4 Error: Access of undefined property mapIndex. mapIndex = index;

src\Main.as(301): col: 27 Error: Access of undefined property mapIndex. loadMap(doors[d].dest,mapIndex);

有人可以帮助我吗?

package 

{
    import flash.display.InteractiveObject;
    import flash.display.MovieClip;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.events.TextEvent;
    import flash.text.TextField;
    import flash.text.TextFormat;

    /**
     * ...
     * @author Umeer
     */
    public class Main extends Sprite 
    {   
        private var cam:MovieClip = new MovieClip();
        private var aDown:Boolean = false;
        private var dDown:Boolean = false;
        private var spaceDown:Boolean = false;
        private var char:Player = new Player();
        private var bullets:Array = [];
        private var blocks:Array = [];
        private var coins:Array = [];
        private var enemies:Array = [];
        private var keys:Array = [];
        private var doors:Array = [];
        private var ladders:Array = [];
        private var climbing:Boolean = false;
        private var hp:HealthBar = new HealthBar(0x00FFFF);
        private var enhp:HealthBar = new HealthBar(0xFF6600);
        private var score:int = 0;
        private var keyCount:int = 0;
        private var current:int = 0;
        private var textBox:TextField = new TextField();
        private var textStyle:TextFormat = new TextFormat("Arial", 18);
        private var enemyNameBox:TextField = new TextField();
        private var keyTextBox:TextField = new TextField();
        private var maps:Array = [
        [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 901],
        [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1],
        [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0],
        [1, 1, 1, 0, 0, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 4],
        [1, 1, 1, 1, 0, 3, 0, 0, 3, 0, 0, 1, 0, 0, 0, 1]],
        [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
        [0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 0, 3, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1],
        [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0],
        [0, 0, 0, 900, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4],
        [1, 1, 1, 1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 1]]
        ];
        private var enemyNames:Array = ["Enemy 1" , "Enemy 2" , "Enemy 3" ];
        public function Main():void 
        {   
            addChild(cam);
            hp.x = 150;
            hp.y = 23;
            addChild(hp);
            textBox.x = 10;
            textBox.y = 10;
            textBox.defaultTextFormat = textStyle;
            addChild(textBox);
            enemyNameBox.x = 300;
            enemyNameBox.y = 35;
            enemyNameBox.defaultTextFormat = textStyle;
            enemyNameBox.width = 300;
            keyTextBox.x = 400;
            keyTextBox.y = 10;
            addChild(keyTextBox);
            keyTextBox.defaultTextFormat = textStyle;
            addChild(enemyNameBox);
            char.x = 400;
            char.y = 300;
            cam.addChild(char);
            enhp.x = 300;
            enhp.y = 23;
            addChild(enhp);
            loadMap(0, -1);
            mapIndex = 0;
            stage.addEventListener(Event.ENTER_FRAME, check);
            stage.addEventListener(KeyboardEvent.KEY_DOWN, keysDown);
            stage.addEventListener(KeyboardEvent.KEY_UP, keysUp);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
            cam.setChildIndex(char, cam.numChildren - 1);
        }
        public function loadMap(index:int,current:int)
        {
            if (cam.numChildren > 1)
            {
                while (cam.numChildren > 0)
                    cam.removeChildAt(0);
            }
            if (current != -1)
            {
                var newArr:Array = [];
                for (var i:int = 0; i < maps[current].length; i++)
                    newArr.push([]);
                for (var s:int = 0; s < maps[current].length; s++)
                {
                    for (var h:int = 0; h < maps[current][0].length; h++)
                        newArr[s].push(0);
                }
                for (var b:int = 0; b < blocks.length; b++)
                    newArr[(blocks[b].y - 25) / 50][(blocks[b].x - 25) / 50] = 1;
                for (var c:int = 0; c < coins.length; c++)
                    newArr[(coins[c].y - 25) / 50][(coins[c].x - 25) / 50] = 2;
                for (var l:int = 0; l < ladders.length; l++)
                {
                    if (ladders[l].top)
                        newArr[(ladders[l].y - 25) / 50][(ladders[l].x - 25) / 50] = 6;
                    else 
                        newArr[(ladders[l].y - 25) / 50][(ladders[l].x - 25) / 50] = 5;
                }
                for (var d:int = 0; d < doors.length; d++)
                {
                    if (doors[d].unlocked)
                        newArr[(doors[d].y - 25) / 50][(doors[d].x - 25) / 50] = 1000+doors[d].dest;
                    else 
                        newArr[(doors[d].y - 25) / 50][(doors[d].x - 25) / 50] = 900 + doors[d].dest;
                }
                maps[current] = newArr;
            }
            bullets = [];
            blocks = [];
            coins = [];
            enemies = [];
            keys = [];
            doors = [];
            ladders = [];
            char.floor = 450;
            for (var i:int = 0; i < maps[0].length; i++)
            {
                for ( var j:int = 0; j < maps[0][0].length; j++)
                {
                    if (maps[index][i][j] == 1)
                    {
                        var block:Block = new Block();
                        block.x = j * 50 + 25;
                        block.y = i * 50 + 25;
                        blocks.push(block);
                        cam.addChild(block);
                    }
                    if (maps[index][i][j] == 2)
                    {
                        var coin:Coin = new Coin();
                        coin.x = j * 50 + 25;
                        coin.y = i * 50 + 25;
                        coins.push(coin);
                        cam.addChild(coin);
                    }
                    if (maps[index][i][j] == 3)
                    {
                        var enem:Enemy = new Enemy(enemyNames[enemies.length]);
                        enem.x = j * 50 + 25;
                        enem.y = i * 50 + 25;
                        enemies.push(enem);
                        cam.addChild(enem);
                    }
                    if (maps[index][i][j] == 4)
                    {
                        var key:Key = new Key();
                        key.x = j * 50 + 25;
                        key.y = i * 50 + 25;
                        /*cam.addChild(key);*/
                        keys.push(key);
                    }
                    if (maps[index][i][j] == 5)
                    {
                        var ladder:Ladder = new Ladder(false);
                        ladder.x = j * 50 + 25;
                        ladder.y = i * 50 + 25;
                        cam.addChild(ladder);
                        ladders.push(ladder);
                    }
                    if (maps[index][i][j] == 6)
                    {
                        var ladder:Ladder = new Ladder(true);
                        ladder.x = j * 50 + 25;
                        ladder.y = i * 50 + 25;
                        cam.addChild(ladder);
                        ladders.push(ladder);
                    }
                    if (maps[index][i][j] >= 1000)
                    {
                        var door:Door = new Door(maps[index][i][j]-1000);
                        door.x = j * 50 + 25;
                        door.y = i * 50 + 25;
                        door.unlocked = true;
                        cam.addChild(door);
                        doors.push(door);
                    }else
                    if (maps[index][i][j] >= 900)
                    {
                        var door:Door = new Door(maps[index][i][j]-900);
                        door.x = j * 50 + 25;
                        door.y = i * 50 + 25;
                        cam.addChild(door);
                        doors.push(door);
                    }
                }
            }
            cam.addChild(char);
            mapIndex = index;
        }
        public function mDown(e:MouseEvent)
        {
            var ang:Number = 0;
            if (mouseX > 400)
                ang = (Math.atan(((mouseY - cam.y) - char.y) / ((mouseX - cam.x) - char.x)));
            else 
                ang = -1 * (Math.atan(((mouseY - cam.y) - char.y) / ((mouseX - cam.x) - char.x)));
            var xSpeed:Number = Math.cos(ang) * 45;
            var ySpeed:Number = Math.sin(ang) * 45;
            if(mouseX<400)
                xSpeed *= -1;
            var bullet:Bullet = new Bullet(char.x, char.y, xSpeed, ySpeed);
            cam.addChild(bullet)
            bullets.push(bullet)
        }
        public function check(e:Event):void
        {   
            textBox.text = "Score: " + score;
            keyTextBox.text = "Keys: " + keyCount;
            hp.scaleX = char.health / 100;
            if (enemies.length > 0)
            {
                enhp.scaleX = enemies[current].health / 100;
                enemyNameBox.text = enemies[current].name2;
            }
            else 
            {
                enhp.scaleX = 0;
                enemyNameBox.text = "All enemies cleared";
            }
            var prevX:int = char.x;
            var prevY:int = char.y;
            if (aDown)
                char.x -= 5;
                /*cam.x += 5;*/
            if (dDown)
                char.x += 5;
                /*cam.x -= 5; the camera moves along with the ball but it keeps meoving when the ball hits the wall*/
            char.adjust();
            for ( var i:int = 0; i < blocks.length; i++)
            {
                blocks[i].CheckObj(char, i);
                for (var s:int = 0; s < enemies.length; s++)
                    blocks[i].CheckObj(enemies[s], i);
            }
            for ( var j:int = enemies.length-1; j >= 0; j--)
            {
                var result:int = enemies[j].CheckObj(char, bullets);
                if(result== -1)
                {   
                    cam.removeChild(enemies[j])
                    enemies.splice(j, 1);
                    current = 0;
                    score += 10;
                }else 
                if (result != -2)
                {
                    cam.removeChild(bullets[result]);
                    bullets.splice(result, 1);
                    current = j;
                }
            }
            for ( var k:int = 0; k < bullets.length; k++)
            {
                bullets[k].check();
            }
            /*cam--; this code moves the screen to the right*/
            for ( var s:int = coins.length-1; s >= 0; s--)
            {
                if (coins[s].CheckObj(char))
                {
                    cam.removeChild(coins[s]);
                    coins.splice(s, 1);
                    score++;
                }
            }
            for (var d:int = 0; d < doors.length; d++)
            {
                if (!doors[d].unlocked)
                    keyCount--;
                doors[d].unlocked = true;
                loadMap(doors[d].dest,mapIndex);

            }
            for (var w:int = 0; w < keys.length; w++)
            {
                if (keys[w].CheckObj(char, enemies) == 1)
                {
                    cam.addChild(keys[w]);
                    keys[w].vis = true;
                }
                if (keys[w].CheckObj(char,enemies)==2)
                {
                    cam.removeChild(keys[w]);
                    keys.splice(w, 1);
                    keyCount++;
                }
            }
            for (var l:int = 0; l < ladders.length; l++)
            {
                (ladders[w].CheckObj(char, 1, climbing));
            }
            cam.x -= char.x - prevX;
            cam.y -= char.y - prevY;
            spaceDown = false;
        }
        public function keysDown(e:KeyboardEvent):void
        {
            if (e.keyCode == 65)
                aDown = true;
            if (e.keyCode == 68)
                dDown = true;
            if (e.keyCode == 87&&char.y+char.width/2==char.floor)
                char.grav = -15;
        }
        public function keysUp(e:KeyboardEvent):void
        {
            if (e.keyCode == 65)
                aDown = false;
            if (e.keyCode == 68)
                dDown = false;
        }
    }

}

mapIndex 是一个未声明的变量。

在你的程序中,class Main 扩展了 class Sprite,所以首先让我们看看它是否可能继承那个变量(或 属性)来自 Sprite。文档在这里:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/display/Sprite.html

在您的浏览器中,运行 搜索 mapIndex,您会发现它不在该页面上。所以即使 class Sprite 有一个变量或 属性 叫做 mapIndex,它也是私有的,所以 Main 仍然没有声明它。

所以它不是继承的 class variable/property,但它可能是局部变量或专门添加到 class Main 的东西。 运行 搜索 var mapIndex 实际上不起作用,因为它可能是函数的参数。 运行 搜索 mapIndex: 也不起作用,因为 ActionScript 3.0 不要求变量、属性或函数具有类型/return 类型。所以你只需要 运行 搜索 mapIndex,查看每个单独的位置,看看其中是否有声明:

1: mapIndex = 0;
2: mapIndex = index;
3: loadMap(doors[d].dest,mapIndex);

这些都不是声明,但它们都以非动态方式引用 variable/property。 (换句话说,您是通过说 mapIndex 而不是 this["mapIndex"] 来访问它。)尝试以这种方式访问​​它需要一个声明,这就是您的代码被破坏的原因。