Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Problem with body.onFloor


AprendizEnLaRed
 Share

Recommended Posts

First sorry for my english

 

I'm newbie with phaser and I am trying to create a small game of runner using the code for platform example.

 

But I do not get to jump with body.onFloor

    function create() {        game.add.sprite(0, 0, 'cielo');        plataformas=game.add.group();        //ground        suelo=game.add.tileSprite(0,500,800,100,'suelo');        suelo.body.allowGravity = false;        suelo.body.immovable = true;        suelo.body.setRectangle(800, 128, 0, 0);        plataformas.add(suelo);        player = game.add.sprite(32, game.world.height - 228, 'jugador');        player.body.bounce.y = 0.2;        player.body.gravity.y = 1000;        player.body.collideWorldBounds = true;        player.animations.add('correr', [0, 1, 2, 3, 4, 5, 6, 7, 8], 20, true);        btnSaltar = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);    }    function update() {        game.physics.collide(player, plataformas);        suelo.tilePosition.x -=2;        player.animations.play('correr');        if (btnSaltar.isDown && player.body.onFloor())        {            player.body.velocity.y = -200;        }    }

I had use suelo.body.setRectangle for use collide.

 

Any advice?

 

And also some good tutorial?I've seen very few

 

Very thx

Link to comment
Share on other sites

Jump and runs have a hard time in 1.1.4/1.1.5 ...

 

onFloor() flickers every frame (https://github.com/photonstorm/phaser/issues/374) (only an issue if you have a jump animation based on onFloor() or not)

 

gravity values bigger then the jump velocity prevents jumps completly

https://github.com/photonstorm/phaser/issues/373 (https://github.com/photonstorm/phaser/issues/402)

I am guessing this is your issue here.

 

Rich is on it, but it looks like we'll have to wait for 1.2 with our platfomers.

 

(See here: http://www.html5gamedevs.com/topic/3777-phaser-115-released-and-where-were-going-next/#entry24039 and more explanation here http://www.html5gamedevs.com/topic/3777-phaser-115-released-and-where-were-going-next/#entry24173 )

 

I am hoping that we'll get slopes and stuf like that in 1.2 (or some time later) as a reward for beeing patient ;)

Link to comment
Share on other sites

  • 5 months later...

Hi everybody,

Little up for this ! The value of body.onFloor() still flickering on Phaser v2.0.7 with animations :(

Exemple : http://www.cubcubcub.com/bugonfloor/

 

The velocity Y constantly changing (0 ~ 4.5).

 

And is there a way to set a sprite/body as floor ? For the moment on the collider event function, I set the body.blocked.down to true to return onFloor() true but is there a better way ?

Link to comment
Share on other sites

Hi everybody,

Little up for this ! The value of body.onFloor() still flickering on Phaser v2.0.7 with animations :(

Exemple : http://www.cubcubcub.com/bugonfloor/

 

The velocity Y constantly changing (0 ~ 4.5).

 

I tried having your anchor not change when you hit the floor and it seemed to stop.

 

Maybe if when you land after changing anchor you set the x/y position up by however much you moved the anchor down by? As what I think you're doing is moving the body into the floor when you move the anchor down as you've landed.

Link to comment
Share on other sites

Hmmm yes you are right. I can change the anchor on axe X but if I change the Y then the body flick. It's weird that the anchor changes values of physics body. As if the physical body was a child of the sprite.

 

In my animation, the body(image) of the cat change so I have to redifine the position of the sprite relative to its body(arcade phys).

 

Edit :
If I change the value of the condition is cat falling/jumpg to higher value (before +/-2, to +/-50)

if (this.sprite.body.velocity.y < -50) {    this.changeAnim('jump');} else if (this.sprite.body.velocity.y > 50) {    this.changeAnim('fall');}

It stop flick (ok it flick on ~3frames) but not infinite flick.

If somone have a good pratice about onFloor / anchor / animation, " I'm all ears " :D

Link to comment
Share on other sites

The anchor directly influences the body's position, as the body must be in the correct position if the user alters the anchor - so in some sense yes, the body is a child of the sprite, and in other ways the sprite is a child of the body. This is what leads to the crazy vibrating as the two try to wrest control of the sprite's position at different points in the update.

Link to comment
Share on other sites

The anchor directly influences the body's position, as the body must be in the correct position if the user alters the anchor - so in some sense yes, the body is a child of the sprite,

 

Hmmm.. If a sprite have no body phys you can move it freely. If we add a body phys to it, you can't move it, the phys move it for you. For me the anchor should act like an offset of the sprite relative to his body phys.

Is it possible to set an offset in pixel (not relative unit like anchor) to a sprite animation without change the phys ? With trimmed images/animations it sounds me "fatality" ^^

Link to comment
Share on other sites

You may be able to do this with the pivot.x and pivot.y properties maybe, though probably a cleaner way is to offset the body instead by using body.setSize, which allows you to set a size and an offset. Be sure to reset the body's position after altering this though, as it can cause a sudden change in velocity and make you sprite fly up into the air or off the screen.

Link to comment
Share on other sites

Thanks lewster32 but..

Changing this.sprite.anchor.setTo(0.5, 0.35); to this.sprite.pivot.y = -10; fail, it flicks too.

Changing this.sprite.anchor.setTo(0.5, 0.35); to this.sprite.body.setSize(12, 25, 0, -10); fail, it flicks too (the reset doom the sprite/body, reseting position etc..)

 

Solutions (?)

1 - Use non-trimmed image/animation and if tomorrow I draw an animation for my cat that requiert larger space I have to re-export everything (and waste space in the image).

 

2 - Set a timer check on change anim like :

function wantToChangeAnimAnchor(){      if (game.time.now - this.timeLastChangeAnim < 100){            return false;      }      ...}

3 - Set bigger value on condition is cat falling/jumping to start animation like :

if (this.sprite.body.velocity.y < -100) {       this.changeAnim('jump');} else if (this.sprite.body.velocity.y > 100) {       this.changeAnim('fall');}

4 - answer 42

Link to comment
Share on other sites

It's hard to tell what's going on in your code but I see at one point you're setting blocked.down to true - you shouldn't be setting this, the physics system decides if the body is blocked and sets it accordingly. Could you explain what it is you're trying to do exactly, and I'll see if I can help, because I'm just making assumptions here!

Link to comment
Share on other sites

To put onFloor() to true. Phaser return true when it collides to the worldbounds but not when it stays on a platform (like a velocity.y === 0)

And is there a way to set a sprite/body as floor ? For the moment on the collider event function, I set the body.blocked.down to true to return onFloor() true but is there a better way ?

Link to comment
Share on other sites

Just check for body.touching.down, which will be true whenever the player is stood on top of something. body.blocked and onFloor (which just returns body.blocked.down) are specifically for world bounds and tiles. You can determine in the collision callback what it's touching if you want to handle special cases.

 

Something crazy is going on in your code to cause that vibration problem, almost certainly some force or movement is being applied every frame. I think what would be very handy would be for you to make a JSFiddle or Codepen of the bare minimum movement code so it can be easily tweaked and debugged. I tend to make very specific tests in JSFiddle so I can experiment and work things out: http://jsfiddle.net/user/lewster32/fiddles/

Link to comment
Share on other sites

Here a codepen (I prefer JSFiddle but tonight big latency on it..)

http://codepen.io/anon/pen/ynGIp

Only works on Chrome.. (wtf?)

 

The assets animations don't load due to the crossdomain security issue. So on the green bloc it doesn't work, but on the worldbounds it flicks.

 

May be in the next release of Phaser, set a better "onFloor()" function to return if player is trully on the floor (not only for worldbounds or tilemap) ?

 

Lewster32 you can take a loot at my code but don't waste too much time ^^ I may (big may ~ no more holiday :() code my own 'dummy velocity gravity' function for the player and just test collision with overlap. Because I found very weird the fact that sprite control the body that control sprite ^^ In my opinion, body should be the only puppet master with just option with pivot/offset x/y of the sprite.

Anyway thanks lewster32 to spend time here.. Hope few ones offer you beers in UK ;)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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