Jump to content

Stopping a body that has been moved with body.moveLeft() and zero gravity (Ninja)


Larzan
 Share

Recommended Posts

I don't know what to do anymore, i am using the Ninja physics system, have loaded a tilemap, am moving the player with moveLeft upon keydown, but

 

how can i stop the movement?

 

I tried everything, every possible method that i could think of, debugging it, but nothing stopped the darn player from happily gliding around.

 

I assume that i don't have acces to the right body, but even after extensive debugging i can't figure out what to do!

 

Any help would be apreciated as i am on a tight timeframe here (LD and such ;) )

       create() {            //this.game.physics.startSystem(Phaser.Physics.P2JS);            //  Activate the Ninja physics system            this.game.physics.startSystem(Phaser.Physics.NINJA);            // disable gravity            this.game.physics.ninja.gravity = 0;            this.map = this.game.add.tilemap('testlevel');          // Load tilemap            this.map.addTilesetImage('Test', 'testtiles');          // add the tile images            this.layer = this.map.createLayer('Tile Layer 1');      // create layer from tilemap            this.layer.resizeWorld();                               // resize world to fit tilemap            // create the bodies for the tiles            this.ninjatiles = this.game.physics.ninja.convertTilemap(this.map, this.layer, this.slopeMap);            // player            this.sirwalter = this.game.add.sprite(200, 100, 'sirwalter');            // add body to player            this.game.physics.ninja.enableCircle(this.sirwalter, this.sirwalter.width / 2);            this.sirwalter.body.friction = 0.5;            this.game.camera.follow(this.sirwalter);            this.cursors = this.game.input.keyboard.createCursorKeys();        }        update() {            for (var i = 0; i < this.ninjatiles.length; i++) {                this.sirwalter.body.circle.collideCircleVsTile(this.ninjatiles[i].tile);            }            if (this.cursors.left.isDown) {                this.sirwalter.body.moveLeft(20);            } else if (this.cursors.right.isDown) {                this.sirwalter.body.moveRight(20);            } else {                this.sirwalter.body.shape.velocity.x = 0;                this.sirwalter.body.velocity.x = 0;            }            if (this.cursors.up.isDown) {                this.sirwalter.body.moveUp(20);            } else if (this.cursors.down.isDown) {                this.sirwalter.body.moveDown(20);            }        }
Link to comment
Share on other sites

The problem is not with the moveLeft function, whichever way i move the body (body.velocity.x or other) it keeps moving, i guess because of the deactivated gravity?!

 

But i really can't have top down gravity as it is supposed to be a top-down view on a map...

 

I'd still like to use the physics for the collision though...

Link to comment
Share on other sites

not sure if what is say would work but let me just fire away on an idea:

since it is zero gravity and you are using velocity, one way would be to apply velocity in the opposite direction after a set time, so you let the character move in a direction for a second then apply an opposite velocity, and maybe doing this in the same check for direction might be an alternative.    Ive had simple things not work because of how the logic was setup. 

Link to comment
Share on other sites

Thanks, yeah, it sure looks like the gravity is the problem.

 

I now changed my whole approach to my game, threw away the tantalizing tilemaps and just implemented the level loading myself and in the course of that switched to ARCADE physics which work perfectly without gravity.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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