Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

01.05.2020, 14:34

Alternative zu clearRect() ??

Serwus liebe Community!
Mit dem folgenden Code plane ich ein Game (nichts Besonderes). I werde die JS-Datei unten einfügen. Wär cool, wenn da mal jemand drüberschauen könnte. Durch das clearRect() in der Funktion drawBall() kann ich logischerweise das Image, welche ich in der Funktion brick() lade, nicht sehen. Ich bitte um Hilfe! Gibt es vielleicht sogar Alternativen zu clearRect() ?? Hoffe,dass meine Fragestellung verständlich genug ist, zumal die Übersicht im Code durch die Kommentare gewärleistet sein sollte.
Danke, mfg UndercoverKEKS


Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
var canvas = document.getElementById("gameScreen");
var context = canvas.getContext("2d");
var imgBrick = document.getElementById('img_brick');

canvas.width = 1020;
canvas.height = 700;

this.width = 200;
this.height = 20;
this.position = {
x: canvas.width/2 - this.width/2,
y: canvas.height - this.height - 10,
};
this.maxSpeed = 10;
this.speed = 0;

function gameLoop() {
animate();
update();
drawBALL();
changeBALL();
requestAnimationFrame(gameLoop);
}

/** paddleMOVE ********************************************************************/


function animate() {

requestAnimationFrame(animate);
context.clearRect(0, 650, canvas.width, canvas.height);
context.fillStyle = "black";
context.fillRect( this.position.x, this.position.y, this.width, this.height );

}

function update() {

this.position.x += this.speed;
if (this.position.x < 0) {this.position.x = 0}
if (this.position.x > canvas.width - this.width) {this.position.x = canvas.width - this.width}

}

gameLoop();

/** paddleSTEER **************************************************************************/

function moveLeft() {
this.speed = -this.maxSpeed;
}

function moveRight() {
this.speed = this.maxSpeed;
}

function stopp() {
this.speed = 0;
}

document.addEventListener("keydown", event => {

if (event.isComposing || event.keyCode === 37) {

moveLeft();

}

if (event.isComposing || event.keyCode === 39) {

moveRight();

}});

document.addEventListener("keyup", event => {

if (event.isComposing || event.keyCode === 37) {

if(this.speed < 0) {stopp();}

}

if (event.isComposing || event.keyCode === 39) {

if(this.speed > 0) {stopp();}

}});


/** ball & rectangle cooperation ************************************************************************************************/

var x = 200;
var y = 200;
var vx = 8;
var vy = 8;

var radius = 20;



function drawBALL() {

x += vx;
y += vy;

[b]context.clearRect(0, 0, canvas.width, 650);[/b] /*durch dieses clearRect ist das Image, das ich in der Funktion brick() lade, nicht sichtbar..was tun?**/
context.fillStyle = "#FF0080";
context.beginPath();
context.arc(x, y, radius, 0, Math.PI*2, true);
context.closePath();
context.fill();

}


function changeBALL() {

var bottomofBALL = y + radius;
var topofRECT = this.position.y;

var leftRECT = this.position.x;
var rightRECT = this.position.x + this.width;


if ((x + radius) >= canvas.width) {vx = -vx}
if ((x - radius) <= 0) {vx = -vx}
if ((y - radius) <= 0) {vy = -vy}



if (( bottomofBALL >= topofRECT) && (x >= leftRECT) && (x <= rightRECT)){vy = -vy}

if ( y > canvas.height) {vy = 50}
}


/** Image ************************************************************************************************/

[b]function brick() {

imgBrick.onload = function() {
context.drawImage(imgBrick, 10, 10, 100, 100)
}
}[/b]

brick();

David Scherfgen

Administrator

Beiträge: 10 382

Wohnort: Hildesheim

Beruf: Wissenschaftlicher Mitarbeiter

  • Private Nachricht senden

2

02.05.2020, 13:13

Du solltest pro Frame nur einmal leeren (clearRect), und zwar bevor du anfängst Paddle & Ball zu zeichnen.

3

02.05.2020, 13:52

Das heißt, ich sollte clearRect() einmal in der GameLoop einbauen?

David Scherfgen

Administrator

Beiträge: 10 382

Wohnort: Hildesheim

Beruf: Wissenschaftlicher Mitarbeiter

  • Private Nachricht senden

4

02.05.2020, 19:15

Ja klar. Ansonsten löschst du doch alles wieder, was du bis dahin gezeichnet hast.

5

04.05.2020, 16:05

Danke Dir!!

Werbeanzeige