在JavaScript中的画布中随机生成图片

3

大家好,我正在尝试使用canvas制作一款javascript游戏。我想生成随机的敌人对象。到目前为止,我找到了这个生成示例:JSFiddle演示

如何加载图像而不是球?任何帮助将不胜感激。谢谢。

// get a refrence to the canvas and its context
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");

// newly spawned objects start at Y=25
var spawnLineY = 25;

// spawn a new object every 1500ms
var spawnRate = 1500;

// set how fast the objects will fall
var spawnRateOfDescent = 0.50;

// when was the last object spawned
var lastSpawn = -1;

// this array holds all spawned object
var objects = [];

// save the starting time (used to calc elapsed time)
var startTime = Date.now();

// start animating
animate();


function spawnRandomObject() {

// select a random type for this new object
var t;

// About Math.random()
// Math.random() generates a semi-random number
// between 0-1. So to randomly decide if the next object
// will be A or B, we say if the random# is 0-.49 we
// create A and if the random# is .50-1.00 we create B

if (Math.random() < 0.50) {
    t = "red";
} else {
    t = "blue";
}

// create the new object
var object = {
    // set this objects type
    type: t,
    // set x randomly but at least 15px off the canvas edges
    x: Math.random() * (canvas.width - 30) + 15,
    // set y to start on the line where objects are spawned
    y: spawnLineY,
}

// add the new object to the objects[] array
objects.push(object);
}



function animate() {

// get the elapsed time
var time = Date.now();

// see if its time to spawn a new object
if (time > (lastSpawn + spawnRate)) {
    lastSpawn = time;
    spawnRandomObject();
}

// request another animation frame
requestAnimationFrame(animate);

// clear the canvas so all objects can be 
// redrawn in new positions
ctx.clearRect(0, 0, canvas.width, canvas.height);

// draw the line where new objects are spawned
ctx.beginPath();
ctx.moveTo(0, spawnLineY);
ctx.lineTo(canvas.width, spawnLineY);
ctx.stroke();

// move each object down the canvas
for (var i = 0; i < objects.length; i++) {
    var object = objects[i];
    object.y += spawnRateOfDescent;
    ctx.beginPath();
    ctx.arc(object.x, object.y, 8, 0, Math.PI * 2);
    ctx.closePath();
    ctx.fillStyle = object.type;
    ctx.fill();
}

}

http://chimera.labs.oreilly.com/books/1234000001654/ch04.html#creating_an_animation_frame_counter - Alex
@Alex,你能帮我看一下我的代码吗? - Shihan Khan
1个回答

3

这里有示例代码

一种方法是创建一个全局数组来存储已加载的图片:

var images = [img1, img2, ... ];

当一个对象被创建时,它会随机分配给images数组中的一个图像。然后我们可以使用ctx.drawImage方法绘制它的图像:ctx.drawImage(object.image,...))。现在重要的是要记住,您需要首先加载所有图像,如下所示:

var img1 = new Image();
img.src = "...";

为了在所有内容都加载完后才运行动画,我们可以使用onload

window.onload = function() {
    animate();
}

以下是我们在创建object时添加的obj.image属性。它从我们的images数组中随机选择一张图片。

var object = {
    ...
    // give random image
    image: images[Math.floor(Math.random()*images.length)]
}

最后,我们可以移除arc绘制函数并使用object.image进行绘制:
for (var i = 0; i < objects.length; i++) {
    var object = objects[i];
    object.y += spawnRateOfDescent;
    ctx.drawImage(object.image, object.x, object.y, 30, 30);
}

1
谢谢。这可以让我的生活更轻松。 - Shihan Khan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接