Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 墙物理和地面物理_Javascript_Game Physics - Fatal编程技术网

Javascript 墙物理和地面物理

Javascript 墙物理和地面物理,javascript,game-physics,Javascript,Game Physics,在元素或游戏块与墙碰撞后,它停止移动。如何重置碰撞检测,使游戏块与墙碰撞后仍能移动?同样,在游戏碎片与地面碰撞后,它停止移动。那么,如何重置碰撞检测,使其在与墙碰撞后可以移动呢 var myGamePiece; var myObstacle; var e = e || window.event; var speed = 3; var gravity = 0.05; function startGame() { myGamePiece = new component(30, 30, "red",

在元素或游戏块与墙碰撞后,它停止移动。如何重置碰撞检测,使游戏块与墙碰撞后仍能移动?同样,在游戏碎片与地面碰撞后,它停止移动。那么,如何重置碰撞检测,使其在与墙碰撞后可以移动呢

var myGamePiece;
var myObstacle;
var e = e || window.event;
var speed = 3;
var gravity = 0.05;
function startGame() {
 myGamePiece = new component(30, 30, "red", 10, 120);
 myObstacle = new component(10, 200, "green", 300, 120);
 myGameArea.start();
 myGameArea.gravityPhysics();
}

var myGameArea = {
 canvas: document.createElement("canvas"),
  start: function() {
   this.canvas.width = 480;
   this.canvas.height = 270;
   this.context = this.canvas.getContext("2d");
   document.body.insertBefore(this.canvas, document.body.childNodes[0]);
   this.interval = setInterval(updateGameArea, 20);
 },
 clear: function() {
  this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
 },
  gravityPhysics: function() {
   myGamePiece.speedY = -speed;
 },
 stop: function() {
   clearInterval(this.interval);
 }
};

 function component(width, height, color, x, y) {
  this.width = width;
  this.height = height;
  this.speedX = 0;
  this.speedY = 0;
  this.x = x;
  this.y = y;
  this.update = function() {
    ctx = myGameArea.context;
    ctx.fillStyle = color;
    ctx.fillRect(this.x, this.y, this.width, this.height);
    this.hitBottom();
  };
  this.crashWith = function(otherobj) {
    var myleft = this.x;
    var myright = this.x + this.width;
    var mytop = this.y;
    var mybottom = this.y + this.height;
    var otherleft = otherobj.x;
    var otherright = otherobj.x + otherobj.width;
    var othertop = otherobj.y;
    var otherbottom = otherobj.y + otherobj.height;
    var crash = true;

    if (
      mybottom < othertop ||
      mytop > otherbottom ||
      myright < otherleft ||
      myleft > otherright
    ) {
      crash = false;
    }

    return crash;
  };
  this.hitBottom = function() {
    var rockbottom = myGameArea.canvas.height - this.height;

    if (this.y > rockbottom) {
      this.y = rockbottom;

      if (
        myGameArea.canvas.height - this.height &&
        myGamePiece.key == 38
      ) {
        this.y = this.speedY;
      }

      this.gravitySpeed = 0; // reset?
    }
  };
}

function updateGameArea() {
  if (myGamePiece.crashWith(myObstacle)) {
    console.log("crash");
  } else {
    myGameArea.clear();
    myObstacle.update();
    myGamePiece.x += myGamePiece.speedX;
    myGamePiece.y -= myGamePiece.speedY;
    myGamePiece.update();
  }
   if (
    myGameArea.gravityPhysics() &&
    e.keyCode == document.addEventListener("keydown")
  ) {
   myGamePiece.speedY = gravity - 10;
    e.keyCode = true;
  }
}
document.onkeydown = checkKeyD;
document.onkeyup = gravityPhysics;

function checkKeyD(e) {
  if (e.keyCode == "38") {
    // up arrow
    myGamePiece.speedY = speed;
  } else if (e.keyCode == "40") {
    // down arrow
    myGamePiece.speedY = -speed;
  } else if (e.keyCode == "37") {
    // left arrow
    myGamePiece.speedX = -speed;
  } else if (e.keyCode == "39") {
    // right arrow
    myGamePiece.speedX = speed;
  }
}
if ((e = e || (window.event && window.event == false))) {
  myGamePiece.speedY -= gravity;
}
function clearmove() {
  myGamePiece.speedX = 0;
  myGamePiece.speedY = 0;
}
var-myGamePiece;
肌障;
var e=e | | window.event;
无功转速=3;
var重力=0.05;
函数startName(){
myGamePiece=新组件(30,30,“红色”,10,120);
myObstacle=新组件(10200,“绿色”,300120);
myGameArea.start();
myGameArea.gravityPhysics();
}
var myGameArea={
画布:document.createElement(“画布”),
开始:函数(){
this.canvas.width=480;
this.canvas.height=270;
this.context=this.canvas.getContext(“2d”);
document.body.insertBefore(this.canvas,document.body.childNodes[0]);
this.interval=setInterval(updateGameArea,20);
},
清除:函数(){
this.context.clearRect(0,0,this.canvas.width,this.canvas.height);
},
重力物理学:函数(){
myGamePiece.speedY=-速度;
},
停止:函数(){
clearInterval(这个.interval);
}
};
功能组件(宽度、高度、颜色、x、y){
这个。宽度=宽度;
高度=高度;
这是0.speedX=0;
该值为0;
这个.x=x;
这个。y=y;
this.update=函数(){
ctx=myGameArea.context;
ctx.fillStyle=颜色;
ctx.fillRect(this.x,this.y,this.width,this.height);
这个;
};
this.crashWith=函数(otherobj){
var myleft=this.x;
var myright=this.x+this.width;
var mytop=this.y;
var mybottom=this.y+this.height;
var otherleft=otherobj.x;
var otherright=otherobj.x+otherobj.width;
var othertop=otherobj.y;
var otherbottom=otherobj.y+otherobj.height;
var-crash=true;
如果(
mybottomotherbottom||
米右<左||
myleft>otherright
) {
崩溃=错误;
}
返回碰撞;
};
this.hitbooth=函数(){
var rockbottom=myGameArea.canvas.height—this.height;
如果(this.y>rockbottom){
y=最低点;
如果(
myGameArea.canvas.height-this.height&&
myGamePiece.key==38
) {
this.y=this.speedY;
}
this.gravitySpeed=0;//是否重置?
}
};
}
函数updateGameArea(){
如果(我的游戏片.撞击物(我的障碍物)){
控制台日志(“崩溃”);
}否则{
myGameArea.clear();
myObstacle.update();
myGamePiece.x+=myGamePiece.speedX;
myGamePiece.y-=myGamePiece.speedY;
myGamePiece.update();
}
如果(
myGameArea.gravityPhysics()&&
e、 keyCode==document.addEventListener(“keydown”)
) {
myGamePiece.speedY=重力-10;
e、 keyCode=true;
}
}
document.onkeydown=checkKeyD;
document.onkeyup=重力物理学;
函数checkKeyD(e){
如果(如键码==“38”){
//向上箭头
myGamePiece.speedY=速度;
}否则,如果(例如,keyCode==“40”){
//向下箭头
myGamePiece.speedY=-速度;
}否则,如果(例如,keyCode==“37”){
//左箭头
myGamePiece.speedX=-速度;
}否则,如果(例如,keyCode==“39”){
//右箭头
myGamePiece.speedX=速度;
}
}
如果((e=e | |(window.event&&window.event==false))){
myGamePiece.speedY-=重力;
}
函数clearmove(){
myGamePiece.speedX=0;
myGamePiece.speedY=0;
}