遇到障碍,我如何添加事件重新开始

时间:2019-02-20 14:39:38

标签: javascript html

我目前正在开发一款小型游戏,当我遇到障碍时,我正在尝试实现“游戏结束”功能。但是每次我添加所有代码以实现该功能时,它都会破坏一切。如果有人可以帮助我,我将不胜感激。我不确定是什么问题。

这是完整的代码(仅js部分):

var myGamePiece;
var myObstacle;

function startGame() {
    myGamePiece = new component(30, 30, "red", 225, 225);
    myObstacle = new component(40, 40, "green", 300, 120);
    myGameArea.start();
}

var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 1000;
        this.canvas.height = 890;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.frameNo = 0;
        this.interval = setInterval(updateGameArea, 20);
        window.addEventListener('keydown', function (e) {
            e.preventDefault();
            myGameArea.keys = (myGameArea.keys || []);
            myGameArea.keys[e.keyCode] = (e.type == "keydown");
        })
        window.addEventListener('keyup', function (e) {
            myGameArea.keys[e.keyCode] = (e.type == "keydown");
        })
    },
    stop : function() {
        clearInterval(this.interval);
    },    
    clear : function() {
        this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
    }
}

function component(width, height, color, x, y, type) {

    this.type = type;
    this.width = width;
    this.height = height;
    this.speed = 0;
    this.angle = 0;
    this.moveAngle = 0;
    this.x = x;
    this.y = y;    
    this.update = function() {
        ctx = myGameArea.context;
        ctx.save();
        ctx.translate(this.x, this.y);
        ctx.rotate(this.angle);
        ctx.fillStyle = color;
        ctx.fillRect(this.width / -2, this.height / -2, this.width, this.height);
        ctx.restore();    
    }
    this.newPos = function() {
        this.angle += this.moveAngle * Math.PI / 180;
        this.x += this.speed * Math.sin(this.angle);
        this.y -= this.speed * Math.cos(this.angle);
    }

}

function updateGameArea() {

    myGameArea.clear();
    myGamePiece.moveAngle = 0;
    myGamePiece.speed = 0;
    myObstacle.update();
    if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.moveAngle = -2; }
    if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.moveAngle = 2; }
    if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speed= 2; }
    if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speed= -2; }
    myGamePiece.newPos();
    myGamePiece.update();
}

startGame();

2 个答案:

答案 0 :(得分:0)

尝试解决此问题: 您在第14和17行缺少分号 让我们看看...

答案 1 :(得分:0)

您要的是所谓的2D碰撞检测。如果要使用正方形,这很简单。基本上,将两个正方形与以下内容进行比较:

if (rect1.x < rect2.x + rect2.width &&
   rect1.x + rect1.width > rect2.x &&
   rect1.y < rect2.y + rect2.height &&
   rect1.y + rect1.height > rect2.y) {
    // collision detected!
}

来自:https://developer.mozilla.org/en-US/docs/Games/Techniques/2D_collision_detection

此代码仅适用于没有任何旋转的正方形。对于旋转,您将不得不根据旋转角度来分解sin / cos / tan的三角函数。

二维碰撞有多种解决方案。一种基于亲自使用此方法进行地理围栏的方法,是获取对象1的每个点,并将其与对象2的每一侧进行比较。如果在一个点的一个方向上有奇数个边(例如,与对),然后使其位于对象内部。您需要为每个点和return true进行迭代,如果有返回值是奇数。然后,您应使用对象2点重复过程逆过程。

这称为多边形点方法:

https://en.wikipedia.org/wiki/Point_in_polygon

这适用于多边形形状,但是一旦使用曲线,它就会变得更难。但是,一般来说,由于简单性,游戏使用“命中盒”。

编辑:需要注意的是,如果命中框的大小相同,则此多边形输入点可以工作,但是如果您可以使两个矩形相互重叠,则必须将Object1的每一侧与侧面的交点进行比较在Object2中。