herrsander
New member
Hallo,
ich versuche ein Rechteck mit der Tastatur zu bewegen, leider tut sich nichts und ich bekomme keine Fehlermeldung...
- - - Aktualisiert - - -
Hallo,
hat sich erledigt.
ich versuche ein Rechteck mit der Tastatur zu bewegen, leider tut sich nichts und ich bekomme keine Fehlermeldung...
HTML:
<html>
<head>
<title>Tastatureingabe</title>
<meta charset="utf-8">
</head>
<body>
<canvas id="canvas" width="512" height="256"></canvas>
<script src="https://code.jquery.com/jquery-2.1.0.js"></script>
<script>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var breite = canvas.width;
var hoehe = canvas.height;
var paddleheight = 75;
var paddlewidth = 10;
var paddleX = (canvas.width-5);
var paddle = function()
{
ctx.beginPath();
ctx.rect(paddleX, (canvas.height-paddleheight)/2, paddlewidth, paddleheight);
ctx.fill();
ctx.closePath();
}
var pad = function()
{
this.x = breite / 2;
this.y = hoehe / 2;
this.xGeschwindigkeit = 5;
this.yGeschwindigkeit = 0;
};
pad.prototype.move = function ()
{
this.x += this.xGeschwindigkeit;
this.y += this.yGeschwindigkeit;
}
pad.prototype.draw = function ()
{
paddle(this.x, this.y, 10);
};
pad.prototype.setDirection = function (richtung)
{
if (richtung === "hoch")
{
this.xGeschwindigkeit = 0;
this.yGeschwindigkeit = -5;
}
else if (richtung === "runter")
{
this.xGeschwindigkeit = 0;
this.yGeschwindigkeit = 5;
}
};
var pad = new pad();
var keyActions =
{
38: "hoch",
40: "runter"
};
$("body").keydown(function (event)
{
var richtung = keyActions[event.keyCode];
pad.setDirection(richtung);
});
setInterval(function ()
{
ctx.clearRect(0, 0, breite, hoehe);
pad.draw();
pad.move();
ctx.strokeRect(0, 0, breite, hoehe);
ctx.fillRect(this.breite/2, 0, 2, this.hoehe);
}, 30);
</script>
</body>
</html>
- - - Aktualisiert - - -
Hallo,
hat sich erledigt.