OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I make 2 Immovable Objects Collide and Just Stop [Javascript Phaser]

  • Thread starter Thread starter LeftClickMage
  • Start date Start date
L

LeftClickMage

Guest
I have searched online for many hours for an answer, but no one seemed to be doing movement like I was. I just started my Phaser journey, and got stuck pretty early. I want my game to have movement locked onto the player in the center, with everything else moving around it. When I tried to add a building, however, the player would be pushed out from the middle.

I have tried setting both sprites to immovable or .body.moves = false, but that just ends up making the 2 sprites phase through each other. I have also tried locking the player to the center, with no luck. If there is a better way to go about player movement, let me know, but for now I cannot figure this out.

CODE:

Code:
class greenearth extends Phaser.Scene{
constructor(){
super("startGame");
}
create(){
this.playerCanMove = true;
this.keyA = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
this.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
this.keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
this.keyW = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
this.background = this.add.tileSprite(0, 0, config.width, config.height, "background").setScale(2);
this.background.setOrigin(0, 0);
this.house = this.physics.add.sprite(200, 300, "house").setScale(1);
this.house.setImmovable(true);
this.player = this.physics.add.sprite(config.width/2, config.height/2, "player").setScale(4);
this.player.play("idle");
this.physics.add.collider(this.player, this.house);

}
playerMovement(delta){
this.playerStopped();
        if(this.keyA.isDown){
            this.playerMovedLeft(player.speed, delta);
        } else if(this.keyD.isDown){
            this.playerMovedRight(player.speed, delta);
        } 
        if(this.keyW.isDown){
            this.playerMovedUp(player.speed, delta);
        } else if(this.keyS.isDown){
            this.playerMovedDown(player.speed, delta);
        } 
    
    this.player.body.velocity.normalize().scale(player.speed);
    if(this.keyA.isDown){
        this.player.play("walkLeft", true);
    } else if (this.keyD.isDown){
        this.player.play("walkRight", true);
    } else if (this.keyW.isDown){
        this.player.play("walkUp", true);
    } else if (this.keyS.isDown){
        this.player.play("walkDown", true);
    } else {
        this.player.play("idle", true);
    }
    
}

playerMovedLeft(speed, delta){
    let speedCorrection = (1000/60)/delta;
    this.house.setVelocityX(player.speed * speedCorrection);
    this.background.tilePositionX -= this.house.body.velocity.x/120;
    
}
playerMovedRight(speed, delta){
    let speedCorrection = (1000/60)/delta;
    this.house.setVelocityX(-player.speed * speedCorrection);
    this.background.tilePositionX -= this.house.body.velocity.x/120;
}
playerMovedUp(speed, delta){
    let speedCorrection = (1000/60)/delta;
    this.house.setVelocityY(player.speed * speedCorrection);
    this.background.tilePositionY -= this.house.body.velocity.y/120;
}
playerMovedDown(speed, delta){
    let speedCorrection = (1000/60)/delta;
    this.house.setVelocityY(-player.speed * speedCorrection);
    this.background.tilePositionY -= this.house.body.velocity.y/120;
}
playerStopped(){
    this.house.setVelocityX(0);
    this.house.setVelocityY(0);
}
update(time, delta){
    this.playerMovement(delta);
    // this.player.x = config.width/2;
    // this.player.y = config.height/2;
    
   
}

}

<p>I have searched online for many hours for an answer, but no one seemed to be doing movement like I was. I just started my Phaser journey, and got stuck pretty early. I want my game to have movement locked onto the player in the center, with everything else moving around it. When I tried to add a building, however, the player would be pushed out from the middle.</p>
<p>I have tried setting both sprites to immovable or <code>.body.moves = false</code>, but that just ends up making the 2 sprites phase through each other. I have also tried locking the player to the center, with no luck. If there is a better way to go about player movement, let me know, but for now I cannot figure this out.</p>
<p>CODE:</p>
<pre><code>class greenearth extends Phaser.Scene{
constructor(){
super("startGame");
}
create(){
this.playerCanMove = true;
this.keyA = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.A);
this.keyS = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.S);
this.keyD = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.D);
this.keyW = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.W);
this.background = this.add.tileSprite(0, 0, config.width, config.height, "background").setScale(2);
this.background.setOrigin(0, 0);
this.house = this.physics.add.sprite(200, 300, "house").setScale(1);
this.house.setImmovable(true);
this.player = this.physics.add.sprite(config.width/2, config.height/2, "player").setScale(4);
this.player.play("idle");
this.physics.add.collider(this.player, this.house);

}
playerMovement(delta){
this.playerStopped();
if(this.keyA.isDown){
this.playerMovedLeft(player.speed, delta);
} else if(this.keyD.isDown){
this.playerMovedRight(player.speed, delta);
}
if(this.keyW.isDown){
this.playerMovedUp(player.speed, delta);
} else if(this.keyS.isDown){
this.playerMovedDown(player.speed, delta);
}

this.player.body.velocity.normalize().scale(player.speed);
if(this.keyA.isDown){
this.player.play("walkLeft", true);
} else if (this.keyD.isDown){
this.player.play("walkRight", true);
} else if (this.keyW.isDown){
this.player.play("walkUp", true);
} else if (this.keyS.isDown){
this.player.play("walkDown", true);
} else {
this.player.play("idle", true);
}

}

playerMovedLeft(speed, delta){
let speedCorrection = (1000/60)/delta;
this.house.setVelocityX(player.speed * speedCorrection);
this.background.tilePositionX -= this.house.body.velocity.x/120;

}
playerMovedRight(speed, delta){
let speedCorrection = (1000/60)/delta;
this.house.setVelocityX(-player.speed * speedCorrection);
this.background.tilePositionX -= this.house.body.velocity.x/120;
}
playerMovedUp(speed, delta){
let speedCorrection = (1000/60)/delta;
this.house.setVelocityY(player.speed * speedCorrection);
this.background.tilePositionY -= this.house.body.velocity.y/120;
}
playerMovedDown(speed, delta){
let speedCorrection = (1000/60)/delta;
this.house.setVelocityY(-player.speed * speedCorrection);
this.background.tilePositionY -= this.house.body.velocity.y/120;
}
playerStopped(){
this.house.setVelocityX(0);
this.house.setVelocityY(0);
}
update(time, delta){
this.playerMovement(delta);
// this.player.x = config.width/2;
// this.player.y = config.height/2;


}

}
</code></pre>
 
Top