Jump to content

javascript game help

giliagmon

im working on a javascript game and i don't want the player to be able to leave the canvas. i have made it where you can't leave the bottom or right side like this

this.hitBottom = function(){
        var rockbottom = myGameArea.canvas.height - this.height;
        var rockside = myGameArea.canvas.width - this.width;
        if (this.y > rockbottom) {
            this.y = rockbottom;
            }
            if(this.x > rockside){
              this.x = rockside;
            }

but i can't figure out how to do the other 2 sides

Link to comment
Share on other sites

Link to post
Share on other sites

Bigger then 0?

 

Or otherwise get the with/height of the canvas and do:

rockbottom-height : Would give you the upper border.

rockside-width: Would give you the left border.

PC: Case: Cooler Master CM690 II - PSU: Cooler Master G650M - RAM: Transcend 4x 8Gb DDR3 1333Mhz - MoBo: Gigabyte Z87x-D3H - CPU: i5 4670K @ 4.5Ghz - GPU: MSI GTX1060 ARMOR OC - Hard disks: 4x 500Gb Seagate enterprise in RAID 0 - SSD: Crucial M4 128Gb

Phone: Samsung Galaxy S6

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×