j'ai demandé une autre question aujourd'hui et j'ai un autre ..cant déplacer deux rectangles de toile en même temps
mais d'abord voici le code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Pong</title>
<script type="text/javascript">
var x = 100;
var y = 100;
var xmoveFirst = 720;
var ymoveFirst = 0;
var xmoveSecond = 30 ;
var ymoveSecond = 0;
function canvas() {
var can = document.getElementById('theCanvas');
can.style.backgroundColor = "black";
var ctx = can.getContext('2d');
//first player
ctx.fillStyle="white";
ctx.fillRect(xmoveFirst,ymoveFirst,5,75);
//second player
ctx.fillStyle = 'white';
ctx.fillRect(xmoveSecond,ymoveSecond,5,75);
//first player move
function move(event) {
ctx.clearRect(0,0,750,750); //clear rects
if (event.keyCode == 40) {
ymoveFirst+=25;
console.log("first,"+ymoveFirst);
}
else if (event.keyCode == 38) {
ymoveFirst-=25;
console.log("first,"+ymoveFirst);
}
else if (event.keyCode == 83) {
ymoveSecond +=25;
}
else if (event.keyCode==87) {
ymoveSecond -=25;
}
ctx.fillStyle="white";
ctx.fillRect(xmoveFirst,ymoveFirst,5,75);
ctx.fillRect(xmoveSecond,ymoveSecond,5,75);
}
var movement = document.addEventListener("keydown", move);
}
</script>
</head>
<body onload="canvas()">
<canvas id="theCanvas" width="750" height="750"></canvas>
</body>
</html>
pour que chacun du rect se déplace parfaitement :) mais voici le problème .. Je ne peux pas les déplacer à la fois togheter ..
merci d'aider !!
Vous devriez apprendre le javascript orienté objet. –