刚自学js在线求帮助 为什么 setInterval()之后显示 mybul is not define呢。 就是发射子弹啊
<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>
相关推荐
基于线性车辆三自由度动力学+无迹卡尔曼滤波UKF估计模型(横摆角速度+质心侧偏角+车速)联合仿真
内容概要:通过构建基于线性车辆三自由度动力学模型与无迹卡尔曼滤波(UKF)的联合仿真系统,实现对车辆关键状态参数的高精度估计,重点包括横摆角速度、质心侧偏角和车速。该方法利用车辆动力学模型描述其横向、纵向及横摆运动行为,结合UKF这一适用于非线性系统的先进状态估计算法,有效处理传感器噪声和模型不确定性,从而在缺乏直接测量手段的情况下,实现对难以获取状态量的可靠估计。整个仿真框架通常在MATLAB/Simulink环境中搭建,能够模拟真实驾驶工况,验证算法的有效性和鲁棒性,为车辆稳定性控制、自动驾驶感知与决策等高级功能提供重要的状态信息支撑。; 适合人群:具备车辆动力学、现代控制理论和MATLAB/Simulink仿真基础,从事汽车工程、自动驾驶或状态估计相关研究的研发人员与研究生。; 使用场景及目标:① 深入理解并掌握非线性系统状态估计的核心方法,特别是UKF的原理与实现;② 学习如何将理论化的车辆动力学模型转化为可用于仿真的模块;③ 实践并验证车辆关键状态(如质心侧偏角)的间接估计技术,服务于车辆动态控制系统的设计与测试。; 阅读建议:此资源以开发仿真模型为核心,不仅关注代码和模型的实现,更强调对车辆动力学机理和滤波算法设计思路的理解。学习者应结合理论知识,动手搭建并调试仿真模型,通过改变工况和参数来观察估计效果,从而深刻领会系统辨识与状态估计在实际工程中的应用价值。
基于PID控制的ABS汽车防抱死制动系统、车轮滑移率+最优滑移率Carsim和Simulink联合仿真(带参考文献)
内容概要:本文档主要围绕基于PID控制的汽车防抱死制动系统(ABS)开展研究,重点聚焦于车轮滑移率与最优滑移率的精确控制。通过Carsim与Simulink联合仿真平台,构建整车动力学模型与ABS控制器,利用PID算法实时调节制动压力,使实际滑移率逼近理论最优值,从而最大化轮胎与路面间的纵向附着力并保障横向稳定性,有效提升紧急制动工况下的制动效能与操控安全性。研究涵盖了从车辆建模、滑移率特性分析、PID控制器设计到联合仿真验证的全过程,并提供了完整的仿真模型、MATLAB/Simulink代码及参考文献,为相关领域的科研与工程实践提供了详实的技术参考。; 适合人群:具备车辆工程、自动化或控制理论基础知识,从事汽车电控系统开发、自动驾驶或车辆动力学仿真的研究生、工程师及科研人员。; 使用场景及目标:① 实现ABS系统的核心控制逻辑,掌握滑移率控制的关键技术;② 学习Carsim与Simulink联合仿真的搭建方法,进行车辆动力学与控制算法的协同仿真;③ 基于PID控制策略优化制动性能,完成相关学术研究或工程项目。; 阅读建议:在学习过程中,应结合所提供的完整仿真模型和代码,深入理解车辆动力学与ABS控制的交互机制,重点分析不同路面附着系数下最优滑移率的设定及PID参数的整定过程,以达到最佳的仿真效果和理论认知。
R语言SCINature绘图模板SCI科研绘图-雷达图
R语言SCINature绘图模板SCI科研绘图--雷达图
JESD82-543 DDR5MRCD03 Multiplexed Rank Registering Clock Driver.rar
JESD82-543 DDR5MRCD03 Multiplexed Rank Registering Clock Driver.rar
【多机器人路径规划】基于动态边权膨胀与节点预留历史机制的多智能体无冲突轨迹生成方法(Matlab代码实现)
内容概要:本文提出了一种基于动态边权膨胀与节点预留历史机制的多智能体无冲突轨迹生成方法,旨在高效解决多机器人路径规划中的碰撞与死锁问题。该方法通过动态调整路径图中边的权重以反映局部拥堵状况,实现路径的实时优化;同时引入节点预留机制,记录各机器人对路径节点的占用历史,确保时空冲突的有效规避。算法融合了路径搜索、冲突检测、权重自适应更新与调度协调等核心模块,形成了完整的无冲突轨迹规划框架,并配套提供了Matlab代码实现,便于算法验证与性能分析。该方法在复杂动态环境中表现出良好的鲁棒性与可扩展性,适用于多机器人系统的协同运动控制。; 适合人群:具备一定编程基础和路径规划基础知识,从事自动化、机器人、智能交通、智能制造等相关领域的科研人员、工程技术人员及研究生。; 使用场景及目标:①应用于仓储物流、智能工厂、无人驾驶车队等多智能体协同作业场景中的路径规划与动态避障;②为解决多机器人系统中的路径冲突、通信协调与死锁预防等关键技术难题提供理论支持与算法工具;③通过Matlab仿真平台复现算法逻辑,进行不同规模与场景下的性能测试、对比分析与算法优化。; 阅读建议:建议读者结合提供的Matlab代码逐模块实现与调试,重点理解动态边权调整策略的设计原理与节点预留机制的实现细节,通过设置不同初始条件与障碍环境开展仿真实验,以全面掌握算法的运行机制与优化潜力。


