Jump to content

Error message when using accelerateToXY function


jaymeh
 Share

Recommended Posts

Hi Everyone,

 

I am currently working on my first game using Phaser but I am having an issue with using accelerateToXY. I am developing a space invaders type game and for the bullet code I am following parts of the tanks example to try and fire a bullet. Here is the line it seems to be stuck on:

bullet.rotation = game.physics.arcade.accelerateToXY('playerBullet', this.player.x, this.player.y + 10, 40, 100, 100);

However when trying to fire the bullet I get the following error message in the console from phaser.min.js:

 

"Uncaught TypeError: Cannot read property 'acceleration' of undefined"

 

If anyone could offer any assistance with this issue it would be greatly appreciated

 

Thanks,

Jaymeh

 

Edit: I am currently running phaser version  2.0.6

Link to comment
Share on other sites

Did you enable physics on the sprite? You must enable physics so that the sprite will have a physics body, acceleration is a property of the physics body.
 

game.physics.enable(bullet, Phaser.Physics.ARCADE);
Link to comment
Share on other sites

Hi Dumtard,

 

Thanks for your reply.

 

I have basically created a group and the bullet is object within that group which has physics attached. It may be helpful if I just pasted in my whole JavaScript file since I am not sure if everything is being setup correctly as various tutorials seem to do it differently. :)

var cSpacebar;var cCursorKeys;var gBullets;var gEnemies;var playState = {	create: function() {		// Setup any functions relating to world		this.background = game.add.tileSprite(0, game.world.height, 'background');		this.background.anchor.setTo(0, 1);				// ...				// Setup the control scheme				cSpacebar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);		cCursorKeys = game.input.keyboard.createCursorKeys();				//...				// Setup the player object				this.player = game.add.sprite(game.world.width/2, game.world.height/2, 'player');		this.player.anchor.setTo(0.5, 0.5);				game.physics.arcade.enable(this.player);		this.player.body.collideWorldBounds = true;		game.camera.follow(this.player);				// ...				// Setup a group for player bullets				gBullets = game.add.group();	    gBullets.enableBody = true;	    game.physics.arcade.enable(gBullets);	    gBullets.createMultiple(30, 'playerBullet', 0, false);	    gBullets.setAll('anchor.x', 0.5);	    gBullets.setAll('anchor.y', 0.5);	    gBullets.setAll('outOfBoundsKill', true);	    gBullets.setAll('checkWorldBounds', true);				// ...				// Setup a group for enemies				gEnemies = game.add.group();		gEnemies.enableBody = true;		game.physics.arcade.enable(gEnemies);		/// gEnemies.createMultiple(10, 'enemy', 				// ...	},	update: function() {		// Player Movement Controls				if(cCursorKeys.left.isDown) {			this.player.x -= 5;		}else if(cCursorKeys.right.isDown) {			this.player.x += 5;		}else{			this.player.x += 0;		} 				if(cCursorKeys.up.isDown) {			this.player.y -= 5;		}else if(cCursorKeys.down.isDown) {			this.player.y += 5;		}else{			this.player.y += 0;		}		cSpacebar.onDown.add(this.fireBullet, this);				// ...				// Stage Setup - Move Background				this.background.y += 2;	},		fireBullet: function() {		// Create a bullet element using players position;		var bullet = gBullets.getFirstExists(false);		game.physics.enable(bullet, Phaser.Physics.Arcade);		console.log(bullet);		bullet.rotation = game.physics.arcade.accelerateToXY('playerBullet');	}};

Thanks,

Jamie

Link to comment
Share on other sites

Looking at the docs, you have to pass in an object to accelerateToXY(), not a string. Arcade.accelerateToXY, and an x, y position to accelerate to. You do not need to enable physics here because that will happen at sprite creation with the group.

The fireBullet function should look something like this.

fireBullet: function() {  var bullet = gBullets.getFirstExists(false);  bullet.rotation = game.physics.arcade.accelerateToXY(bullet, this.player.x, this.player.y);}

Setting up the group, Group.createMultiple is asking for a key to an image, you do not load a 'playerBullet' image that I can see, and createMultiple will create the bodies for you. I would put the image loading into the preload function not the create function.

preload: function() {  game.load.image('playerBullet', 'path/to/image.png');}create: function() {  ...  gBullets = game.add.group();  gBullets.enableBody = true;  gBullets.createMultiple(30, 'playerBullet');  gBullets.setAll('anchor.x', 0.5);  gBullets.setAll('anchor.y', 0.5);  gBullets.setAll('outOfBoundsKill', true);  gBullets.setAll('checkWorldBounds', true);  ...}

Edit: Looking at your code again, you don't seem to load any assets in, you will need to load in something for background and player as well.
And move this line:

cSpacebar.onDown.add(this.fireBullet, this);

out of the update function and into the create function. It is adding the fireBullet function to spacebar every frame which is not necessary, it only has to be added once.

 

 

Link to comment
Share on other sites

Ahh that's where I went wrong I was adding a reference to the image I loaded not the bullet object. All the preload stuff gets loaded in a seperate JavaScript class.

As with the space bar I was getting confused as I thought it had to be checked that the space had been pressed every frame.

Thank you very much for your help .

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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