Dragyn3652 Posted December 1, 2015 Share Posted December 1, 2015 Hello - I am in need of help figuring out how to stop the paddles from leaving the screen. I am a beginner so my code is not the cleanest, but i would appreciate any help that is out there https://repl.it/B5BZ i uploaded the code to repl.it. thanks Quote Link to comment Share on other sites More sharing options...
GBeebe Posted December 3, 2015 Share Posted December 3, 2015 After you apply speed to the Y, you need to check to see if it's in bounds, if not move it to the edge. I have the variables screenTop and screenHeight which you'll need to replace with your own. Probably 0 (zero) for screenTop and window.innerHeight for screenHeight.this.update = function() { if(this.side == "left") { if(keys.p1.down) { this.y += this.speed; if ((this.y + this.sizeLength) > screenHeight) { this.y = screenHeight - this.sizeLength } } if(keys.p1.up) { this.y -= this.speed; if (this.y < screenTop) { this.y = screenTop; } } }else{ if(keys.p2.down) { this.y += this.speed; if ((this.y + this.sizeLength) > screenHeight) { this.y = screenHeight - this.sizeLength } } if(keys.p2.up) { this.y -= this.speed; if (this.y < screenTop) { this.y = screenTop; } } } } } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.