Jump to content

Need help with Ninja Physics (Phaser 2.4.4)


27thColt
 Share

Recommended Posts

So I am using Ninja physics for the first time for a fighting game, I figured Ninja physics would be most useful for the game.

But I need help, I keep getting this error:

Uncaught TypeError: Cannot read property 'x' of undefined

c.Physics.Ninja.AABB.collideAABBVsAABB @ phaser-ninja-physics.min.js:19

playState.update @ play.js:63

c.StateManager.update @ phaser-ninja-physics.min.js:6

c.Game.updateLogic @ phaser-ninja-physics.min.js:8

c.Game.update @ phaser-ninja-physics.min.js:8

c.RequestAnimationFrame.updateRAF @ phaser-ninja-physics.min.js:13

window.requestAnimationFrame.forceSetTimeOut._onLoop @ phaser-ninja-physics.min.js:13

 

I only get the error when I include the function collideAABBVsAABB() in my update().

Honestly, I don't know why. Here is the code:

playState = {
	preload:function() {
		game.load.image("player1", "assets/img/player1.png");
		game.load.image("player2", "assets/img/player2.png");
		game.load.image("ground", "assets/img/ground.png");
	},
	//creates everything
	create:function() {
		//Players 1 and 2
		this.player1 = game.add.sprite(this.game.width / 2 - 64, this.game.height / 2, "player1");
		this.player2 = game.add.sprite(this.game.width / 2 + 64, this.game.height / 2, "player2");
		this.ground = game.add.sprite(0, this.game.height - 91, "ground");

		this.player1.scale.setTo(2, 2);
		this.player2.scale.setTo(2, 2);


		this.game.time.desiredFps = 30;


		//START OF PHYSICS
		this.game.physics.startSystem(Phaser.Physics.NINJA);
    	this.game.physics.ninja.gravity = 1;
    	this.game.physics.ninja.setBoundsToWorld();

    	this.game.physics.ninja.enableAABB([this.player1, this.player2, this.ground]);

    	this.player1.body.friction = 0;
    	this.player2.body.friction = 0;
    	this.ground.body.friction = 0;

    	this.player1.body.collideWorldBounds = true;
    	this.player2.body.collideWorldBounds = true;
    	this.ground.body.collideWorldBounds = true;

    	this.player1.body.gravityScale = 1;
    	this.player2.body.gravityScale = 1;
    	this.ground.body.gravityScale = 0;

    	this.player1.body.bounce.y = 0.2;
    	this.player2.body.bounce.y = 0.2;

		this.player1.body.immovable = true;
		this.player2.body.immovable = true;
		this.ground.body.immovable = true;


		//END OF PHYSICS

		//key capturing
		this.game.input.keyboard.addKeyCapture([
			Phaser.Keyboard.UP,
			Phaser.Keyboard.DOWN,
			Phaser.Keyboard.LEFT,
			Phaser.Keyboard.RIGHT,
			Phaser.Keyboard.SPACEBAR]);
	},


	//main game loop
	update:function() {
		this.player2.body.aabb.collideAABBVsAABB(this.ground);
		this.player1.body.aabb.collideAABBVsAABB(this.ground);

		this.keyPress();
	},


	//Updates for Key Presses
	keyPress:function() {

		//Player 1
		if (this.input.keyboard.isDown(Phaser.Keyboard.A)) { //left
			this.player1.body.moveLeft(40);

		} else if (this.input.keyboard.isDown(Phaser.Keyboard.D)) { //right
			this.player1.body.moveRight(40);

		} else if (this.input.keyboard.isDown(Phaser.Keyboard.W) && this.player1.body.touching.down) {
			this.player1.body.moveUp(400);

		}

		//Player 2
		if (this.input.keyboard.isDown(Phaser.Keyboard.LEFT)) { //left
			this.player2.body.moveLeft(40);

		} else if (this.input.keyboard.isDown(Phaser.Keyboard.RIGHT)) { //right
			this.player2.body.moveRight(40);

		} else if (this.input.keyboard.isDown(Phaser.Keyboard.UP) && this.player2.body.touching.down) {
			this.player2.body.moveUp(400);

		}
	}
};

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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