Jump to content

Soloshadow
 Share

Recommended Posts

Hi everyone,

I am pretty new to phaser but I am working on an endless runner game and I would like my player to be able to jump on top of platforms. BUt everytime I try to jump on one my character just pass through it like it isn't even there.

 

This is the code to my platform (I am working in typescript if it matters)

class Platform extends Phaser.Sprite{

    private platform;

    constructor(g: Phaser.Game, x: number, y: number){
        super(g,x,y,"platform",0);
               
        this.platform = this.game.add.sprite(x,y,'platform')
        this.platform.scale.setTo(5, 2);

        

        
       //this.game.physics.startSystem(Phaser.Physics.ARCADE);
          
        this.game.physics.enable(this.platform, Phaser.Physics.ARCADE);
        //this.platform.body.allowGravity = false;
        this.platform.body.immovable = true;

    }

    public update(){
        this.platform.body.velocity.x -= 0.5;
        /*
        if(this.platform.body.velocity.x < -(this.platform.width)){
            this.platform.body.velocity.x = this.game.width;
            console.log("this");
        }
        */
    }

}

 

This is the player class. I didn't work on this. A team member did this 

class Player extends Phaser.Sprite{ //create a subclass voor Phaser.Sprite 
    
    private playerSprite = this.game.add.sprite(0, 0, 'player'); //de url van de sprite staat in global.ts
    
    //gravity
    private jumpTimer = 0;

    private jump: Phaser.Key = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); //mapping keys
    private jumpAlt: Phaser.Key = this.game.input.keyboard.addKey(Phaser.Keyboard.UP);
    private slide: Phaser.Key = this.game.input.keyboard.addKey(Phaser.Keyboard.CONTROL);
    private slideAlt: Phaser.Key = this.game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
    
    public speed:number = 30;
    private score:number = 0;
    
    constructor(game:Phaser.Game, x:number, y:number){ 
        //word ook sprite aangemaakt???
        super(game,x,y,"player",0); //Een typescript functie voor subclasses( https://tutorialstown.com/super-keyword-with-constructor-in-typescript/ )
        
        //geen verwarring meer bij this.y en this.playerSprite.y
        this.playerSprite.x = x;
        this.playerSprite.y = y;
        
        //spritegrootte
        this.playerSprite.width = 100;
        this.playerSprite.height = 144;
        
        //muisklik (op telefoon is dat je vinger)
        this.playerSprite.inputEnabled = true;
        //this.playerSprite.events.onInputDown.add(Player.prototype.jump, this);
        
        this.jump.onDown.add(Player.prototype.goUp, this); //Voer functies uit op basis van input
        this.jumpAlt.onDown.add(Player.prototype.goUp, this);
        this.slide.onDown.add(Player.prototype.goDown, this);
        this.slideAlt.onDown.add(Player.prototype.goDown, this);
        
        this.anchor.set(0.5,0.5); //verander de anchor point voor de sprite
        this.scale.set(0, 0.2);
        
        //jumping (gravity)
        this.game.physics.startSystem(Phaser.Physics.ARCADE);
        //this.game.physics.arcade.gravity.y = 250;
   

        this.game.physics.arcade.enable(this.playerSprite);
      
        this.playerSprite.body.collideWorldBounds = true;
        
        
        //this.game.physics.enable(this.playerSprite, Phaser.Physics.ARCADE);
        this.playerSprite.body.gravity. y = 250;
        this.playerSprite.body.bounce.y = 0.15;
        this.playerSprite.body.collideWorldBounds = true;

        console.log("player setup"); 
    }

  private doJump(velocity:number)
    {
        if (this.jump.isDown && this.playerSprite.body.onFloor() && this.game.time.now > this.jumpTimer || 
        this.jumpAlt.isDown && this.playerSprite.body.onFloor() && this.game.time.now > this.jumpTimer)
        {
            this.playerSprite.body.velocity.y = velocity;
            this.jumpTimer = this.game.time.now + 750;
            console.log("jumping");
        }
    }
    

 

And in the update function in the gameplay state we added this.game.physics.arcade.collide(this.player, this.platform);

this.player  and this.platform is the name of the instance that was created in the gameplay state

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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