基本上我正在为HTML5和Java游戏编写代码的开头,我现在卡住了,因为我想将我的角色(图片astronaut1.png)移动到屏幕上作为我的游戏片段。我还没有为动画编码任何东西,我只关心现在我的宇航员图像只是因为它出现在HTML代码中而生成,因为每次我改变我的代码第35行的位置时,宇航员都保持不变屏幕的一角。我准确地通过ctx.drawimage(img,x,y)改变图像的位置。
这是我的HTML:
这是我的Java:
function startGame() {
myGameArea.start();
}
//defines area, goes on to draw the canvas
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = 1270;
this.canvas.height = 700;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateGameArea, 20);
window.addEventListener('keydown', function (e) {
e.preventDefault();
myGameArea.keys = (myGameArea.keys || []);
myGameArea.keys[e.keyCode] = (e.type == "keydown");
})
window.addEventListener('keyup', function (e) {
myGameArea.keys[e.keyCode] = (e.type == "keydown");
})
},
stop : function() {
clearInterval(this.interval);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
window.onload = function() {
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
var img = document.getElementById("character");
ctx.drawImage(img, 200, 200);
}