Jump to content

Camera Phaser 33


Jacko333100
 Share

Recommended Posts

Hey Guys, i created a camera in phaser 3 and everything works fine. thats my code.

this.cameras.main.setSize(500,600);
 
this.cameras.main.startFollow(this.kaefer);
 
 
this.cameras.main.setBounds(0,0,this.GAME_WIDTH,this.GAME_HEIGHT);

 

and now i want the camera to move with my player only and only if the player moves to the right or to the left( so no camera moving when my player jumps). and thats my code for moving the player. Can anyone help me please. i tried an if clause. but that didnt work fine. PLEASE HELP!!!!

move(){
 
if (this.cursors.left.isDown){
this.setVelocityX(-160);
this.anims.play(this.name + 'left', true);
if(this.cursors.up.isDown && this.body.touching.down){
this.setVelocityY(-400);
}
}
else if (this.cursors.right.isDown){
this.setVelocityX(160);
this.anims.play(this.name + 'right', true);
if(this.cursors.up.isDown && this.body.touching.down){
this.setVelocityY(-400);
}
}
else if(this.cursors.up.isDown && this.body.touching.down){
this.setVelocityY(-400);
}else{
this.setVelocityX(0);
this.anims.play(this.name + 'turn');
}
}
Link to comment
Share on other sites

You could either use setBounds in the create method to not allow the camera to move vertically or you could get rid of follow kaefer and in the update method of the scene have:

 

this.cameras.main.setScroll(this.kaefer.x);

You'll probably need to assign the players initial y cord to a variable and pass it as the second parameter in setScroll.

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...