HTML5 Canvas将对象移动到另一个

时间:2016-04-27 14:33:32

标签: javascript html5 canvas

所以这是我的问题。

我正在制作一个HTML5画布游戏,我希望一个敌人(pinguin)移动到玩家(一条鱼)。我到处搜索,但我找不到解决方案。这是我的代码:

使用Javascript:

var myGamePiece;
var Pinguin1;
var Pinguin2;
var Pinguin3;

function startGame(character) {
    document.getElementById("CharacterSelect").innerHTML = "";
    console.log("Making canvas...");
    myGameArea.start();
    console.log("Inserting game pieces...");
    myGamePiece = new component(45, 30.75, character, 290, 290, "image");
    Pinguin1 = new component(50, 66.5, "img/pinguin1.png", 50, 200, "image");
    Pinguin2 = new component(50, 66.5, "img/pinguin2.png", 10, 305, "image");
    Pinguin3 = new component(50, 87.5833333333333, "img/pinguin3.png", 5, 5, "image");
    console.log("Starting game...");
}



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

function component(width, height, color, x, y, type) {
    this.type = type;
    if (type == "image") {
        this.image = new Image();
        this.image.src = color;
    }
    this.width = width;
    this.height = height;
    this.speedX = 0;
    this.speedY = 0;    
    this.x = x;
    this.y = y;    
    this.update = function() {
        ctx = myGameArea.context;
        if (type == "image") {
            ctx.drawImage(this.image, 
                this.x, 
                this.y,
                this.width, this.height);
        } else {
            ctx.fillStyle = color;
            ctx.fillRect(this.x, this.y, this.width, this.height);
        }
    }
    this.newPos = function() {
        this.x += this.speedX;
        this.y += this.speedY;        
    }
    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;
    }
}

function updateGameArea() {
    if (myGamePiece.crashWith(Pinguin1) || myGamePiece.crashWith(Pinguin2) || myGamePiece.crashWith(Pinguin3)) {
        myGameArea.stop();
        document.getElementsByTagName("canvas").remove();
        document.getElementById("CharacterSelect").innerHTML = "Game Over!<br><button onclick='location.reload()'>Try again</button>";
    } else {
    myGameArea.clear();
    myGamePiece.speedX = 0;
    myGamePiece.speedY = 0;    
    if (myGameArea.keys && myGameArea.keys[37]) {myGamePiece.speedX = -5; }
    if (myGameArea.keys && myGameArea.keys[39]) {myGamePiece.speedX = 5; }
    if (myGameArea.keys && myGameArea.keys[38]) {myGamePiece.speedY = -5; }
    if (myGameArea.keys && myGameArea.keys[40]) {myGamePiece.speedY = 5; }
    myGamePiece.newPos();
    if(myGamePiece.x >= 580 || myGamePiece.y >= 580 || myGamePiece.x < 0 || myGamePiece.y < 0){
        myGamePiece.x = 290;
        myGamePiece.y = 290;
    }
    myGamePiece.update();
    Pinguin1.update();
    Pinguin2.update();
    Pinguin3.update();
    }
}

//Remove function
Element.prototype.remove = function() {
    this.parentElement.removeChild(this);
}
NodeList.prototype.remove = HTMLCollection.prototype.remove = function() {
    for(var i = this.length - 1; i >= 0; i--) {
        if(this[i] && this[i].parentElement) {
            this[i].parentElement.removeChild(this[i]);
        }
    }
}

请注意:以上大部分代码均来自w3schools

HTML:

<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Bluppie on Canvas</title>
        <script src="script.js"></script>
        <link rel="stylesheet" href="style.css" type="text/css">
    </head>

    <body>
        <h1>Bluppie the Fish</h1>
        <p id="CharacterSelect">
            Please select a character:<br>
            <img src="img/fish.png" alt="Bluppie the Fish" title="Bluppie the Fish" onclick="startGame(this.src);">
            <img src="img/fish2.png" alt="Mrs. Fish" title="Mrs. Fish" onclick="startGame(this.src);">
            <img src="img/fish3.png" alt="Fat Fish" title="Fat Fish" onclick="startGame(this.src);">
        </p>
    </body>
</html>

CSS:

canvas {
    border: 1px solid #d3d3d3;
    background-color: #f1f1f1;
}

如果有人能回答这个问题,我会很高兴。这花了我很多时间,我找不到任何答案。而且:抱歉我的英语不好。

编辑:markE,你知道Mario&amp; Luigi及时合作?如果你知道:非常感谢你!

否则:谢谢。 :)

0 个答案:

没有答案