Jump to content

Moving platforms


Julia
 Share

Recommended Posts

Hey,

 

I was wondering how I could get a few platforms to move from left to right and back, using platforms from my platformgroup.

The problem is the platform isn't bouncing back from the sides and changing the velocity to + or  -.

 

Currently I have this (for only one platform):

 

platforms.getAt(3).body.velocity.x = -100;platforms.getAt(3).body.bounce.set(1);

Any suggestions?

Link to comment
Share on other sites

So which Physics do you enable?
in the update function you could probably write: (Do not know if this is any good for performance)
 

if ( platforms.getAt(3).x <= 0 ){     platforms.getAt(3).x += 0.1}else if ( platforms.getAt(3).x >= game.width ){     platforms.getAt(3).x -= 0.1}else{     platforms.getAt(3).x += 0.1}

So this is a very sloppy solution till it is written into the update with many if statements and you have to set the "walk" way per code.


So with ARCADE physics enabled you could do as posted above using bounce (theory).
So you have enabled a physics on this Object ? And of course you need a Collide - in the update function
 

game.physics.arcade.collide(this.yourPlatform, this.levelCollideOBJECTS);
Link to comment
Share on other sites

Thank you for your answer Sam. Still not working unfortunately :( 
I have physics enabled, this is the piece of code I have in my create function :

platforms = game.add.group();platforms.enableBody = true;platforms.physicsBodyType = Phaser.Physics.ARCADE;var platform = game.add.sprite(x, y, 'platform');game.physics.enable(platform, Phaser.Physics.ARCADE);platform.body.immovable = true;platform.body.allowGravity = false;platform.body.collideWorldBounds = true;platforms.add(platform);

It's moving from left to right, but when it collides with the world bounds, it just stops...

Link to comment
Share on other sites

Seems it missing these lines, the bounce property should work anyway. 

// This gets it movingplatform.body.velocity.setTo(200,200);// This makes the game world bounce-ableplatform.body.collideWorldBounds = true;// This sets the image bounce energy for the horizontal // and vertical vectors. "1" is 100% energy returnplatform.body.bounce.set(1); 
Link to comment
Share on other sites

markusT, your solution is working, but the problem seems it won't work on only one platform from my group, why I've used platforms.getAt(3). Eventually about 5 out of 20 platforms or so should be moving. 

So I can get this to work on all platforms but that's not what I meant :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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