如何使对象滚动HTML5画布?

时间:2016-05-21 19:47:14

标签: javascript html5 canvas scroll html5-canvas

我一直试图让对象在画布上滚动,就像它下降时一样,它会从顶部出来。我已经让它适用于底部和右侧,但它只是卡在左侧和顶部。 这是代码在行动中的样子 http://output.jsbin.com/gavuqo/

这是我的javascript代码:

var count = 1;
var myGamePiece;
var myObstacle;
var myHealth;
var myScore;

function startGame() {
    myGamePiece = new component(40, 40, "http://www.link", 10, 120, "image");
    myObstacle = new component(60, 60, "http://www.link", 300, 120, "image"); 
    myHealth = new component(60, 60, "http://www.link", 300, 100, "image"); 

    myGameArea.start();
}

var myGameArea = {
    canvas : document.createElement("canvas"),
    start : function() {
        this.canvas.width = 600;
        this.canvas.height = 400;
        this.context = this.canvas.getContext("2d");
        document.body.insertBefore(this.canvas, document.body.childNodes[0]);
        this.frameNo = 0;
        this.interval = setInterval(updateGameArea, 20);
        },
    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.angle = Math.floor(Math.random() * 360) + 0  ;
    this.speed = 1;
    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() {
if( this.x<=0 || this.x>=600){this.x = -this.speed * Math.sin(this.angle);}else{    this.x += this.speed * Math.sin(this.angle);
}
        if( this.y<=0 || this.y>=400) {this.y = -this.speed * Math.cos(this.angle);}else{
        this.y += this.speed * Math.cos(this.angle);}    


    }



                //myObstacle.newPos();



    this.newPosmain = 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(myObstacle) && count==1) {
            myObstacle = new component(60, 60, "http://www.link", 300, 120, "image"); 
            count++;
            myGamePiece = new component(80, 80, "http://www.link", 10, 120, "image");



    } else {
        myGameArea.clear();
        myObstacle.update();
        //myObstacle.wallBounce();
        myObstacle.newPos();
        myHealth.update();
        myHealth.newPos();
        //myHealth.wallBounce();
        myGamePiece.newPosmain(); 
        myGamePiece.update();
    }
    if(myGamePiece.crashWith(myObstacle) && count==2){
        myObstacle = new component(60, 60, "http://www.link", 300, 120, "image"); 
                myGamePiece = new component(100, 100, "http://www.link", 10, 120, "image");

                count++;

}
if(myGamePiece.crashWith(myObstacle) && count==3){

         myGamePiece = new component(500, 500, "#", 0, 0, "image");

    myeGameArea.stop();


    document.body.style.backgroundImage = "url('#')";
      count=0;              

}

if(myGamePiece.crashWith(myHealth) && count==1){
                myGamePiece = new component(100, 100, "http://www.link", 10, 120, "image");
                count=0;
             count--;
}
if(myGamePiece.crashWith(myHealth) && count==2){
    myGamePiece = new component(40, 40, "http://www.link", 10, 120, "image");            
                count--;
}
if(myGamePiece.crashWith(myObstacle) && count==0){
            myObstacle = new component(60, 60, "http://www.link", 300, 120, "image"); 
                myGamePiece = new component(100, 100, "http://www.link", 10, 120, "image");

                count++;
}
if(myGamePiece.crashWith(myHealth) && count==0){
                myGamePiece = new component(500, 500, "http://i.imgur.com/m7dlzy7.png", 0, 0, "image");

}
}
function moveup() {
    myGamePiece.speedY = -2; 
}

function movedown() {
    myGamePiece.speedY = 2; 
}

function moveleft() {
    myGamePiece.speedX = -2; 
}

function moveright() {
    myGamePiece.speedX = 2; 
}

function clearmove() {
    myGamePiece.speedX = 0; 
    myGamePiece.speedY = 0; 
}

1 个答案:

答案 0 :(得分:0)

只需将x或y位置调整为与出口点相反即可。如果它从底部退出,则将其重置为顶部减去对象的直径而不调整x,依此类推。

在通用代码中:

// dia = diameter of object (you may have to use different values for width/height)

if (x < -dia) x = width;
else if (x >= width) x = -dia;

if (y < -dia) y = height;
else if (y >= height) y = -dia;

实施例

(再次点按以更改方向)

var ctx = c.getContext("2d"),
    x = c.width * 0.5, y = c.height * 0.5,
    dx = Math.max(2, Math.random() * 4),
    dy = Math.max(2, Math.random() * 4),
    dia = 10;

dx *= Math.random() < 0.5 ? -1 : 1;
dy *= Math.random() < 0.5 ? -1 : 1;

(function loop() {
  ctx.clearRect(0,0,c.width,c.height);
  ctx.fillRect(x, y, dia, dia);
  
  x += dx;
  y += dy;
  
  if (x < -dia) x = c.width;
  else if (x >= c.width) x = -dia;

  if (y < -dia) y = c.height;
  else if (y >= c.height) y = -dia;
  
  requestAnimationFrame(loop)
})()
#c {border:1px solid #999}
<canvas id=c>