Jump to content

How do I check if my sprite is touching the platform so that I can make it jump?


Recommended Posts

In my code, I have a ground, and some ledges. On pressing up arrow, I want my player sprite to jump only if he is on the ground or a ledge. I added the code:


        if (cursors.up.isDown && player.body.touching.down)        {            player.body.velocity.y = -250;        } 

as I saw in phaser's official first tutorial. This is working for the ground but not for the ledges. When the player is on a ledge, the up arrow doesn't make the sprite jump. In my game, I have not added them to the same group. Is that why this is happening? 


This is the related code:



platforms = this.add.group();        platforms.enableBody=true;                var ground = platforms.create(0,this.world.height - 64, 'ground');        ground.scale.setTo(2, 2);        ground.body.immovable = true;                ledges = this.add.group();        ledges.enableBody = true;        ledges.body.immovable = true;                player = this.add.sprite(32,this.world.height - 150, 'dude');        //enable physics on the player        this.physics.arcade.enable(player);cursors = this.input.keyboard.createCursorKeys();                this.timer = this.game.time.events.loop(2000, this.drawLedge, this);          var rand = Math.floor(Math.random() * 4500)+100;        this.time2 = this.game.time.events.loop(rand,this.drawHelp,this);update: function() {        this.physics.arcade.collide(player,platforms);         this.physics.arcade.collide(player,ledges);         player.body.velocity.x = 0;                player.animations.play('right');                if (cursors.up.isDown && player.body.touching.down)        {            player.body.velocity.y = -250;        }     },drawLedge: function() {        var ledge_y = Math.floor(Math.random()*this.game.world.height/2)+(this.game.world.height/2);        var ledge = ledges.create(this.game.world.width,ledge_y,'ledge');        ledge.anchor.setTo(0.5);        ledge.checkWorldBounds = true;        ledge.kilOutOfBounds = true;        ledge.body.velocity.x=-150; },        drawHelp: function() {        var helper = ledges.create(this.game.world.width, Math.floor(Math.random()*this.game.world.height)+400,'ledge');        helper.anchor.setTo(0.5);        helper.body.velocity.x = -150;        helper.checkWorldBounds = true;        helper.kilOutOfBounds = true;    },

Am I missing something?




Link to comment
Share on other sites


  • Recently Browsing   0 members

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