Jessicaward25

Changing sprite position

Recommended Posts

Ok, so I want to add gravity to a sprite..

 

CreateGame function

game.physics.startSystem(Phaser.Physics.ARCADE);player = game.add.sprite(400, 400, 'playerImage');game.physics.arcade.enable(player, Phaser.Physics.ARCADE);player.enableBody = true;player.collideWorldBounds = true;player.body.velocity.setTo(200, 200);player.body.setSize(20, 32, 5, 16);player.body.bounce.set(0.8);game.physics.arcade.gravity.y = 250;

Update/Draw function

game.world.removeAll();game.add.sprite(player.x, player.y, 'playerImage');

Does the "game.add.sprite(player.x, player.y, 'playerImage);" line add a completely new sprite? I've noticed in the update function it draws the player at player.x and player.y... But the gravity section of code doesn't ever touch player.x/player.y.. How would I change the X/Y coordinates using Phaser's arcade physics?

 

Or is there a better way of drawing the sprite?

 

Thanks in advance

Share this post


Link to post
Share on other sites

Your code isn't changing the gravity for a sprite, it's changing it for the entire world.

 

 
Here is how I would re-write your code above:
game.physics.startSystem(Phaser.Physics.ARCADE);player = game.add.sprite(400, 400, 'playerImage');game.physics.arcade.enable(player);// player.enableBody = true; (this isn't needed for a Sprite, only for a Group)// player.collideWorldBounds = true; (this won't work, it needs to be applied to the Body)player.body.velocity.setTo(200, 200);player.body.setSize(20, 32, 5, 16);player.body.bounce.set(0.8);// this sets gravity on the whole world, if you want it just on a single sprite you// should apply it to the sprite body insteadgame.physics.arcade.gravity.y = 250;

Your update function is going to clear the entire world (why?!) every single frame. And then create a brand new sprite, which won't have physics enabled for it at all, every single frame.

 

You really shouldn't do stuff like that in your update loop. That really isn't what it's meant for.

 

What is it you're trying to actually do in your update loop?

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.