MyArray [3] [ - 2]失败(quelle surprise),怎么检测到这个? [英] MyArray[3][-2] fails (quelle surprise), how to detect this?

查看:71
本文介绍了MyArray [3] [ - 2]失败(quelle surprise),怎么检测到这个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

function getImage(id,x,y){

if(mpdef [0 + yy + y] [0 + xx + x] == undefined){

//设置为默认图像

document.getElementById(id).src = terrdef [13];

}

else {

document.getElementById(id).src = terrdef [mpdef [0 + yy + y] [0 + xx

+ x]];

}

返回;

}


我用这个函数来获取一个引用图形的索引号

档案。理论上,它会检查mpdef数组是否具有有效的
值作为指定的行/列索引,然后输入

默认索引号或从teh数组中获取索引号,取决于

是否mpdef数组在该位置是否有值。


然而,它似乎在任何时候都会窒息引用的行或列

是-1或更低。我该怎么理解?

-

-

Fabian

经常访问我的网站很长时间期间!
http://www.lajzar.co.uk

function getImage(id,x,y) {
if (mpdef[0 + yy + y][0 + xx + x] == undefined) {
// set to default image
document.getElementById(id).src = terrdef[13];
}
else {
document.getElementById(id).src = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}
return;
}

I use this function to grab an index number which refers to a graphics
file. In theory, firt it checks to see if the mpdef array has a valid
value as teh specified row/colum index, and then either puts in a
default index number or grabs the index number from teh array, depending
on whether the mpdef array has a value at that location.

However, it seems to choke when either teh referenced row or column
is -1 or lower. How should I catch this?
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk

推荐答案

" Fabian"写于2003年12月12日:
"Fabian" wrote on 12/11/2003:
function getImage(id,x,y){
if(mpdef [0 + yy + y] [0 + xx + x ] == undefined){
//设置为默认图像
document.getElementById(id).src = terrdef [13];
}
其他{
文件.getElementById(id).src = terrdef [mpdef [0 + yy + y] [0 +
xx + x]];
}


此退货声明不应该是必要的。纠正我,如果我是错误的b $ b,但结束括号应该创造一个隐含的回报。

返回;
}

我使用此函数来获取索引号,该索引号引用
图形文件。从理论上讲,它检查mpdef数组是否有一个
有效值作为指定的行/列索引,然后放入一个
默认索引号或从数组中获取索引号,
取决于mpdef数组是否在该位置具有值。

然而,当引用的行或列
为-1或更低时,它似乎会窒息。我该怎么理解?
function getImage(id,x,y) {
if (mpdef[0 + yy + y][0 + xx + x] == undefined) {
// set to default image
document.getElementById(id).src = terrdef[13];
}
else {
document.getElementById(id).src = terrdef[ mpdef[0 + yy + y][0 + xx + x] ];
}
This return statement shouldn''t be necessary. Correct me if I''m
wrong, but the closing brace should create an implicit return.
return;
}

I use this function to grab an index number which refers to a graphics file. In theory, firt it checks to see if the mpdef array has a valid value as teh specified row/colum index, and then either puts in a
default index number or grabs the index number from teh array, depending on whether the mpdef array has a value at that location.

However, it seems to choke when either teh referenced row or column
is -1 or lower. How should I catch this?




嗯,你有什么理由不能检查阵列索引

是否更大零,或等于零。如果其中任何一个为负数,则显示默认值为
。如果它们为零或正数,则检查数组

元素是否包含值。如果它没有显示默认值。如果它是
确实包含一个值,则显示:


if(0> x || 0> y)

{

document.getElementById(id).src = terrdef [13];

}

else if(undefined == mpdef [0 + yy + y] [0 + xx + x])

{

document.getElementById(id).src = terrdef [13];

}

其他

{

document.getElementById(id).src = terrdef [mpdef [0 + yy + y] [0 + xx

+ x]];

}


如果保证从右到左的评估,你可以减少以上

to:


if((undefined == mpdef [0 + yy + y] [0 + xx + x])||(0> ; x || 0>

y))

{

document.getElementById(id).src = terrdef [13];

}

其他

{

document.getElementById(id).src = terrdef [mpdef [0 + yy + y] [0 + xx

+ x]];

}


迈克


-

Michael Winter

M.Winter @ [no-spa m] blueyonder.co.uk(删除[无垃圾邮件]回复)



Umm, is there any reason why you can''t just check if the array indices
are greater than, or equal to, zero. If either are negative, display
the default. If they are zero or positive, then check if the array
element contains a value. If it doesn''t display the default. If it
does contain a value, display that:

if( 0 > x || 0 > y )
{
document.getElementById(id).src = terrdef[13];
}
else if( undefined == mpdef[0 + yy + y][0 + xx + x])
{
document.getElementById(id).src = terrdef[13];
}
else
{
document.getElementById(id).src = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}

If right-to-left evaluation is guaranteed, you can reduce the above
to:

if(( undefined == mpdef[0 + yy + y][0 + xx + x] ) || ( 0 > x || 0 >
y ))
{
document.getElementById(id).src = terrdef[13];
}
else
{
document.getElementById(id).src = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}

Mike

--
Michael Winter
M.Winter@[no-spam]blueyonder.co.uk (remove [no-spam] to reply)


" Fabian" < LA **** @ hotmail.com>写道:
"Fabian" <la****@hotmail.com> writes:
function getImage(id,x,y){
if(mpdef [0 + yy + y] [0 + xx + x] == undefined){
//设置为默认图像
document.getElementById(id).src = terrdef [13];
}
else {
document.getElementById(id)。 src = terrdef [mpdef [0 + yy + y] [0 + xx
+ x]];
}
返回;
}
然而,似乎当引用的行或列
为-1或更低时,阻塞。我该怎么抓住这个?


