删除数组元素时出错

时间:2016-07-05 00:37:09

标签: javascript arrays

当我使用下面的代码删除数组元素时,它会删除该数组中的所有元素。我很奇怪我做错了什么?如果这是一个愚蠢的错误,请不要讨厌,因为我是一个新的编码员。

//key array
keys: [{
  x: 300,
  y: 300,
  w: 20,
  h: 20
}, {
  x: 300,
  y: 230,
  w: 20,
  h: 20
}]

//delete key if collision is detected
for (i = 0; i < level.keys.length; i++) {
  if (collectKey(p, level.keys[i])) {
    if (level.keys.length > -1) {
      level.keys.splice(0)
      console.log("Collected Key")
      console.log(level.keys.length + " keys left")
    }
  }
}


// detect collision
if (level.keys.length > -1) {
  function collectKey(e1, e3) {
    var e2 = {
      x: e3.x + 20,
      y: e3.y + 20,
      w: e3.w + 15,
      h: e3.h + 15
    }
    if (e1.x < (e2.x - x + e2.w) && (e1.x + e1.w) > e2.x - x && e1.y - vy < (e2.y - y + e2.h) && (e1.y - vy + e1.h) > e2.y - y) {
      return true;
    }
    return false;
  }
} else {
  //openDoor();
}

此代码将检测我是否在密钥内,如果是,则会删除该密钥。

有人想要所有的代码

<!DOCTYPE html>
<html>
	<head>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
	<script type="text/javascript">


		var game = {
			levels:[
				{
					name:"Level 1",
					tiles:[

						//List background tiles
						{x:300,y:300,w:600,h:600,solid:false,color:"#EEEEEE"},

						//List solid forground tiles
						{x:0,y:400,w:500,h:500,solid:true,color:"#CECECE"},
						{x:300,y:300,w:600,h:1,solid:true,color:"#CECECE"},
					],
					entities:[
						{}
					],
					keys:[
						{x:300,y:300,w:20,h:20},
						{x:300,y:230,w:20,h:20}
					]
				},
				{
					name:"Level 2",
					tiles:[
						{}
					],
					entities:[
						{}
					]
				}
			]
		}
	</script>
	</head>
	<style>
		body{
			text-align: center;
		}

		#game{
			background-color: white; 
			border: 1px solid black;
			display: inline;
		}
	</style>

	<h1 style="color:#123456;">
		The Game
	</h1>
	<br>
	<body style="background-color:lightgrey;">
	<canvas text-align="center" id="game" width=500px height=500px> 
	</canvas>
	</body>
	<br>
	<h5>
		This is a game I created. Learned most of the code from Accidental games on youtube.
	</h5>
	<script type="text/javascript">


		g = document.getElementById("game");
		g = g.getContext("2d")
		console.log("loaded")
		var MAXSPEED = 5;
		var MAXTERMINALVEL = 8;
		var GRAVITY = 1;
		var x = 0;
		var y = 0;
		var vx = 0;
		var vy = 0;
		var levelNo = 0;
		var level = game.levels[levelNo]
		var canFall = true
		var keymap = []
		var p = {x:240,y:240,w:20,h:20}
		console.log(level.tiles[0].color);
		var map = {65: false, 32: false, 68: false}
		$("body").bind('keydown',function(e){
			if(e.keyCode in map){
				map[e.keyCode] = true
				if(map[68] && map[69] && map[32]){

				}
			}

		}).keyup(function(e){
			if(e.keyCode in map){
				map[e.keyCode] = false;
			}
		});

		var main = setInterval(function(){
			document.getElementById("game").height += 0;
			if(levelNo > -1){
				if(map[65]){
					if(vx<MAXSPEED){
						vx++
					}
				}
				if(map[68]){
					if(vx>-MAXSPEED){
						vx--
					}
				}
				if(map[32] && canFall==false){
					vy+=20
				}


				if(vx<0){
					vx+=0.25
				}

				if(vx>0){
					vx-=0.25
				}
				canFall = true;
				for(i=0;i<level.tiles.length;i++){
					if(isTouchingFloor(p,level.tiles[i])){
						vy = 0;
						canFall = false;
					}
					if(isTouchingWall(p,level.tiles[i])){
						vx=0
					}
				}

				for(i=0;i<level.keys.length;i++){
					if(level.keys.filter(collectKey)
				}

				if(canFall && vy>-MAXTERMINALVEL){
					vy-=GRAVITY
				}

				y-=vy
				x-=vx



				for(i=0;i<level.tiles.length;i++){
					g.fillStyle = level.tiles[i].color;
					g.fillRect(level.tiles[i].x-x,level.tiles[i].y-y,level.tiles[i].w,level.tiles[i].h);
				}

				for(i=0;i<level.keys.length;i++){
					g.fillStyle = "#ff9900"
					g.fillRect(level.keys[i].x-x+20,level.keys[i].y-y+20,40,40)
				}

				g.fillStyle = "#000000"
				g.fillRect(240,240,20,20);
				g.fillStyle = "#404040"
				g.font = "20px Arial";
				g.fillText("X: "+x,10,25);
				g.fillText("Y: "+y,10,55);

				if(y>600){
					y=0
					x=0
				}
			}



		},17)

			function collectKey(e1, e3){
					var e2 = {x:e3.x + 20,y:e3.y + 20,w:e3.w + 15,h:e3.h + 15}
						if(!(e1.x < (e2.x-x + e2.w) && (e1.x + e1.w) > e2.x-x && e1.y-vy < (e2.y-y + e2.h) && (e1.y-vy + e1.h) > e2.y-y)){
							return true;
						}
					return false;
				}


		function isTouchingFloor(e1, e2){
			if(e2.solid == true){
				if(e1.x < (e2.x-x + e2.w) && (e1.x + e1.w) > e2.x-x && e1.y-vy < (e2.y-y + e2.h) && (e1.y-vy + e1.h) > e2.y-y){
					return true;
				}
				return false;
			}		
			return false;
		}
		function isTouchingWall(e1, e2){
			if(e2.solid == true){
				if(e1.x-(vx) < (e2.x-x + e2.w) && (e1.x-(vx) + e1.w) > e2.x-x && (e1.y-1) < (e2.y-y + e2.h) && ((e1.y-1) + e1.h) > e2.y-y){
					return true
				}
				return false
			}
			return false
		}
	</script>
</html>

0 个答案:

没有答案