Jump to content

Collide player with world bounds


moto0000
 Share

Recommended Posts

I wrote a small "plugin" to collide sprite with World Bounds

game.module(	'plugins.worldbounds').body(function() {game.collideWorldBounds = {	add: function(sprite, body) {		this.sprite = sprite;		this.body   = body;		game.scene.addObject(this);	},	update: function() {		var width  = Math.max(				this.sprite.width * (1 - this.sprite.anchor.x),				this.body.shape.width / 2			),			height = Math.max(				this.sprite.height * (1 - this.sprite.anchor.y),				this.body.shape.height / 2			);		// top		if(this.body.position.y - height < 0) {			this.body.position.y -= this.body.position.y - height;			this.body.velocity.y = 0;		}		// right		if(this.body.position.x + width > game.system.width) {			this.body.position.x += game.system.width - this.body.position.x - width;			this.body.velocity.x = 0;		}		// bottom		if(this.body.position.y + height > game.system.height) {			this.body.position.y += game.system.height - this.body.position.y - height;			this.body.velocity.y = 0;		}		// left		if(this.body.position.x - width < 0) {			this.body.position.x -= this.body.position.x - width;			this.body.velocity.x = 0;		}	}};});
Link to comment
Share on other sites

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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...