wombat

Locking camera on a sprite

Recommended Posts

Hi all,

I want to make the camera follow a sprite so that the sprite is always in the same position on the screen. 

 

Updating the camera's position in the 'update' works, but with a lag (i e when the player is moving fast the surrounding doesn't catch up with the sprite immediately.

 

Not working 1:

// in the 'update' functiongame.camera.setPosition(this.player.sprite.x - 10, this.player.sprite.y - 50);

Setting the camera to follow the sprite does what I want, except that the upper left corner of the sprite is in the center of the screen.

 

Not working 2:

game.camera.follow(this.player.sprite);

Then I thought I could make my player sprite a child of a group, add another (invisible) sprite to the group at an offset, and have the camera follow the invisible sprite. I want the player to have a physics body however, and from what I understand I cant enable physics on the parent Group. (I can pass the group to game.physics.enable, but there's no 'body' set on the group).

 

Out of ideas. How can I make the camera 'follow' the sprite, instantly, but with an offset?

Share this post


Link to post
Share on other sites

Thank you, but specifying Phaser.Camera.FOLLOW_LOCKON changes nothing. It's the default value.

 

// Phaser.Camera sourcefollow: function (target, style) {        if (typeof style === "undefined") { style = Phaser.Camera.FOLLOW_LOCKON; }

Share this post


Link to post
Share on other sites

You can currently not specify an offset for locking on to a sprite. You could however do this in your update() function:

game.camera.focusOnXY(mysprite.x + offsetX, mysprite.y + offsetY)

the standard camera follow does:

game.camera.focusOnXY(mysprite.x, mysprite.y)

when following a sprite

Share this post


Link to post
Share on other sites

...here's something that works, kind of:

sprite.anchor.setTo(1.6, 0.45);

although it expresses the offset in sprite dimensions ("draw top left corner 1.6 times sprite.width to the left of center") so it's a bit tricky to get right. Also it's a PIXI.Sprite attribute so I don't know if it's safe to mess with it.

 

@CtlAltDel When I tried that there was a lag, so I thought that when I moved the camera in the update() I was out of sync with the world's movements. When the sprite was accelerating/decelerating (fast) it would change its position on the screen. When I set the anchor as above it doesn't do this. I believe that's because the camera position isn't normally updated in the update() function?

 

@Heppell08 As I understand it the camera follow styles define an area of the screen in which the sprite can move without the camera following (i e without the background and other sprites scrolling). Perhaps I can define a really small area (1x1 pixel perhaps) at the place where I want the sprite to be 'locked'? I'll give it a try, though maybe I'm not really understanding how it's supposed to work.

 

 

Edit: Ta-da!

       
game.camera.deadzone = new Phaser.Rectangle(200,380,1,1);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.