制作(蹩脚)游戏,我想检查所选方块是否包含怪物,但是我的 indexOf 一直返回-1。下面的相关代码。
初始化:
var monsters = [];
var monsterPositions = [2,8,13,15,22];
var player = { "currentPosition": 0 };
for ( var i in monsterPositions ) {
monsters[i] = new createMonster("monster",monsterPositions[i],1); }
function createMonster(name,startingPoint,level) {
this.currentPosition = startingPoint; }
<td>
使用onclick触发的功能:
function processClick(trigger){
console.log( trigger.id + " " + monsterPositions );
if ( monsterPositions.indexOf(trigger.id) >= 0 ){
if ( !fight( findMonster( parseInt( trigger.id ) ) ) ){
return;
}
}
if ( gameOn ) move( parseInt( trigger.id ) );
}
页面总是直奔move()
,即使它是一个怪物点,console.log()
结果如下:
1 2,8,13,15,22 adventure.js (line 79)
2 2,8,13,15,22 adventure.js (line 79)
7 2,8,13,15,22 adventure.js (line 79)
8 2,8,13,15,22 adventure.js (line 79)
13 2,8,13,15,22 adventure.js (line 79)
18 2,8,13,15,22 adventure.js (line 79)
23 2,8,13,15,22 adventure.js (line 79)
22 2,8,13,15,22 adventure.js (line 79)
21 2,8,13,15,22 adventure.js (line 79)
20 2,8,13,15,22 adventure.js (line 79)
15 2,8,13,15,22 adventure.js (line 79)
(如果上面的代码不够用,请在此处链接),请帮助我。(当然我为什么会在这里)