Jump to content

Sprite child acting funny when physics enabled


Bengalaa
 Share

Recommended Posts

Hi! I have been trying to learn how to use the Sprite.addChild... but I noticed something weird:

 

When I have a sprite (child), and add it as a child of another sprite (parent), if both sprites (parent and child) have physics enabled, whenever i give the parent a velocity, the child will acquire a weird acceleration, anyway, when I print the child's acceleration on the console log, it will be 0.

 

I find this weird, I expected the child to move with it's parent. Is removing the physics to the child the only way to avoid this funny behaviour? because I can think of several cases where I would want the child sprite to have physics enabled.

 

Here is an example:

var mainState = ( function () {    var preload = function () {        game.load.spritesheet('sun', 'img/sun.png', 50,50);        game.load.spritesheet('glow', 'img/glow.png', 70,70);    }    var create = function () {        game.physics.startSystem(Phaser.Physics.ARCADE);        fireball = game.add.sprite(0,0, 'sun');        glow = game.add.sprite(100,100, 'glow');        fireball.anchor.setTo(0.5,0.5);        glow.anchor.setTo(0.5,0.5);        game.physics.enable(fireball, Phaser.Physics.ARCADE); // everything goes fine                 // if I remove this line        game.physics.enable(glow, Phaser.Physics.ARCADE);         glow.addChild(fireball);        glow.body.velocity.x = 10;        // fireball will acquire a huge acceleration    }    var update = function () {    };    return { preload : preload,         create : create,         update : update };})();var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game');game.state.add('main', mainState);game.state.start('main');
Link to comment
Share on other sites

Sorry, I can't manage to make a group respond like a sprite :( I've been exploring the Phaser examples, but I can't find how to change the velocity of all the sprites inside a group... am I missing something?  (I'm very newbie at this n.n')

var sun;var mainState = ( function () {    var preload = function () {    game.load.spritesheet('sun', 'img/sun.png', 50,50);    game.load.spritesheet('glow', 'img/glow.png', 70,70);    }    var create = function () {    game.physics.startSystem(Phaser.Physics.ARCADE);    fireball = game.add.sprite(0,0, 'sun');    glow = game.add.sprite(0,0, 'glow');    fireball.anchor.setTo(0.5,0.5);    glow.anchor.setTo(0.5,0.5);    game.physics.enable(fireball, Phaser.Physics.ARCADE);    game.physics.enable(glow, Phaser.Physics.ARCADE);    sun = game.add.group();    sun.add(glow);    sun.add(fireball);    game.physics.enable(sun, Phaser.Physics.ARCADE);    sun.body.velocity.x = 10; // body is not defined     // glow.addChild(fireball);    // glow.body.velocity.x = 10;    }    var update = function () {    };    return { preload : preload,         create : create,         update : update };})();var game = new Phaser.Game(800, 600, Phaser.AUTO, 'game');game.state.add('main', mainState);game.state.start('main');
Link to comment
Share on other sites

A group doesn't have a body of its own so you can't enable it - you need to enable a body for each sprite.

 

The way the tank and the turret stay together in the tanks example seems to be what you're after.  Essentially apply the velocity to the tank body and then update the turret's position to be where the tank is each frame.

 

http://examples.phaser.io/_site/view_full.html?d=games&f=tanks.js&t=tanks

 

Check out the update() function in it.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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