刚自学js在线求帮助 为什么 setInterval()之后显示 mybul is not define呢。 就是发射子弹啊

zhangqiuqiu1993 2015-08-27 12:02:05
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-CN">
<head>
<title>新建网页</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="布尔教育 http://www.itbool.com" />
</head>
<body onkeydown="do_something(event)">
<div id="game" style="background:pink;width:560px;height:560px;position:absolute;left:0px;top:0px">
<div id="tank" style="position:absolute;top:0px;left:0px;background-position:0px 0px;width:28px;height:28px;background:url('img/player1.bmp')">
</div>
</div>
</body>
<script type="text/javascript">

function getStyle(obj)
{
if(obj.currentStyle)
{
return obj.currentStyle;
}
else
{
return getComputedStyle(obj,false);
}
}

var tk=document.getElementById("tank");
function tank(x,y,direct)
{
this.t_x=x;
this.t_y=y;
this.direct=direct;



tk.style.left=this.t_x+"px";
tk.style.top=this.t_y+"px";
this.speed=14;
this.move=function move(e){
var key=e.keyCode;

switch(key){
//up 1 left 2 right 4 down 3
case 65:tk.style.backgroundPosition="0px 28px";//left
this.direct=2;
this.t_x-=this.speed;
break;
case 37:tk.style.backgroundPosition="0px 28px";
this.direct=2;
this.t_x-=this.speed;
break;
case 83:tk.style.backgroundPosition="0px 56px";//down
this.direct=3;
this.t_y+=this.speed;
break;
case 40:tk.style.backgroundPosition="0px 56px";
this.direct=3;
this.t_y+=this.speed;
break;
case 68:tk.style.backgroundPosition="0px 84px";//right
this.direct=4;
this.t_x+=this.speed;
break;
case 39:tk.style.backgroundPosition="0px 84px";
this.direct=4;
this.t_x+=this.speed;
break;
case 87:tk.style.backgroundPosition="0px 0px";//up
this.direct=1;
this.t_y-=this.speed;
break;
case 38:tk.style.backgroundPosition="0px 0px";
this.direct=1;
this.t_y-=this.speed;
break;
}

};
this.continue_move=function continue_move(){

var gm=document.getElementById("game");
var gmstyle=getStyle(gm);
var left=gmstyle.left;
left = parseInt(left.substr(0,left.length-2));
var top=gmstyle.top;
top = parseInt(top.substr(0,top.length-2));
var width=gmstyle.width;
width = parseInt(width.substr(0,width.length-2));
var height=gmstyle.height;
height = parseInt(height.substr(0,height.length-2));

var tx_max=left+width-28;
var tx_min=left;
var ty_min=top;
var ty_max=top+height-28;
if (this.t_x<=tx_max && this.t_x>=tx_min && this.t_y<=ty_max &&this.t_y>=ty_min) {

if (this.t_x<tx_max && this.t_x>tx_min &&this.t_y<=ty_max&&this.t_y>=ty_min) {
if (this.direct == 4 ) {
this.t_x+=this.speed;
} else if (this.direct==2) {
this.t_x-=this.speed;
};
}

if (this.t_y<ty_max && this.t_y>ty_min && this.t_x <=tx_max&&this.t_x>=tx_min) {
if (this.direct == 1) {
this.t_y-=this.speed;
}else if (this.direct == 3) {
this.t_y+=this.speed;
};
}
tk.style.left=this.t_x+"px";
tk.style.top=this.t_y+"px";
}
};
this.shoot= function shoot(){
var mybul=new bullet();
mybul.creat_bullet(this.t_x,this.t_y,this.direct);
setInterval("mybul.bullet_move()",200);
};
}



function bullet(){//子弹
this.speed=30;
var game=$("game");
this.b_element=document.createElement("div");


this.creat_bullet=function creat_bullet(parent_x,parent_y,parent_dict){
clearInterval(parent_y);
this.direct=parent_dict;
if (this.direct==1) {//up
this.t_x=parent_x+10;
this.t_y=parent_y-8;
this.position="0px 0px";
}else if (this.direct==2) {//left
this.t_x=parent_x-8;
this.t_y=parent_y+10;
this.position="16px 0px";
}
else if (this.direct==3) {//down
this.t_x=parent_x+10;
this.t_y=parent_y+28;
this.position="12px 0px";
}else if (this.direct==4) {//right
this.t_x=parent_x+28;
this.t_y=parent_y+10;
this.position="8px 0px";
};
this.b_element.style.backgroundImage="url(img/bullet.png)";
this.b_element.style.backgroundPosition=this.position;
this.b_element.style.position="absolute";
this.b_element.style.background="yellow";
this.b_element.style.left=this.t_x+"px";
this.b_element.style.top=this.t_y+"px";
this.b_element.style.width="8px";
this.b_element.style.height="8px";
this.b_element.id="1";
this.child =game.appendChild(this.b_element);

};

this.bullet_move=function bullet_move(){
alert(this.b_element.id);
if (this.direct==1) {//up
this.t_y-=this.speed;
}
else if (this.direct==2) {//left
this.t_x-=this.speed;
}else if (this.direct==3) {//down
this.t_y+=this.speed
}else if (this.direct==4) {//right
this.t_x+=this.speed
};
this.b_element.style.left==this.t_x+"px";
this.b_element.style.top=this.t_y+"px";
};
}



