Jump to content

physics collider not working. [phaser]


AmbitiousIndie
 Share

Recommended Posts

// hello again lads! I've got another problem. I was working on a game and I was specifically  

// working on a Gameplay scene and there was no specific error in the console but the physics  

// collider between my platform and the player wasn't working and my player fell through the 

// platform. Can somebody point out the mistake I've made? Any help will be greatly appreciated :)

// The code from my 'Gameplay' scene:


var platform;
var player;

class gamePlay extends Phaser.Scene{
	constructor(){
		super({key: 'playing'});
	}

	preload(){

		this.load.image('sky', 'assets/sky.png');
		this.load.image('sun', 'assets/sun.png');
		this.load.image('ground', 'assets/platform.png');
		this.load.image('cubey', 
			'assets/cubey(1).png',
			{frameWidth: 32, frameHeight: 32}
		);
		
	}


	create(){
	
		this.add.image(400, 300, 'sky');
		this.add.image(400, 100, 'sun');

		platform = this.physics.add.staticGroup();

		platform.create(400, 500, 'ground');
		platform.create(250, 500, 'ground');
		platform.create(500, 500, 'ground');

		player = this.physics.add.sprite(400, 300, 'cubey');

		player.setBounce(0.3);
		player.setCollideWorldBounds(true);
		this.physics.add.collider(player, platform); // not working

	}



	update(){



	}	
}

 

Edited by AmbitiousIndie
Link to comment
Share on other sites

Instead of defining `player` from a `var` keyword, try defining it in the class you called using:

class example extends Phaser.Scene {

  constructor() {

    super('example');

  }

  create() {

    this.player = this.physics.add.sprite(x, y, "key");

    this.physics.add.collider(this.player, this.collidingThing);

  }

 

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...