Jump to content

Camera following group and these sprites


Youle
 Share

Recommended Posts

Hi,

I've some problem with Phaser.Group and Camera.

I've a group : game.pirate, and it's sprite : pirateSprite, when in move my group I can see the camera is following it but its sprite is continuing its move.

There's my code :

function create(game) {    game.pirate = new Phaser.Group(game, null, 'Pirate', true);    map = game.add.tilemap('map');    map.addTilesetImage('ground_1x1');    layer = map.createLayer('Tile Layer 1');    layer.resizeWorld();        var pirateSprite = game.add.sprite(0, 0, 'pirate');    pirateSprite.animations.add('idle')    pirateSprite.animations.play('idle', 2, true)    pirateSprite.anchor.setTo(0.5, 0.5);    game.pirate.add(pirateSprite)    game.camera.follow(game.pirate);    cursors = game.input.keyboard.createCursorKeys();}function update(game) {    if (cursors.left.isDown)    {        game.pirate.x-=6;    }    else if (cursors.right.isDown)    {        game.pirate.x+=6;    }    if (cursors.up.isDown)    {        game.pirate.y-=6    }    else if (cursors.down.isDown)    {        game.pirate.y+=6    }}

Have you an idea ?

Link to comment
Share on other sites

I'm not sure what you're trying to do, or what the problem is here. In your code above, your camera is following the x and y position of the group, not of the sprite inside. If you then move the sprite, it will move relative to the group and, consequently, the camera, so it will go off the screen.

 

I'm pretty sure also that the camera can only follow objects properly if they are not inside moving groups, as I think it directly gets their local position, rather than their world position - but I could be wrong here.

Link to comment
Share on other sites

Actually it's precisely my problem I never ask to move my sprite but only my group (so my sprite x and y should be 0, 0 and visually the same of it parent). 

If I ask to follow the sprite but move the group, Phaser consider my sprite at 0, 0... and don't move.

Isn't it possible to follow an entire group (who's a display object) ? 

Link to comment
Share on other sites

In theory it should be, as all it needs is the position property of the object to track it, but maybe not... the docs state this for the target property:

 

target Phaser.Sprite |Phaser.Image |Phaser.Text  

The object you want the camera to track. Set to null to not follow anything

 

Suggesting only sprites, images and text are trackable.

Link to comment
Share on other sites

No problem. I'd suggest if you want to get round this, you can still use sprites as groups:

var pirate = game.add.sprite(0, 0, null);var pirateSprite = game.add.sprite(0, 0, 'pirate');// add the pirateSprite to the pirate 'group' (in actual fact just another sprite)pirate.addChild(pirateSprite);

You'll not get some of the handy group methods, but you can still access an array of the sprite's children and pass that to collision handlers or iterate through as usual via pirateSprite.children - hopefully that allows you to keep doing what you wanted to do originally!

 

I'll have to look into groups being tracked by the camera as I can't see any reason why this doesn't work.

Link to comment
Share on other sites

Just been doing a bit of testing and groups are indeed trackable exactly as expected: http://jsfiddle.net/lewster32/4tS4q/

 

I think I may have found your issue. The group you're adding is not being added to the world, because the second parameter is 'null', and then it's being added to the stage rather than the world. Can you try changing line 2 to this:

game.pirate = new Phaser.Group(game, game.world, 'Pirate');

And see if this fixes the problem?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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