Jump to content

Collisions, jumping, and probably a very stupid mistake!


tongy01
 Share

Recommended Posts

Hi, Im very new (minutes old) to the forum, and have been trawling through other posts of a similar nature in hopes I could solve this on my own without wasting anyones time.

 

Ive been following the e-book 'Discover Phaser' and have so far not encountered many problems (my own fault for incorrect capitalisations and missing semicolons), but Im quite sure I've done everything so far correctly. My code so far is :

var mainState = {        preload: function() {                game.load.image('player', 'assets/player.png');        game.load.image('wallV', 'assets/wallVertical.png');        game.load.image('wallH', 'assets/wallHorizontal.png');    },        create: function() {                game.stage.backgroundColor = '#3498db';                game.physics.startSystem(Phaser.Physics.ARCADE);                this.player = game.add.sprite(game.world.centerX, game.world.centerY, 'player');        this.player.anchor.setTo(0.5, 0.5);                // Tell Phaser that the player will use the Arcade physics engine        game.physics.arcade.enable(this.player);         // Add vertical gravity to the player        this.player.body.gravity.y = 500;                this.cursor = game.input.keyboard.createCursorKeys();                this.createWorld();    },                update: function(){                this.movePlayer();        game.physics.arcade.collide(this.player, this.walls);                if (!this.player.inWorld)         { this.playerDie();        }            },           movePlayer: function() {            // If the left arrow key is pressed         if (this.cursor.left.isDown) {            // Move the player to the left            this.player.body.velocity.x = -200;         }                    // If the right arrow key is pressed        else if (this.cursor.right.isDown) {             // Move the player to the right             this.player.body.velocity.x = 200;        }                         // If neither the right or left arrow key is pressed            else {            // Stop the player                 this.player.body.velocity.x = 0;        }                        // If the up arrow key is pressed and the player is touching the ground        if (this.cursor.up.isDown && this.player.body.touching.down) {                        this.player.body.velocity.y = -320;        }    },        createWorld: function() {        // Create our wall group with Arcade physics             this.walls = game.add.group();             this.walls.enableBody = true;        // Create the 10 walls            game.add.sprite(0, 0, 'wallV', 0, this.walls); // Left             game.add.sprite(480, 0, 'wallV', 0, this.walls); // Right            game.add.sprite(0, 0, 'wallH', 0, this.walls); // Top left            game.add.sprite(300, 0, 'wallH', 0, this.walls); // Top right            game.add.sprite(0, 320, 'wallH', 0, this.walls); // Bottom left            game.add.sprite(300, 320, 'wallH', 0, this.walls); // Bottom right                    game.add.sprite(-100, 160, 'wallH', 0, this.walls); // Middle left            game.add.sprite(400, 160, 'wallH', 0, this.walls); // Middle right                    var middleTop = game.add.sprite(100, 80, 'wallH', 0, this.walls);            middleTop.scale.setTo(1.5, 1);            var middleBottom = game.add.sprite(100, 240, 'wallH', 0, this.walls);            middleBottom.scale.setTo(1.5, 1);                  // Set all the walls to be immovable            this.walls.setAll('body.immovable', true);          },        playerDie: function() {         game.state.start('main');    },        };var game = new Phaser.Game(500, 340, Phaser.AUTO, 'gameDiv');game.state.add('main', mainState);game.state.start('main');

The game seems to run fine, except for the jump.

 

It seems to me that the ' this.player.body.touching.down ' is the problem and Ive found other post with people having issues here too. I've tried 'this.player.body.blocked.down' and 'this.player.body.onFloor()' and have probably used them totally incorrectly.

 

If i remove this section (so i only pass 'this.cursor.up.isDown' in the if statement) Jumping still doesn't work properly. to get any movement up, i have to move the player off an edge so its in mid air, and then up will take the player up the ceiling. 

 

 

 

I also have a general question, unrelated to this problem regarding the editor Brackets, and if there was away to get the problem finder so detect the phaser.min.js file and recognise the laser functions so i don't constantly get errors. Im currently including the two JS files in the HTML file.

<script type="text/javascript" src="phaser.min.js"></script><script type="text/javascript" src="main.js"></script> </head>

main.js is my code, the same as the file pasted above.

 

Thanks in advance for any help anyone can give, and I'm hoping I haven't humiliated myself to much.

Link to comment
Share on other sites

  • 2 months later...

Nope, I ended up skipping ahead to the end of the chapter and copied out the full example of the finished program.

 

It worked perfectly at this point.

 

There must be a very simple mistake which got missed by the editors in the chapter I mentioned above. Ive not yet had the time to cross reference the new version with the un-working version to see what was different.

Link to comment
Share on other sites

  • 9 months later...

I've also encountered this dumb error(lol). Probably the part missed out was the use of player.body.onFloor()

//movePLayer if (this.cursor.up.isDown || this.wasd.up.isDown) {    this.jumpPlayer(); }},jumpPlayer: function() {     if (this.player.body.onFloor()) {         this.jumpSound.play();          this.player.body.velocity.y = -320; }},

haven't testet this but thanks for letting me know that the full source code is working.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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