实际上,它只会在* first *坐标为负数时窒息(或者

任何其他与mpdef属性无关的值)

数组。


试试这个:


函数getImage(id,x,y){

var elem = document.getElementById(id);

var mpdefColumn = mpdef [0 + yy + y]; //我假设y表示column

if(mpdefColumn){

var mpdefCell = mpdefColumn [0 + xx + x];

if(mpdefCell!== undefined){

elem.src = terrdef [mpdefCell];

返回;

}

}

elem.src = terrdef [13];

}


if(mpdef [0 + yy + y] [0 + xx + x] == undefined){
function getImage(id,x,y) {
if (mpdef[0 + yy + y][0 + xx + x] == undefined) {
// set to default image
document.getElementById(id).src = terrdef[13];
}
else {
document.getElementById(id).src = terrdef[ mpdef[0 + yy + y][0 + xx
+ x] ];
}
return;
} However, it seems to choke when either teh referenced row or column
is -1 or lower. How should I catch this?
Actually, it only chokes when the *first* coordinate is negative (or
any other value that doesn''t correpspond to a property of the mpdef
array.

Try this:

function getImage(id,x,y) {
var elem = document.getElementById(id);
var mpdefColumn = mpdef[0+yy+y]; // I assume "y" means "column"
if (mpdefColumn) {
var mpdefCell = mpdefColumn[0+xx+x];
if (mpdefCell !== undefined ) {
elem.src = terrdef[mpdefCell];
return;
}
}
elem.src = terrdef[13];
}

if (mpdef[0 + yy + y][0 + xx + x] == undefined) {




如果未定义mpdef [0 + yy + y],则第二次查找失败。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML死亡颜色:< URL:http://www.in fimum.dk/HTML/rasterTriangleDOM.html>

''没有判断的信仰只会降低精神神圣。''



if mpdef[0+yy+y] is undefined, the second lookup fails.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
''Faith without judgement merely degrades the spirit divine.''


Lasse Reichstein Nielsen hu kiteb:
Lasse Reichstein Nielsen hu kiteb:
试试这个:

函数getImage(id,x,y){
var elem = document.getElementById(id);
var mpdefColumn = mpdef [0 + yy + y]; //我假设y表示列
如果(mpdefColumn){
var mpdefCell = mpdefColumn [0 + xx + x];
if(mpdefCell!== undefined){
elem.src = terrdef [mpdefCell];
返回;
}
}
elem.src = terrdef [13];
}
Try this:

function getImage(id,x,y) {
var elem = document.getElementById(id);
var mpdefColumn = mpdef[0+yy+y]; // I assume "y" means "column"
if (mpdefColumn) {
var mpdefCell = mpdefColumn[0+xx+x];
if (mpdefCell !== undefined ) {
elem.src = terrdef[mpdefCell];
return;
}
}
elem.src = terrdef[13];
}




函数getImage(id,x1,y1){

// shiftys来计算所有指南针方向面

if((ff == 0) ||(ff == 1)){x2 = x1; y2 = y1; }

if((ff == 2)||(ff == 3)){x2 = -y1; y2 = x1; }

if((ff == 4)||(ff == 5)){x2 = -x1; y2 = -y1; }

if((ff == 6)||(ff == 7)){x2 = y1; y2 = -x1; }

//检测未定义的地图位置

if((0 + yy + y2< 0)||(0 + xx + x2< 0)||( 1 + yy + y2>

mpdef.length)||(1 + xx + x2> mpdef [0] .length)){

document.getElementById( id).src = terrdef [nullterr];

}

else {

document.getElementById(id).src = terrdef [mpdef [ 0 + yy + y2] [0 + xx +

x2]];

}

}


上面是我现在使用的,它似乎工作。如果您感兴趣的话,上面的内容正在[ www.lajzar.co.uk/midnight ]。

(箭头键导航,或使用角落里的按钮)。

两个提出疑问...


1)我肯定有4个并行if语句不是特别有效,但我不确定一个更好的解决方案,正确检查所有四个
地图边缘。


2)我认为可能有一种更有效的方法来解释8

可能的面板。

-

-

Fabian

经常访问我的网站并且长期使用!
http://www.lajzar.co.uk



function getImage(id,x1,y1) {
// shiftys to account for all compass direction facings
if ((ff==0) || (ff==1)) { x2 = x1; y2 = y1; }
if ((ff==2) || (ff==3)) { x2 = -y1; y2 = x1; }
if ((ff==4) || (ff==5)) { x2 = -x1; y2 = -y1; }
if ((ff==6) || (ff==7)) { x2 = y1; y2 = -x1; }
// detect undefined map locations
if ((0 + yy + y2 < 0) || (0 + xx + x2 < 0) || (1 + yy + y2 >
mpdef.length) || (1 + xx + x2 > mpdef[0].length)) {
document.getElementById(id).src = terrdef[ nullterr ];
}
else {
document.getElementById(id).src = terrdef[ mpdef[0 + yy + y2][0 + xx +
x2] ];
}
}

The above is what I am using now, and it seems to work. In case you''re
interested, the above is being used at [ www.lajzar.co.uk/midnight ].
(arrow keys to navigate, or use the buttons in the corner).
Two points to question...

1) I''m sure having 4 parallel if statements is not particularly
efficient, but I''m not sure of a better solution that checks all four
map edges properly.

2) I think there is probably a more efficient way to account for the 8
possible facings.
--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk


这篇关于MyArray [3] [ - 2]失败(quelle surprise),怎么检测到这个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