Jump to content

Having trouble with deadzone


dnassler
 Share

Recommended Posts

I'm brand new to Phaser so bear with me.

 

I'm trying to tweak the "basic follow" camera example so that the camera will only follow the movable "player" sprite when the player moves to within 50 pixels of the right side of the viewable space.

 

The example by default follows the "player" when it is in the center of the viewable space so long as the world is big enough to allow for the camera to move.

 

So when I move the player sprite by the cursor keys "right" when the player.x is at or greater than 400 the camera moves with the sprite (400 is the center in the example)...

 

But I want the camera to follow the player only if the player is nearer to the edge of the viewable space... say within 50pixels.

 

When I add the following statement just after the "follow" statement in the example then the camera does not scroll at all. What am I doing wrong? How do you use the deadzone?

 

    game.camera.follow(player);
    game.camera.deadzone.setTo(50,50,700,500);
 
Thanks in advance for anyone's help.
Link to comment
Share on other sites

Also the behaviour varies depending on where the deadzone is set. So....

 

    game.camera.deadzone.setTo(50,50,700,500);
    game.camera.follow(player);
 
Causes the camera to not follow at all but...
 
    game.camera.follow(player);
    game.camera.deadzone.setTo(50,50,700,500);
 
Causes the camera to follow the sprite as if the deadzone was not specified (i.e. behaves the same as the example for "basic follow").
Link to comment
Share on other sites

  • 4 months later...

I'm not sure if you ever solved this problem, but I had the same one so I thought I'd post the answer in case anyone has this problem in the future.

 

The problem is the deadzone object is not instantiated by default, so setTo() can't be called on an undefined object. You have to create a new rectangle first like this:

 

game.camera.follow(player);

game.camera.deadzone = new Phaser.Rectangle(50,50,700,500);

 

After instantiating the deadzone Rectangle, you can then adjust it in the future with the setTo() function if you need to. The reason it would sort of work if follow was called first was because execution was stopping at the error when you called setTo, so if you called follow afterwards, it would never get executed.

 

Hope this helps someone.

Link to comment
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...
 Share

  • Recently Browsing   0 members

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