admin管理员组文章数量:1293986
I want to speed up my whole background if press the up & arrow keys.
I have tried to do it but do not work.
this.speed
is to control the speed for the background.
So I put an if statement and said if up arrow key pressed then put the speed to 10.(You can see the Code below)
This is my Code:
//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 = ".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-ponent, 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;
});
}
This is the Code that i have included for the up arrow keys, but doesn't work:
$(document).keydown(function(e){
if(e.keyCode == 38) {
this.speed = 10;
});
}
Thanks
I want to speed up my whole background if press the up & arrow keys.
I have tried to do it but do not work.
this.speed
is to control the speed for the background.
So I put an if statement and said if up arrow key pressed then put the speed to 10.(You can see the Code below)
This is my Code:
//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/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-ponent, 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;
});
}
This is the Code that i have included for the up arrow keys, but doesn't work:
$(document).keydown(function(e){
if(e.keyCode == 38) {
this.speed = 10;
});
}
Thanks
Share Improve this question edited Nov 16, 2016 at 2:43 Aruna 12k3 gold badges30 silver badges42 bronze badges asked Dec 4, 2014 at 20:27 The Good GuyThe Good Guy 311 gold badge2 silver badges11 bronze badges1 Answer
Reset to default 2If you want to set the "speed" property of your "Background" object to 10 then you should not be setting "this.speed" to 10, because in the keydown function, "this" does not refer to your background object.
This is probably what you meant to do:
$(document).keydown(function(e){
if(e.keyCode == 38) {
game.background.speed = 10;
}
});
See: How does the "this" keyword work?
本文标签: javascriptMove Background With Arrow Key PressStack Overflow
版权声明:本文标题:javascript - Move Background With Arrow Key Press - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741592285a2387199.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论