function $(id){
return document.getElementById(id);
}

function do_something(event){

if (event.keyCode==65||event.keyCode==83||event.keyCode==68||event.keyCode==87||event.keyCode==37||event.keyCode==40||event.keyCode==39||event.keyCode==38) {
mytank.move(event);//移动
}
else if (event.keyCode==32) {
alert(event.keyCode);
mytank.shoot();//发射子弹
};
}

var mytank=new tank(140,140,1);

var tt=setInterval("mytank.continue_move()",200);


</script>
</html>
...全文
244 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangqiuqiu1993 2015-08-28
  • 打赏
  • 举报
回复
<script type="text/javascript"> function getStyle(obj) { if(obj.currentStyle) { return obj.currentStyle; } else { return getComputedStyle(obj,false); } } function $(id){ return document.getElementById(id); } var game=$("game");//game var gmstyle=getStyle(game); var left=gmstyle.left; left = parseInt(left.substr(0,left.length-2)); var top=gmstyle.top; top = parseInt(top.substr(0,top.length-2)); var width=gmstyle.width; width = parseInt(width.substr(0,width.length-2)); var height=gmstyle.height; height = parseInt(height.substr(0,height.length-2)); var dog_arr=new Array(); var bul_arr=new Array();//子弹数组 function tank(x,y,direct) { this.t_x=x; this.t_y=y; this.direct=direct; var tx_max=left+width-28; var tx_min=left; var ty_min=top; var ty_max=top+height-28; this.speed=14; this.t_element=document.createElement("div"); this.tank_creat=function tank_creat(){ this.t_element.id="tank"; this.t_element.style.backgroundImage="url(img/player1.bmp)"; this.t_element.style.position="absolute"; // this.d_element.style.background="red"; this.t_element.style.left=this.d_x+"px"; this.t_element.style.top=this.d_y+"px"; this.t_element.style.width="28px"; this.t_element.style.height="28px"; this.child =game.appendChild(this.t_element); alert(this.t_x); }; this.move=function move(e){ var key=e.keyCode; switch(key){ //up 1 left 2 right 4 down 3 case 65:this.t_element.style.backgroundPosition="0px 28px";//left this.direct=2; if (this.t_x>tx_min) {this.t_x-=this.speed;}; break; case 37:this.t_element.style.backgroundPosition="0px 28px"; this.direct=2; if (this.t_x>tx_min) {this.t_x-=this.speed;}; break; case 83:this.t_element.style.backgroundPosition="0px 56px";//down this.direct=3; if (this.t_x<ty_max) {this.t_y+=this.speed;}; break; case 40:this.t_element.style.backgroundPosition="0px 56px"; this.direct=3; if (this.t_y<ty_max) {this.t_y+=this.speed;}; break; case 68:this.t_element.style.backgroundPosition="0px 84px";//right this.direct=4; if (this.t_x<tx_max) {this.t_x+=this.speed;}; break; case 39:this.t_element.style.backgroundPosition="0px 84px"; this.direct=4; if (this.t_x<tx_max) {this.t_x+=this.speed;}; break; case 87:this.t_element.style.backgroundPosition="0px 0px";//up this.direct=1; if (this.t_y>ty_min) {this.t_y-=this.speed;}; break; case 38:this.t_element.style.backgroundPosition="0px 0px"; this.direct=1; if (this.t_y>ty_min) {this.t_y-=this.speed;}; break; } }; this.continue_move=function continue_move(){ if (this.t_x<=tx_max && this.t_x>=tx_min && this.t_y<=ty_max &&this.t_y>=ty_min) { if (this.t_x<tx_max && this.t_x>tx_min &&this.t_y<=ty_max&&this.t_y>=ty_min) { if (this.direct == 4 ) { this.t_x+=this.speed; } else if (this.direct==2) { this.t_x-=this.speed; }; } if (this.t_y<ty_max && this.t_y>ty_min && this.t_x <=tx_max&&this.t_x>=tx_min) { if (this.direct == 1) { this.t_y-=this.speed; }else if (this.direct == 3) { this.t_y+=this.speed; }; } this.t_element.style.left=this.t_x+"px"; this.t_element.style.top=this.t_y+"px"; }; }; this.shoot= function shoot(){ var mybul=new bullet(); mybul.creat_bullet(this.t_x,this.t_y,this.direct); bul_arr.push(mybul); var fd = setInterval("bul_arr["+(bul_arr.length-1)+"].bullet_move()",200); bul_arr[bul_arr.length-1].fd=fd; }; } function bullet(){//子弹 this.speed=20; this.power=1;//子弹的威力1. var this_bullet=this; this.fd; this.b_element=document.createElement("div"); this.creat_bullet=function creat_bullet(parent_x,parent_y,parent_dict){ clearInterval(parent_y); this.direct=parent_dict; if (this.direct==1) {//up this.t_x=parent_x+10; this.t_y=parent_y-8; this.position="0px 0px"; }else if (this.direct==2) {//left this.t_x=parent_x-8; this.t_y=parent_y+10; this.position="24px 0px"; } else if (this.direct==3) {//down this.t_x=parent_x+10; this.t_y=parent_y+28; this.position="16px 0px"; }else if (this.direct==4) {//right this.t_x=parent_x+28; this.t_y=parent_y+10; this.position="8px 0px"; }; this.b_element.class="mybullet"; this.b_element.style.backgroundImage="url(img/bullet.bmp)"; this.b_element.style.backgroundPosition=this.position; this.b_element.style.position="absolute"; // this.b_element.style.background="yellow"; this.b_element.style.left=this.t_x+"px"; this.b_element.style.top=this.t_y+"px"; this.b_element.style.width="8px"; this.b_element.style.height="8px"; this.child =game.appendChild(this.b_element); }; this.bullet_move=function bullet_move(){ var b_x_min=left; var b_x_max=left+width-8; var b_y_min=top; var b_y_max=top+height-8; if (this.t_x>=b_x_max || this.t_x<=b_x_min || this.t_y>=b_y_max || this.t_y<=b_y_min) { game.removeChild(this.child); clearInterval(this.fd); delete this; }; if (this.direct==1) {//up this.t_y-=this.speed; } else if (this.direct==2) {//left this.t_x-=this.speed; }else if (this.direct==3) {//down this.t_y+=this.speed }else if (this.direct==4) {//right this.t_x+=this.speed }; this.b_element.style.left=this.t_x+"px"; this.b_element.style.top=this.t_y+"px"; }; } function wang_dog() { this.d_x=Math.random()*25; this.d_x=parseInt(this.d_x)*20; this.d_y=Math.random()*25; this.d_y=parseInt(this.d_y)*20; this.blood=10;//满血为10 this.num;//当前的狗狗编号 var x_speed=10; var y_speed=10; var dog_num=1;//需要创建的狗狗数 this.d_element=document.createElement("div"); this.dog_creat=function dog_creat(){ this.d_element.class="mydog"; this.d_element.style.backgroundImage="url(img/dog.gif)"; this.d_element.style.position="absolute"; // this.d_element.style.background="red"; this.d_element.style.left=this.d_x+"px"; this.d_element.style.top=this.d_y+"px"; this.d_element.style.width="50px"; this.d_element.style.height="50px"; this.child =game.appendChild(this.d_element); }; this.dog_move=function dog_move(){ this.d_x+=x_speed; this.d_y+=y_speed; if (this.d_x>=left+width-50||this.d_x<=left) { x_speed=(-x_speed); }; if (this.d_y>=top+height-50||this.d_y<=top) { y_speed=(-y_speed); }; this.d_element.style.left=this.d_x+"px"; this.d_element.style.top=this.d_y+"px"; } this.attack=function attack(){//判断狗狗是不是被打中了 for (var i = bul_arr.length - 1; i >= 0; i--) { if (bul_arr[i].t_x>=this.d_x &&bul_arr[i].t_x<=(this.d_x+50) &&bul_arr[i].t_y<=(this.d_y+50) && bul_arr[i].t_y>=this.d_y) { this.blood-=bul_arr[i].power; if (this.blood<=0) { alert("吓死宝宝了"); // for (var i = dog_arr.length - 1; i >= 0; i--) { // if(dog_arr[i]==this) // { dog_arr.splice(this.num,1); clearInterval(attack_fd[this.num]); // } // }; if (dog_arr.length==0) { dog_num++; for (var i = 0; i < dog_num; i++) { add_dog(); }; }; game.removeChild(this.child); delete this; }; }; }; }; this.enemy_down=function enemy_down()//消灭坦克 { var x=this.d_x+25; var y=this.d_y+25; if (x>=(mytank.t_x-25)&&this.d_x<=(mytank.t_x+28+25)&&this.d_y>=(mytank.t_y-25)&&this.d_y<=(mytank.t_y+28+25)) { alert("tank挂了,你输了"); clearInterval(down_fd[this.num]); clearInterval(tk_move_fd); clearInterval(attack_fd[this.num]); while(game.childNodes.length>0) { game.removeChild(game.firstChild); } if (confirm("你还玩不玩的")) { once_agian(); }; }; }; } function do_something(event){ if (event.keyCode==65||event.keyCode==83||event.keyCode==68||event.keyCode==87||event.keyCode==37||event.keyCode==40||event.keyCode==39||event.keyCode==38) { mytank.move(event);//移动 } else if (event.keyCode==32) { mytank.shoot();//发射子弹 }; } var down_fd = new Array; var attack_fd= new Array; var dog_move_fd = new Array; function add_dog(){ var w_dog=new wang_dog(); w_dog.dog_creat(); dog_arr.push(w_dog); w_dog.num=dog_arr.length-1; var m_fd=setInterval("dog_arr["+(dog_arr.length-1)+"].dog_move()",300); var at_fd=setInterval("dog_arr["+(dog_arr.length-1)+"].attack()",300); var d_fd=setInterval("dog_arr["+(dog_arr.length-1)+"].enemy_down()",100); dog_move_fd.push(m_fd) attack_fd.push(at_fd); down_fd.push(d_fd); }; function once_agian(){ add_dog(); var mytank=new tank(140,140,2); mytank.tank_creat(); tk_move_fd = setInterval("mytank.continue_move()",300); alert(getStyle($("tank")).left); } var mytank=new tank(140,140,1); mytank.tank_creat(); var tk_move_fd=setInterval("mytank.continue_move()",300); add_dog(); alert(getStyle($("tank")).left);
zhangqiuqiu1993 2015-08-27
  • 打赏
  • 举报
