【问题标题】:Move Background With Arrow Key Press使用箭头键移动背景
【发布时间】:2014-12-04 20:27:36
【问题描述】:

如果按向上和箭头键,我想加快整个背景的速度。

我试过了,但是没用。

this.speed是后台控制速度。

所以我放了一个 if 语句,如果按下向上箭头键,则将速度设置为 10。(您可以查看下面的代码)

这是我的代码:

//Setting the canvas and context
var canvas = document.getElementById('background');
var context = canvas.getContext('2d');



//===========================
//ENTER: MOVING BACKGROUND
//===========================

//Creating one abstract object to hold all images
var imageRepository = new function() {
    //Upload background image
    this.background = new Image();
    this.background.src = "http://fc07.deviantart.net/fs70/f/2013/174/3/e/recycled_texture_background_by_sandeep_m-d6aeau9.jpg";
};

//Abstract function that will hold most all other properties
function Drawable() {
    this.init = function(x, y) {
        // Default variables
        this.x = x;
        this.y = y;
    };
    this.speed = 0;
    this.canvasWidth = 0;
    this.canvasHeight = 0;

    this.draw = function() {

    this.keys = keypress_handler();
    };
}

//Creating the background image and drawing it
function Background() {

    this.speed = 1; // Resetting speed of background for animation (positive so top to bottom motion)
    this.draw = function() {
        //Setting velocity to y-component, since track needs to go from top to bottom
        this.y += this.speed;
        this.context.drawImage(imageRepository.background, this.x, this.y);
        // Draw it again for animation, top edge of the first background
        this.context.drawImage(imageRepository.background, this.x, this.y - this.canvasHeight);
        // If one background ends, reset
        if (this.y > this.canvasHeight)
            this.y = 0;

                 };
    }


// Make background have properties from Drawable function
Background.prototype = new Drawable();

//Makes object to hold everything else the game will have
function Game() {
    this.init = function() {
        // Gets canvas element
        this.bgCanvas = document.getElementById('background');
        // Sees if canvas is supported by the browser
        if (this.bgCanvas.getContext) {
            this.bgContext = this.bgCanvas.getContext('2d');
            // Initialize objects to contain their context and canvas
            Background.prototype.context = this.bgContext;
            Background.prototype.canvasWidth = this.bgCanvas.width;
            Background.prototype.canvasHeight = this.bgCanvas.height;
            // Initialize the background image
            this.background = new Background();
            this.background.init(0,0); // Set draw point to 0,0
            return true;

        } else {
            return false;



    }
    };
    // Start the animation loop for the background
    this.start = function() {
        animate();
    };
}

//Requests animation frame
function animate() {
    requestAnimFrame( animate );
    game.background.draw();
}

//Setting all animation frames required
window.requestAnimFrame = (function(){
    return  window.requestAnimationFrame   ||
            window.webkitRequestAnimationFrame ||
            window.mozRequestAnimationFrame    ||
            window.oRequestAnimationFrame      ||
            window.msRequestAnimationFrame     ||
            function(callback, element){
                window.setTimeout(callback, 1000 / 60);
            };
})();

//Create the final object and run it
var game = new Game();
function init() {
    if(game.init())
        game.start();


}

$(document).keydown(function(e){
    if(e.keyCode == 38) {

  this.speed = 10;

});

}

这是我为向上箭头键添加的代码,但不起作用:

    $(document).keydown(function(e){
        if(e.keyCode == 38) {

      this.speed = 10;

    });
} 

谢谢

【问题讨论】:

    标签: javascript jquery background


    【解决方案1】:

    如果您想将“Background”对象的“speed”属性设置为 10,那么您不应该将“this.speed”设置为 10,因为在 keydown 函数中,“this”不是指您的背景对象。

    这可能就是你想要做的:

    $(document).keydown(function(e){
        if(e.keyCode == 38) {
          game.background.speed = 10;
        }
    });
    

    见:How does the "this" keyword work?

    【讨论】:

      猜你喜欢
      • 2019-10-27
      • 1970-01-01
      • 1970-01-01
      • 2013-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-11
      • 2020-10-23
      相关资源
      最近更新 更多