我目前正在研究2D平台游戏,当我尝试运行时,我收到此错误消息 游戏:[Fault] exception,information = ReferenceError:错误#1069:在Number上找不到属性1,没有默认值。 故障,loadMap()在Main.as:253 有人可以告诉我如何解决这个问题?
这是我在main中的完整代码:
package
{
import flash.display.Bitmap;
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.ui.Mouse;
import flash.utils.*;
import flash.events.TextEvent;
import flash.globalization.Collator;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.ui.Mouse;
import flash.utils.getQualifiedClassName;
/**
* ...
* @author Umeer
*/
public class Main extends Sprite
{
[Embed(source="../Assets/wip_parallax_background_by_stormandy-d5n5tr6.png")]
public var BG:Class;
private var cam:MovieClip = new MovieClip();
private var aDown:Boolean = false;
private var dDown:Boolean = false;
private var wDown:Boolean = false;
private var sDown:Boolean = false;
private var leftDown:Boolean = false;
private var rightDown:Boolean = false;
private var spaceDown:Boolean = false;
private var char:Player = new Player();
private var char2:Player2 = new Player2();
private var bullets:Array = [];
private var blocks:Array = [];
private var coins:Array = [];
private var coins2:Array = [];
private var enemies:Array = [];
private var keys:Array = [];
private var doors:Array = [];
private var ladders:Array = [];
private var redwater1:Array = [];
private var bluewater1:Array = [];
private var climbing:Boolean = false;
private var hp:HealthBar = new HealthBar(0x00FFFF);
private var hp2:HealthBar = new HealthBar(0xFF0000);
private var enhp:HealthBar = new HealthBar(0xFF6600);
private var score:int = 0;
private var keyCount:int = 0;
private var current:int = 0;
private var mapIndex: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();
public var Restart:Replay;
public var ShowEnd:TextField = new TextField();
private var maps:Array = [
[
[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,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 0, 0, 0, 1, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]]
];
private var enemyNames:Array = ["Enemy 1" , "Enemy 2" , "Enemy 3" ];
public function Main():void
{
var background:Bitmap = new BG();
addChild(background);
addChild(cam);
hp.x = 150;
hp.y = 23;
addChild(hp);
hp2.x = 500;
hp2.y = 23;
addChild(hp2);
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 = 175;
char.y = 300;
cam.addChild(char);
char2.x = 125;
char2.y = 300;
cam.addChild(char2);
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(KeyboardEvent.KEY_DOWN, keysDown2);
stage.addEventListener(KeyboardEvent.KEY_UP, keysUp2);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_DOWN, m2Down);
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++)
{
if (!block[b].mover)
newArr[(blocks[b].y - 25) / 50][(blocks[b].x - 25) / 50] = 1;
else
newArr[(blocks[b].yHome - 25) / 50][(blocks[b].xHome - 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 u:int = 0; u < coins2.length; u++)
newArr[(coins2[u].y - 25) / 50][(coins2[u].x - 25) / 50] = 7;
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++)
newArr[(doors[d].y - 25) / 50][(doors[d].x - 25) / 50] = 8;
maps[current] = newArr;
newArr = [];
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++)
{
if (!block[b].mover)
newArr[(blocks[b].y - 25) / 50][(blocks[b].x - 25) / 50] = 1;
else
newArr[(blocks[b].yHome - 25) / 50][(blocks[b].xHome - 25) / 50] = [1,blocks[b].xSpeed,blocks[b].ySpeed,blocks[b].xDist,blocks[b].yDist];
}
for (var c:int = 0; c < coins.length; c++)
newArr[(coins[c].y - 25) / 50][(coins[c].x - 25) / 50] = 2;
for (var u:int = 0; u < coins2.length; u++)
newArr[(coins2[u].y - 25) / 50][(coins2[u].x - 25) / 50] = 7;
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++)
newArr[(doors[d].y - 25) / 50][(doors[d].x - 25) / 50] = [doors[d].des,doors[d].unlocked];
styles[current] = newArr; /* errors will ocur because this part is duplicate*/
}
bullets = [];
blocks = [];
coins = [];
coins2 = [];
enemies = [];
keys = [];
doors = [];
ladders = [];
redwater1 = [];
bluewater1 = [];
/*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();
/*insert code from video part 15 (12:18)*/
block.x = j * 50 + 25;
block.y = i * 50 + 25;
if (getQualifiedClassName(styles[index][i][j] == "Array"))
{
block.xSpeed = styles[index][i][j][1];
block.ySpeed = styles[index][i][j][2];
block.xDist = styles[index][i][j][3];
block.yDist = styles[index][i][j][4];
block.xHome = block.x;
block.yHome = block.y;
block.mover = true;
}
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] == 7)
{
var coin2:Coin2 = new Coin2();
coin2.x = j * 50 + 25;
coin2.y = i * 50 + 25;
coins.push(coin2);
cam.addChild(coin2);
}
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] >= 8)
{
var door:Door = new Door(styles[index][i][j][0]);
door.x = j * 50 + 25;
door.y = i * 50 + 25;
door.unlocked = styles[index][i][j][1];
if (door.dest == current)
{
char.x = door.x;
char.y = door.y;
char.floor = 10000;
}
cam.addChild(door);
doors.push(door);
}
if (maps[index][i][j] == 11)
{
var redwaters:RedWater = new RedWater();
redwaters.x = j * 50 + 25;
redwaters.y = i * 50 + 25;
redwater1.push(redwaters);
cam.addChild(redwaters);
}
if (maps[index][i][j] == 12)
{
var bluewaters:BlueWater = new BlueWater();
bluewaters.x = j * 50 + 25;
bluewaters.y = i * 50 + 25;
bluewater1.push(bluewaters);
cam.addChild(bluewaters);
}
}
}
cam.addChild(char);
cam.addChild(char2);
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)
}/*code for shooting bullets*/
public function m2Down(e:MouseEvent)
{
var ang:Number = 0;
if (mouseX > 400)
ang = (Math.atan(((mouseY - cam.y) - char2.y) / ((mouseX - cam.x) - char2.x)));
else
ang = -1 * (Math.atan(((mouseY - cam.y) - char2.y) / ((mouseX - cam.x) - char2.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(char2.x, char2.y, xSpeed, ySpeed);
cam.addChild(bullet)
bullets.push(bullet)
}/*code for shooting bullets*/
public function check(e:Event):void
{
textBox.text = "Score: " + score;
keyTextBox.text = "Keys: " + keyCount;
hp.scaleX = char.health / 100;
hp2.scaleX = char2.health / 100;
if (char.health > 0)
{
hp.scaleX = char.health / 100;
}
else
{
hp.scaleX = 0;
End();
}
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;
var prevX:int = char2.x;
var prevY:int = char2.y;
for ( var i:int = 0; i < blocks.length; i++)
{
blocks[i].CheckObj(char, i);
blocks[i].CheckObj(char2, i);
if (blocks[i].mover)
blocks[i].adjust(char, i);
for (var s:int = 0; s < enemies.length; s++)
blocks[i].CheckObj(enemies[s], i);
}
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*/
if (leftDown)
char2.x -= 5;
/*cam.x += 5;*/
if (rightDown)
char2.x += 5;
if(!climbing)
{
char.adjust();
char2.adjust();
}
else
{
if (wDown)
char.y -= 5;
if (sDown)
char.y += 5;
}
for ( var j:int = enemies.length-1; j >= 0; j--)
{
var result:int = enemies[j].CheckObj(char || char2 , 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 s:int = coins2.length-1; s >= 0; s--)
{
if (coins2[s].CheckObj(char2))
{
cam.removeChild(coins2[s]);
coins2.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[l].CheckObj(char, 1, climbing));
}
/*cam.x -= char.x - prevX;
cam.y -= char.y - prevY;*/ /*camera follows the player*/
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.onLadder !=-1))
{
if(char.onLadder==-1)
char.grav = -15;
else
{
climbing = true;
wDown = true;
}
}
if (e.keyCode == 83)
{
if (char.onLadder != -1)
climbing = true;
sDown = true;
}
}
public function keysUp(e:KeyboardEvent):void
{
if (e.keyCode == 65)
aDown = false;
if (e.keyCode == 68)
dDown = false;
if (e.keyCode == 83)
sDown = false;
if (e.keyCode == 87)
wDown = false;
}
public function keysDown2(e:KeyboardEvent):void
{
if (e.keyCode == 37)
leftDown = true;
if (e.keyCode == 39)
rightDown = true;
if (e.keyCode == 38&&char2.y+char2.width/2==char2.floor)
char2.grav = -15;
}
public function keysUp2(e:KeyboardEvent):void
{
if (e.keyCode == 37)
leftDown = false;
if (e.keyCode == 39)
rightDown = false;
}
public function End():void
{
removeEventListener(KeyboardEvent.KEY_DOWN, keysDown);
removeEventListener(KeyboardEvent.KEY_UP, keysUp);
removeEventListener(KeyboardEvent.KEY_DOWN, keysDown2);
removeEventListener(KeyboardEvent.KEY_UP, keysUp2);
removeEventListener(MouseEvent.MOUSE_DOWN, mDown);
removeEventListener(MouseEvent.MOUSE_DOWN, m2Down);
Mouse.show();
ShowEnd.text = "You died";
addChild(ShowEnd);
ShowEnd.width = 300;
Restart = new Replay();
addEventListener(MouseEvent.CLICK, restartgame);
addChild(Restart);
}
public function restartgame(e:Event):void
{
removeChild(Restart);
removeChild(ShowEnd);
keyCount = 0;
score = 0;
removeEventListener(MouseEvent.CLICK, restartgame);
init();
}
}
}
答案 0 :(得分:1)
我不得不抱怨你的代码很乱。 您应该将代码重构为几个类,以减少重复的代码等。
关于问题。 有问题的一行就是这一行:
block.xSpeed = styles[index][i][j][1];
为了简单起见,我假设index,i和j为0。 我们来看一下样式的定义:
private var styles:Array = [
[
[0, //and so on
根据上述假设:
styles[0][0][0]
是上面代码段中的数字0。将[1]
添加到该行的末尾会尝试在此编号中查找属性1,该属性不存在。
不是无休止地将数组相互嵌套,而是构建最能描述数据的数据结构。 不要对原始数据进行操作,而是提供在数据结构中访问它的方法。
而不是map[i][j]
,执行以下操作:map.get(i, j)
这允许您自行测试此函数,在错误的参数值上抛出错误等。
我还建议将数据序列化/反序列化为对象。在某些数组中,您可以读取属性,而不是某些数字。这使代码更具可读性。