Phaser: Physics and screen boundaries

This post is part of a Phaser series. Click here to see the first post of the series.

You can detect a collision between physics-enabled items.

Phaser offers us 2 handy methods, to start with.

We have collider and overlap. Both let us detect when objects get one near the other, but with a difference: collider automatically makes object bounce when they meet. overlap lets objects overlap with each other.

This is how we add a collider:

const collisionHappened = (dog, cat) => {
  projectile.destroy()
}

this.physics.add.collider(dogs, cats, collisionHappened, null, this)

and this is how we add an overlap:

const overlapHappened = (dog, cat) => {
  projectile.destroy()
}

this.physics.add.overlap(dogs, cats, collisionHappened, null, this)

You can also set screen boundaries so physics objects will not disappear when they reach the end of the screen.

Call the setCollideWorldBounds() on the object, and pass it the true value:

const dog = this.physics.add.sprite(20, 20, 'dog')
dog.setCollideWorldBounds(true)

If you also want it to bounce when it reaches the screen limit, call

dog.setBounce(1)

The number you pass will determine how fast it will bounce back. Try setting 0.5 or 1.5 and you’ll see it bouncing with less or more energy.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com

This post is part of a Phaser series. Click here to see the first post of the series.

You can detect a collision between physics-enabled items.

Phaser offers us 2 handy methods, to start with.

We have collider and overlap. Both let us detect when objects get one near the other, but with a difference: collider automatically makes object bounce when they meet. overlap lets objects overlap with each other.

This is how we add a collider:

const collisionHappened = (dog, cat) => {
  projectile.destroy()
}

this.physics.add.collider(dogs, cats, collisionHappened, null, this)

and this is how we add an overlap:

const overlapHappened = (dog, cat) => {
  projectile.destroy()
}

this.physics.add.overlap(dogs, cats, collisionHappened, null, this)

You can also set screen boundaries so physics objects will not disappear when they reach the end of the screen.

Call the setCollideWorldBounds() on the object, and pass it the true value:

const dog = this.physics.add.sprite(20, 20, 'dog')
dog.setCollideWorldBounds(true)

If you also want it to bounce when it reaches the screen limit, call

dog.setBounce(1)

The number you pass will determine how fast it will bounce back. Try setting 0.5 or 1.5 and you’ll see it bouncing with less or more energy.


This content originally appeared on flaviocopes.com and was authored by flaviocopes.com


Print Share Comment Cite Upload Translate Updates
APA

flaviocopes.com | Sciencx (2021-04-26T05:00:00+00:00) Phaser: Physics and screen boundaries. Retrieved from https://www.scien.cx/2021/04/26/phaser-physics-and-screen-boundaries/

MLA
" » Phaser: Physics and screen boundaries." flaviocopes.com | Sciencx - Monday April 26, 2021, https://www.scien.cx/2021/04/26/phaser-physics-and-screen-boundaries/
HARVARD
flaviocopes.com | Sciencx Monday April 26, 2021 » Phaser: Physics and screen boundaries., viewed ,<https://www.scien.cx/2021/04/26/phaser-physics-and-screen-boundaries/>
VANCOUVER
flaviocopes.com | Sciencx - » Phaser: Physics and screen boundaries. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/26/phaser-physics-and-screen-boundaries/
CHICAGO
" » Phaser: Physics and screen boundaries." flaviocopes.com | Sciencx - Accessed . https://www.scien.cx/2021/04/26/phaser-physics-and-screen-boundaries/
IEEE
" » Phaser: Physics and screen boundaries." flaviocopes.com | Sciencx [Online]. Available: https://www.scien.cx/2021/04/26/phaser-physics-and-screen-boundaries/. [Accessed: ]
rf:citation
» Phaser: Physics and screen boundaries | flaviocopes.com | Sciencx | https://www.scien.cx/2021/04/26/phaser-physics-and-screen-boundaries/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.