Jump to content

player not moving with moving platform.


valueerror
 Share

Recommended Posts

the funny thing is - it works in my demo game  http://test.xapient.net/phaser/ALL/   but not in the much simpler testcase..  i'm unable to figure out what i need to do..

i tried applying materials, setting the circle different (i found out that friction is somehow dependant on the circle)...but nothing...

 

here is the testcase:

 http://test.xapient.net/phaser/movingplatform.html

 

here is the code:

function preload(){    game.load.spritesheet('mario', 'assets/mariospritesheet-small.png',50,50);    game.load.image('platform', 'assets/paddle-small.png'); }function create() {         game.physics.startSystem(Phaser.Physics.P2JS);        game.physics.p2.gravity.y = 1200;        game.physics.p2.friction= 5;        platformH = game.add.sprite(300,  200, 'platform');         game.physics.p2.enable(platformH);        platformH.body.kinematic=true;        game.add.tween(platformH.body).to({ x: '+100' }, 2000,Phaser.Easing.Linear.None).to({x:'-100'}, 2000,Phaser.Easing.Linear.None).yoyo().loop().start();        player = game.add.sprite(150,  50, 'mario',1);         game.physics.p2.enable(player);        player.body.fixedRotation=true;         cursors = game.input.keyboard.createCursorKeys();        jumpKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);}function update() {        if (cursors.left.isDown  ){ player.body.moveLeft(300);}        else if (cursors.right.isDown ) { player.body.moveRight(300);}         if (jumpKey.isDown ){player.body.moveUp(700);}}

 

Link to comment
Share on other sites

thx jp91 for this suggestion..  but wouldn't this create a permanent connection between those two?  it looks like a mighty workaround around a very simple problem..    IMHO the friction setting should do it - also..  it works in my demo game (just move to the right of the world - you'll see 2 horizontal moving plattforms where you can jump on and float with them..  i just don't know why it works in the one example and why not in the other  :(

Link to comment
Share on other sites

you mean it would create the spring the moment the player touches the platform?

 

as for the examples.. in both examples it does NOT work..  in my second example i'm using materials as i did in my demo game (first link first post)  in my demo game it works with the same setup as in example 2 ..   i just can't find the difference  :(

 

 

since it works in my demo game without springs i find you suggestion interesting but i think it is not necessary.. 

Link to comment
Share on other sites

well..  i found out the difference...  :wacko:

 

in my demogame i'm tweening (only for the horizontal platforms) the velocity and not the x coordinates...    damn..  i did't see that at all..

 

if tweening x or y for horizontal moving platforms no collision is happening anymore.. i am surprised that my player does not fall through.. because if i program a collisioncallback it only gets fired once -- when you land on the platform.. after that you should be still colliding because you don't fall through the platform but maybe those narrowphase equations are over and therfore no callbacks are called... what do i know..

 

so while tweening Y is ok for vertical movement it is, for some (maybe obvious) reason not working with X for horizontal movements..  :(

 

i honestly don't understand why since velocity is probably nothing else than changing x and y coords every step.. but here collisions are happening.. in a tween (which simulates the same thing) NOT

 

i knew this already before i made this post but since it "suddenly" worked i thought this changed with 2.0.1  

 

so if i want to tween between two exact points i have to use body.x instead of body.velocity.x  ..  

so back to the beginning..  

how to make my player move with the platform when this platform moves in x direction ?

Link to comment
Share on other sites

Hey, I probably wouldn't use tweens at all for something like this. Instead, I'd do:

function preload(){    game.load.spritesheet('mario', 'assets/mariospritesheet-small.png',50,50);    game.load.image('platform', 'assets/paddle-small.png');     game.load.image('clouds', 'assets/512x512-tiling-sky.jpg');}function create() {         game.add.tileSprite(0, 0, 800, 600, 'clouds');        game.physics.startSystem(Phaser.Physics.P2JS);        game.physics.p2.gravity.y = 1200;        game.physics.p2.friction= 5;        platformV = game.add.sprite(200,  300, 'platform');         game.physics.p2.enable(platformV);        platformV.body.kinematic=true;        platformV.body.velocity.y = -50;                platformH = game.add.sprite(300,  200, 'platform');         game.physics.p2.enable(platformH);        platformH.body.kinematic=true;        platformH.body.velocity.x = 50;                player = game.add.sprite(150,  50, 'mario',1);         game.physics.p2.enable(player);        player.body.fixedRotation=true;         cursors = game.input.keyboard.createCursorKeys();        jumpKey = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);}function update() {        if (platformV.body.y <= 100) {          platformV.body.velocity.y = 50;        }        else        if (platformV.body.y >= 300) {          platformV.body.velocity.y = -50;        }                if (platformH.body.x <= 200) {          platformH.body.velocity.x = 50;        }        else        if (platformH.body.x >= 400) {          platformH.body.velocity.x = -50;        }        if (cursors.left.isDown  ){  player.scale.x = -1; player.body.moveLeft(300);}        else if (cursors.right.isDown ) { player.scale.x = 1; player.body.moveRight(300);}         if (jumpKey.isDown ){player.body.moveUp(700);}        }

You're probably going to have a bunch of these platforms in a level. In that case, I'd create a custom platform class that extends sprite and have it's update logic move itself.

Link to comment
Share on other sites

hey thx.. i came to a similar conclusion 20 minutes ago ...   tweens had been my first choice because it is a beautiful and simple solution.. also flexible..

since i'm not really good at javascript, subclassing sounds really good but i wouldn't know how to do it  :unsure:

 

so i tried to implement my own "tween" function that, when the platforms are confiured properly, does the rest..

 

so my solution is: creating a group for all platforms - and then call my function via "forEach" in the update loop

movingplatforms.forEach(movePlatforms,this); 
function  movePlatforms(platform){    if (platform.body.sprite.name == 'horizontal'){   //check for the moving direction         if (platform.body.x > platform.body.sprite.rightbounds){  platform.body.sprite.velo *= -1;} //if reached bounds reverse speed        if (platform.body.x < platform.body.sprite.leftbounds) { platform.body.sprite.velo *= -1;}  // reverse speed        platform.body.velocity.x = platform.body.sprite.velo;    } else if (platform.body.sprite.name == 'vertical'){        if (platform.body.y > platform.body.sprite.bottombounds){  platform.body.sprite.velo *= -1;}        if (platform.body.y < platform.body.sprite.topbounds) { platform.body.sprite.velo *= -1;}        platform.body.velocity.y = platform.body.sprite.velo;    } }

this also allows me to create all my platforms from objects (a tiled object layer) and configure the all the needed values directly in tiled (name, velo, distance.. )  

 

here is the working example:

http://test.xapient.net/phaser/movingplatform-withouttweens.html

Link to comment
Share on other sites

There's an example here that you can follow: http://examples.phaser.io/_site/view_full.html?d=sprites&f=extending+sprite+demo+1.js&t=extending%20sprite%20demo%201

 

But it seems like you've found a good solution for your use case  :)

 

 

thx for the link !  this looks promising.. do you think i could use this in combination with   createFromObjects()    ? because i'm using tiled to make my tilemap layers and i am using an object layer to position all of the sprites that need to "do" something..  (release powerups, move around the world, count up coins, etc.)

Link to comment
Share on other sites

  • 7 months later...
 Share

  • Recently Browsing   0 members

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