object.draw() 显示了 al object.draw() 调用中最后定义的图像
object.draw() shows last defined image on al object.draw() calls
我对javascript/编程很陌生,我正在尝试我编写的一些基本的画布游戏脚本。我正在尝试使 4 个背景图像在关键事件上以不同的速度向左或向右移动。到目前为止一切顺利,它工作正常。
当我在画布上绘制 4 张图像时,问题就开始了。所有 x,y 和 h,w 设置工作正常,接受图像本身。在画布上,它显示了所有图纸的相同(最后定义的)图像。我现在知道我在这个问题上哪里出错了。
希望你们能帮助我!提前感谢
<!DOCTYPE HTML>
<html>
<head>
<title>Canvas test game/ movement</title>
</head>
<body>
<canvas id="myCanvas" width="1600" height="1000"></canvas>
<footer></footer>
</body>
<script>
var canvas = document.getElementById('myCanvas'),
context = canvas.getContext('2d'),
img = new Image();
var newImage = function(){
this.image = function(name){
img.src="./images/" + name;
};
this.setSize = function(w, h){
this.w = w;
this.h = h;
};
this.setPosition = function(x, y){
this.x = x;
this.y = y;
};
this.setSpeed = function(speed){
this.speed = speed;
};
this.changePosition = function(direction){
if (direction){
this.x = this.x + this.speed;
} else {
this.x = this.x - this.speed;
}
};
this.draw = function(){
context.drawImage(img, this.x, this.y, this.w, this.h);
};
};
var move = function(direction){
achtergrond.changePosition(direction);
maan.changePosition(direction);
landschapAchter.changePosition(direction);
landschapVoor.changePosition(direction);
};
var achtergrond = new newImage();
achtergrond.image("galaxy.png");
achtergrond.setSize(1600, 1000);
achtergrond.setPosition(0, 0);
achtergrond.setSpeed(0);
var maan = new newImage();
maan.image("maan.png");
maan.setSize(400, 400);
maan.setPosition(200, 100);
maan.setSpeed(1);
var landschapAchter = new newImage();
landschapAchter.image("landschapAchter.png");
landschapAchter.setSize(3200, 500);
landschapAchter.setPosition(0, 450);
landschapAchter.setSpeed(2);
var landschapVoor = new newImage();
landschapVoor.image("landschapVoor.png"); // In the browser all 4 (achtergrond, maan, landschapAchter and landschapVoor) objects show this img on object.draw()
landschapVoor.setSize(4294, 400);
landschapVoor.setPosition(0, 600);
landschapVoor.setSpeed(3);
var checkKey = function(e){
e = e || window.event;
if (e.keyCode == '37') {
move(1);
} else if (e.keyCode == '39') {
move(0);
}
// Move images on key event
context.clearRect ( 0, 0 , 1600 , 1000 );
achtergrond.draw();
maan.draw();
landschapAchter.draw();
landschapVoor.draw();
};
window.onload = function(){
maan.draw();
landschapAchter.draw();
landschapVoor.draw();
achtergrond.draw();
}
document.onkeydown = checkKey;
</script>
</html>
我想你在指定 src 标签时错过了这一点:)
var newImage = function(){
this.image = function(name){
this.src="./images/" + name;
};
试试这个。
相关文章:
- 函数未在Object.keys或Object.getOwnPropertyNames下列出,但可以调用
- delete在Object上效率低下,但在DOM Element's的数据属性,与null out相比
- 有没有一种方法可以列出Ember.Object的所有绑定
- 如何取消object.prototypes javascript的一个函数
- Jquery Datatables错误:无效的JSON基元:draw
- 为什么是文档.旧版应用程序中的DOM-object.properties为null
- 将*.js文件的内容放入Object中
- Object.prototype using 'this'
- 使用Object.create()的角度服务继承
- 如何使用object.assign()从其他对象引用基本对象属性
- 循环的数组推入在Object容器中具有不同的值
- reducers在redux中得到Function not Object,what'it’他错了
- JSON.stringify和Object.keys在同一个对象上产生不同的结果
- 在使用object.create创建的对象中使用super
- Join架构验证:Join.object定义数组中的有效键
- jquery打印[object XMLDocument]而不是文件内容
- ExtJS 4 Object.prototype fail
- JavaScript-从对象数组中输出随机OBJECT
- 为什么“{}+1”在Chrome和Firefox中排名第一,而字符串'[object object]1'
- object.draw() 显示了 al object.draw() 调用中最后定义的图像