循环使用Javascript将图像数组用作敌人
Javascript for loop to use array of images as enemies
我有一个html文件,它在其中设置了从xml文件解析的图像数组。
var states = xmlDoc.getElementsByTagName("random_state");
for(var i=0; i<states.length;i++)
{
var xx = states[i].getElementsByTagName("random_image");
for(var j=0; j<xx.length;j++)
{
randomurl = xx[j].childNodes[j].nodeValue
}
}
在我的game.js文件中,我有这个
I.sprite = Sprite(randomurl);
它设定了敌人的形象。randomurl是html文件中的全局变量
我的问题是,它没有在屏幕上显示图像阵列,而是只显示阵列中的最后一个图像。我尝试了多种方法来做到这一点,每次都失败了。我正在尝试使用整个阵列的图像显示为敌人。
xml代码只返回4个图像,这是存储在数据库中的图像数组。代码太全面,无法添加。但我已经测试过我的图像是否正确返回,因为我可以让它们出现在div中。我的问题是,它们将以敌人的形式出现,因此只有阵列中的最后一个图像显示为精灵,尽管我需要所有返回的图像来填充这个精灵?
XML代码
$obj2 = StatesCollection::GetRandomStateImages($_GET['state_abbreviation']);
foreach($obj2 as $row2)
{
$random_url="./images/" . $row2['state_image'];
$response .= '<random_state><random_image>' . htmlentities($random_url, ENT_QUOTES) . '</random_image></random_state>';
}
它从数据库中的方法返回图像。
以及game.js的功能,它可以保存敌人的
function Enemy(I) {
I = I || {};
I.active = true;
I.age = Math.floor(Math.random() * 128);
I.color = "#A2B";
I.x = width / 4 + Math.random() * width / 2;
I.y = 0;
I.xVelocity = 0
I.yVelocity = 2;
I.width = 32;
I.height = 32;
I.inBounds = function() {
return I.x >= 0 && I.x <= width &&
I.y >= 0 && I.y <= height;
};
I.sprite = Sprite(randomurl);
I.draw = function() {
this.sprite.draw(canvas, this.x, this.y);
};
I.update = function() {
I.x += I.xVelocity;
I.y += I.yVelocity;
I.xVelocity = 3 * Math.sin(I.age * Math.PI / 64);
I.age++;
I.active = I.active && I.inBounds();
};
I.explode = function() {
Sound.play("explosion");
this.active = false;
// Extra Credit: Add an explosion graphic
};
return I;
};
控制精灵的精灵类
(function() {
function LoaderProxy() {
return {
draw: $.noop,
fill: $.noop,
frame: $.noop,
update: $.noop,
width: null,
height: null
};
}
function Sprite(image, sourceX, sourceY, width, height) {
sourceX = sourceX || 0;
sourceY = sourceY || 0;
width = width || image.width;
height = height || image.height;
return {
draw: function(canvas, x, y) {
canvas.drawImage(
image,
sourceX,
sourceY,
width,
height,
x,
y,
width,
height
);
},
fill: function(canvas, x, y, width, height, repeat) {
repeat = repeat || "repeat";
var pattern = canvas.createPattern(image, repeat);
canvas.fillColor(pattern);
canvas.fillRect(x, y, width, height);
},
width: width,
height: height
};
};
Sprite.load = function(url, loadedCallback) {
var img = new Image();
var proxy = LoaderProxy();
img.onload = function() {
var tile = Sprite(this);
$.extend(proxy, tile);
if(loadedCallback) {
loadedCallback(proxy);
}
};
img.src = url;
return proxy;
};
window.Sprite = function(name, callback) {
return Sprite.load(name, callback);
};
window.Sprite.EMPTY = LoaderProxy();
window.Sprite.load = Sprite.load;
}());
好吧,所以我想我终于明白了,你想要一个URL数组,每次创建敌人时都要遍历它们并使用不同的URL。为了做到这一点,我建议保留一个全局url数组,而不是单个url。我称之为enemyUrls
。你可以这样填充它:
var states = xmlDoc.getElementsByTagName("random_state"),
imgUrlNode, i;
enemyUrls = [];
for(i = 0; i < states.length;i++) {
imgUrlNode = states[i].getElementsByTagName("random_image")[0];
if (imgUrlNode) {
enemyUrls.push(imgUrlNode.nodeValue);
}
}
再添加一个名为currentImg
的全局变量,并将其初始化为0:
var currentImg = 0;
然后当你制造你的敌人时,这样做:
I.sprite = Sprite(enemyUrls[currentImg]);
currentImg = (currentImg + 1) % enemyUrls.length;
相关文章:
- 使用图像数组列表的simplegallary.js显示适合桌面屏幕的图像
- 在字符串中查找所有图像 src URL 并存储在数组中
- 在ListView中添加JSON数组中存储的图像-ReactNative
- JavaScript中的数组图像循环
- 循环以检查数组中的最后一个图像
- 如何从 URL 数组向文档添加图像列表
- 数组上的多图像上传-Javascript/Jquery
- Jquery/Javascript:通过按下按钮显示数组中的随机图像
- JavaScript:是否可以将图像/信息附加到数组项
- Javascript图像数组预加载速度和从内存中删除
- 将图像数组访问到动态表中
- Javascript检查图像是否在数组中
- Javascript图像数组
- 正在从字节数组加载图像内容
- Javascript:如何将SVG图像放入数组中
- JavaScript 在删除预上传图像时不清除图像数组
- 比较数组图像&值如果ok提交表单
- 画布图案饼图与数组图像
- Javascript数组(图像滑块)(Webkit中的bug ?)
- 在画布中绘制数组图像