我想用Javascript构建一个小小的来回射击游戏作为我在数字艺术领域的最后一堂课。但是,我写的代码在几秒钟后开始运行得非常慢。起初我使用的是setInterval(),但后来切换到requestAnimationFrame()来尝试提高性能。这没有用。这是代码:
var canvas = document.getElementById("myCanvas");
var ctx = canvas.getContext("2d")
var youtube = new Image(); // Create new img element
var bullet = new Image();
var background = new Image();
var spaceship = new Image();
var url = new Image ();
var moveh = 150
var shipx = 150
var right = true
var rightPressed = false
var leftPressed = false
var spacePressed = false
var bulletUp = 280
var shot = false
var explosion = false
document.addEventListener("keydown", keyDownHandler, false);
document.addEventListener("keyup", keyUpHandler, false);
function keyDownHandler(e) {
if(e.keyCode == 39) {
rightPressed = true;
}else if(e.keyCode == 37) {
leftPressed = true;
}else if (e.keyCode == 32){
spacePressed = true
}
}
function keyUpHandler(e) {
if(e.keyCode == 39) {
rightPressed = false;
}
else if(e.keyCode == 37) {
leftPressed = false;
}
else if (e.keyCode == 32){
spacePressed = false;
}
}
function moveship(){
drawship()
canvas.width = canvas.width
if (rightPressed && shipx < 380){
shipx += 7
}
else if (leftPressed && shipx > 3){
shipx -= 7
}
}
function drawyoutube(){
youtube.addEventListener("load", function() {
ctx.drawImage(youtube, moveh,10);
}, false);
youtube.src = 'YouTube.png'; // Set source path
}
function drawbackground(){
background.addEventListener("load", function(){
ctx.drawImage(background, 0,0);
}, false);
background.src = "paper.jpg"
}
function drawship(){
spaceship.addEventListener("load", function(){
ctx.drawImage(spaceship, shipx, 250);
}, false);
spaceship.src = "SpaceShip.png";
}
function MoveYoutube(){
drawyoutube()
if (right == true){
moveh += 8
if (moveh > 380){
right = false
}
}
else if(right == false){
moveh -= 10
}if (moveh < 3){
right = true
}
}
function drawbullet(){
if (shot == true){
bullet.addEventListener("load", function(){
ctx.drawImage(bullet, shipx + 40, bulletUp);
}, false);
bullet.src = "bullet.png";
}
}
function shoot(){
if (spacePressed){
shot = true
}
else if (shot == true){
drawbullet()
bulletUp -= 10
}if (bulletUp == -20){
shot = false
bulletUp = 260
}else if(bulletUp == 10){
explosion == true
write()
}
}
}
function write(){
url.addEventListener("load", function(){
ctx.drawImage(background, 150,35);
}, false);
url.src = "url.png"
}
function draw(){
drawbackground()
moveship()
shoot()
MoveYoutube()
requestAnimationFrame(draw)
}
我的直觉是说它与我加载图片的方式有关,但我不知道。
答案 0 :(得分:1)
正如Alexander O&#Mara;的评论中所述,您每次绘制图像时都会尝试重新加载图像。
在开始游戏逻辑之前,您需要一个加载所有图像资源的步骤。像这样:
var imageSrcs = ['YouTube.png', 'bullet.png', 'paper.jpg', 'SpaceShip.png', 'url.png'];
var images = [youtube, bullet, background, spaceship, url];
var loadCount = 0; // keep a count of images that have loaded so far
function loadImages() {
for(var i = 0; i<images.length; i++) {
loadImage(images[i], imageSrcs[i]);
}
}
function loadImage(img, src) {
img.addEventListener('load', imageLoaded);
img.src = src;
}
function imageLoaded() {
loadCount++;
if(loadCount === images.length) {
startGame();
}
}
然后,您可以从startGame开始渲染和更新循环,因为您知道所有图像都已加载。在那之后,您可以确信ctx.drawImage(spaceship, 0, 0)
应该将图像绘制到画布上,并且您不必将其包装在加载事件处理程序中。