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

30.08.2020, 16:34

Bug in JS-Code nicht zu finden?!

Servus miteinander,
wie bereits in der Überschrift zu lesen kann ich den Bug nicht finden. Wenn der Ball an den Ecken ankommt, so wechselt er ständig die Richtung. Wär cool, wenn jemand einen Blick draufwirft, ich bin mit meinem Latein nämlich am Ende.
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/** surrounding ****************************************************************/
    var canvas = document.getElementById("GS");
    var context = canvas.getContext("2d");
    
     canvas.width = 1020;
     canvas.height = 700;
    
     var Uwidth = 200;
     var Uheight = 20;
     var UpositionX = canvas.width/2 - Uwidth/2;
     var UpositionY = canvas.height - Uheight - 10;
     var UmaxSpeed = 20;
     var Uspeed = 0;
     
     var Owidth = 200;
     var Oheight = 20;
     var OpositionX = canvas.width/2 - Uwidth/2;
     var OpositionY =  10;
     var OmaxSpeed = 20;
     var Ospeed = 0;

     
        function gameLoop() {
            
            context.clearRect(0, 0, canvas.width, canvas.height);
            
            animate();
            update();
            ani();
            upd();
            changeBALL();
            drawBALL();
            requestAnimationFrame(gameLoop);
            
        }
     
            requestAnimationFrame(gameLoop);
            

/** paddleMOVE_unten ********************************************************************/  
    
    function animate() {
            
            requestAnimationFrame(animate);
            context.fillStyle = "black";
            context.fillRect( UpositionX, UpositionY, Uwidth, Uheight);
            
        }
    
    function update() {
        
        UpositionX += Uspeed;
        if (UpositionX <= 0) {UpositionX = 0}
        if (UpositionX >= canvas.width - Uwidth) {UpositionX = canvas.width - Uwidth}
        
    }
    
/** paddleMOVE_oben********************************************************************/    
    
        function ani() {
            
            requestAnimationFrame(animate);
            context.fillStyle = "black";
            context.fillRect( OpositionX, OpositionY, Owidth, Oheight);
            
        }
    
    function upd() {
        
        OpositionX += Ospeed;
        if (OpositionX <= 0) {OpositionX = 0}
        if (OpositionX >= canvas.width - Owidth) {OpositionX = canvas.width - Owidth}
        
    }
/** paddleSTEER_unten **************************************************************************/
    
    function UmoveLeft() {
        Uspeed = -UmaxSpeed;
        }
        
    function UmoveRight() {
        Uspeed = UmaxSpeed;
    }
    
    function Ustopp() {
        Uspeed = 0;
    }
    
    document.addEventListener("keydown", event => {
        
        if (event.isComposing || event.keyCode === 37) {
        
            UmoveLeft();
            
            }
            
        if (event.isComposing || event.keyCode === 39) {
            
            UmoveRight();
            
        }});

    document.addEventListener("keyup", event => {
        
        if (event.isComposing || event.keyCode === 37) {
                        Ustopp();   
            }
            
        if (event.isComposing || event.keyCode === 39) {
                            Ustopp();
        }});

/** paddleSTEER_oben **************************************************************************/

    function OmoveLeft() {
        Ospeed = -OmaxSpeed;
        }
        
    function OmoveRight() {
        Ospeed = OmaxSpeed;
    }
    
    function Ostopp() {
        Ospeed = 0;
    }
    
    document.addEventListener("keydown", event => {
        
        if (event.isComposing || event.keyCode === 65) {
        
            OmoveLeft();
        
            }
            
        if (event.isComposing || event.keyCode === 68) {
            
            OmoveRight();
            
        }});

    document.addEventListener("keyup", event => {
        
        if (event.isComposing || event.keyCode === 65) {
            
            
                Ostopp();   
            }
            
        if (event.isComposing || event.keyCode === 68) {
            
            
                Ostopp();   
        }});
/** ball & rectangle cooperation ************************************************************************************************/
    
    var x = 20 + (Math.round(Math.random() * 900) + 0.5);
    var y = 150 + (Math.round(Math.random() * 100) + 0.5) ;
    var vx = 8;
    var vy = 8;
    var radius = 20;
    
    function drawBALL() {
        
        x += vx;
        y += vy;

        context.fillStyle = "#FF0080";
        context.beginPath();
        context.arc(x, y, radius, 0, Math.PI*2, true);
        context.closePath();
        context.fill();
        
            } 
    
    function changeBALL() {
        
            var topofBALL = y - radius;
            var bottomofBALL = y + radius;
            var UtopofRECT = UpositionY;
            var ObottomofRECT = OpositionY + Oheight;
            
            var UleftRECT =  UpositionX;
            var UrightRECT = UpositionX + Uwidth;
            var OleftRECT = OpositionX;
            var OrightRECT = OpositionX + Owidth;
    
        
            if ((x + radius) >= canvas.width) {vx = -vx}
            if ((x - radius) <= 0) {vx = -vx}
            
            if ( y > canvas.height) {vy = 50}
            if (y < 0) {vy = -50}
        
            if (( bottomofBALL >= UtopofRECT ) && (x >= UleftRECT ) && (x  <= UrightRECT )){vy = -vy}
            if (( topofBALL <= ObottomofRECT) && (x >= OleftRECT) && (x  <= OrightRECT)){vy = -vy}
    
        }



    
    

TrommlBomml

Community-Fossil

Beiträge: 2 117

Wohnort: Berlin

Beruf: Software-Entwickler

  • Private Nachricht senden

2

30.08.2020, 16:41

ChangeBALL() sollte bei Abprallern prüfen, ob der Ball auch in die Richtung fliegt, also bspw. wenn du die linke Seite prüfst auch schaust, dass der Ball nach links fliegt:

Quellcode

1
if ((x - radius) <= 0 /*zweite Bedingung*/ && vx < 0) {vx = -vx}


Wenn du das für alle vier Abprallrichtungen prüfst sollte es vielleicht schon gelöst sein. Ansonsten bin ich etwas verwirrt, dass du die Funktion gameLoop() und animate() per requestAnimationFrame registrierst, das solltest du nur für die gameLoop machen.

3

31.08.2020, 13:56

Danke Dir vielmals, hat tatsächlich geholfen!!

Werbeanzeige