Jump to content

checkWorldBounds


Pau
 Share

Recommended Posts

Hello!

How do you check if an object is out of the world bound using Phaser3?

I am looking for this Phaser3 equivalent code:

block.checkWorldBounds = true;

block.events.onOutOfBounds.add(lose, this);

block is an image:

block = this.physics.add.image(dropPos, 100, 'c'+aleatorio).setInteractive();
block.setVelocity(0, 200);

 

Thanks!

Link to comment
Share on other sites

Thanks for you help, but i still doing something wrong :(

I am creating multiple blocks each certain amount of time. That is the difference with your code. I have tryied to create a group of elements too, and still doesen't work.
The alert message appears at the beggining of the game or after a time, but never at the right moment.
Can please help me?

Thanks

 

var timedEvent=0;
		var group;
		var config = {
			type: Phaser.AUTO,
			width: 800,
			height: 600,	
			scene: {
				preload: preload,
				create: create
			},
			physics: {
				default: 'arcade',
				arcade: {
					debug: true,
					gravity: { y: 200 }
				}
			}
		};
		var game = new Phaser.Game(config);
		var juego;
		function preload(){
	//	  this.load.crossOrigin = "anonymous";

	//	this.load.setBaseURL("https://pablomonteserin.com/apuntes/web/js/canvas/phaser/ex/fall-down-game/");
	this.load.image('sky', 'img/background.png');
	this.load.image('c0', 'img/c0.png');
	this.load.image('c1', 'img/c1.png');
	this.load.image('c2', 'img/c2.png');
	this.load.image('c3', 'img/c3.png');
}
function create(){
	this.add.sprite(0, 0, 'sky');
	this.physics.world.setBoundsCollision(true, true, true, false);
	timedEvent = this.time.delayedCall(0, onEvent, [], this);
	this.physics.world.on("worldbounds", function (body) {
		alert("great")
		console.log("worldbounds", body);
});
}


function onEvent(){
	candyTimer = 0;
	var aleatorio = Math.floor(Math.random()*3);
	var dropPos = Math.floor(Math.random()*590);

	var block = this.physics.add.image(dropPos, 100, 'c'+aleatorio).setInteractive();
	block.setVelocity(0, 200);

	block.on('pointerdown', clickCandy);

	block.setCollideWorldBounds(true);

	block.body.onWorldBounds = true;
	block.setBounce(0.1, 0.1);
	timedEvent = this.time.delayedCall(1000, onEvent, [], this);


}

function clickCandy(){
	this.destroy();
}

 

envio.zip

Link to comment
Share on other sites

Hi, I had some trouble with checkworldbounds as well. I would suggest that you just create a new gameObject collider outside the camera bounds ( or group of them if you need to check all bounds) and check collision with it. Hope it will help.
 

this.bottom_collider = new Phaser.Physics.Arcade.Sprite(this,0,this.cameras.main.height,).setOrigin(0, 0);
        
//no idea if it is necessary
        this.bottom_collider.width = this.cameras.main.width;
//add to scene
        this.add.existing(this.bottom_collider);
//add to physics
        this.physics.add.existing(this.bottom_collider,true);
//set the physics body width/height to accommodate your desired size
        this.bottom_collider.body.width = this.cameras.main.width;

//after that just add overlap
        this.physics.add.overlap(elementToCheck, this.bottom_collider, callbackFunction,null);

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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