Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Forced Camera Movement Not Triggering Collisions


Disease
 Share

Recommended Posts

I have a problem that I can't seem to overcome.

 

My game is a forced-scroller beat 'em up like Altered Beast but with endless runner elements. The player can move left and right and I want to constrain the player's character within the bounds of the camera, which is always scrolling to the right. I did a search on how to do this and it was suggested to place "barrier" sprites along the edges of the stage, set their "fixedToCamera" to true and have them collide with the player. I also had to set body.immovable to true on these barriers to get it to somewhat work.

 

Now all of this sort of works. That is to say, if the player is moving with the left and right keys, the barrier stops them from moving through the edges of the screen - great! The problem is when I just let the player sit their neutral that the barriers never collide with them! My guess is that the game.physics.arcade.collide method only checks when one of those objects is moving. Is there some kind of hack I can use to make it so it ALWAYS checks for collisions, even when both objects are technically not moving (even though they ARE moving on the screen)?

update() {		this.game.camera.x += IDemon.gameScrollSpeed;		this.game.physics.arcade.collide(this.player, this.cameraBarriers, this.checkCameraBarrierCollision, null, this);}create() {		// Add Camera Barriers		this.cameraBarriers = this.game.add.group();		this.cameraBarriers.enableBody = true;		this.cameraBarriers.create(0, 0, "cameraBarrier").fixedToCamera = true;		this.cameraBarriers.create(790, 0, "cameraBarrier").fixedToCamera = true;		this.cameraBarriers.setAll("body.immovable", true);}
Link to comment
Share on other sites

You can also try overriding the "update" method on your sprite and applying a small velocity.x when reaching the edge of the camera. If the player sprite is idle, they'll start gliding (or walking, if you change the animation) when they hit the left side.

 

This issue was me being stupid and setting the velocity from what keys were pressed AFTER doing the physics collisions! After I fixed that I basically implemented what you said here - on collision with the back wall I poll the keys one more time to give them a last chance to get away from the edge of the camera view. All of this is working pretty well and I am on my new problem:

 

[Note: I am pretty sure I will figure this out and am just listing the issue for fun. Do not expend any unwanted mental energy here if you don't want to!]

 

How to detect when the player gets "squished" between the world and the edge of the camera. The issue here is that I can only check body.right.blocked when the player is running into a tile that they can collide with. So if they are holding right they get squished but if they are not then they don't. The 'touching' method also does not work - I don't think you are considered to be touching a body after the collision separates you from the object. My next thought was "create a sprite just to the right of the player and check OVERLAP (thanks Rich!) on that sprite. This seems clumsy though so what I will do is: when the player collides with the rear (left) camera barrier (camera is always moving right like in Altered Beast) we do a callback to a function that runs 'getTiles' in an area just to the right of the player (add to players x value). This works perfectly until the character ducks, and then for some reason the area checked is now BELOW his feet which means he will die if touching the left camera barrier even if he is not being 'squished'. My guess is that I am handling ducking wrong because I remember having problems resizing the body when moving into a crouch WITHOUT the player falling into a crouch state. 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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