回复
哪位大牛 帮我看下。 我才刚学JS。 求指导~
zhangqiuqiu1993 2015-08-27
  • 打赏
  • 举报
回复
然后 为什么 我做的 子弹 创建一个 element 设置背景色 设置上了。 背景图片却没显示。
zhangqiuqiu1993 2015-08-27
  • 打赏
  • 举报
回复
引用 4 楼 jslang 的回复:
[quote=引用 2 楼 zhangqiuqiu1993 的回复:] 然后 为什么 我做的 子弹 创建一个 element 设置背景色 设置上了。 背景图片却没显示。
this.b_element.style.background 属性是可以同时设置背景图片和背景色。这就把前面设置的 backgroundImage 属性给覆盖了 要单独设置背景色应该是 this.b_element.style.backgroundColor = "yellow"; [/quote]好了哦。 谢谢。
zhangqiuqiu1993 2015-08-27
  • 打赏
  • 举报
回复
引用 4 楼 jslang 的回复:
[quote=引用 2 楼 zhangqiuqiu1993 的回复:] 然后 为什么 我做的 子弹 创建一个 element 设置背景色 设置上了。 背景图片却没显示。
this.b_element.style.background 属性是可以同时设置背景图片和背景色。这就把前面设置的 backgroundImage 属性给覆盖了 要单独设置背景色应该是 this.b_element.style.backgroundColor = "yellow"; [/quote] 单独设置的时候 图片也 没出来啊。 就是没出来才设置背景色的
天际的海浪 2015-08-27
  • 打赏
  • 举报
回复
引用 2 楼 zhangqiuqiu1993 的回复:
然后 为什么 我做的 子弹 创建一个 element 设置背景色 设置上了。 背景图片却没显示。
this.b_element.style.background 属性是可以同时设置背景图片和背景色。这就把前面设置的 backgroundImage 属性给覆盖了 要单独设置背景色应该是 this.b_element.style.backgroundColor = "yellow";
天际的海浪 2015-08-27
  • 打赏
  • 举报
回复

	this.shoot = function shoot() {
		var mybul = new bullet();
		mybul.creat_bullet(this.t_x, this.t_y, this.direct);
		setInterval(function() {
			mybul.bullet_move();
		}, 200);
	};

87,964

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