Author Topic: Moving platform collide  (Read 2184 times)

uttech2

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Moving platform collide
« on: Wed, Jun 10, 2015 »
Hello all,

I am trying to build a moving platform which should change direction when colliding with a "blocking" sprite.

I works, but unfortunately my platform loses speed after colliding and slows down.

I tried a bit with the flags "moves", "solid" and "immovable" both for the platform and the blocker, but it doesn't seem to have the desired affect.

Has someone an idea? Thank you very much.

PlayState.as:
Code: [Select]
public class PlayState extends FlxState
{
//Variables
public var mover:Mover;
public var blocker:Blocker;
public var blocker2:Blocker;

public function PlayState()
{

}

override public function create():void
{
mover = new Mover(32,0);
add(mover);

blocker = new Blocker;
add(blocker);
blocker2 = new Blocker(128,0);
add(blocker2);
}

override public function update():void
{
super.update();

FlxG.collide(mover, blocker, moverHit);
FlxG.collide(mover, blocker2, moverHit);
}

public function moverHit(m:Mover, b:Blocker):void
{
m.velocity.x = -m.velocity.x;
}

}


Mover.as:
Code: [Select]
public class Mover extends FlxSprite
{
//Variables
[Embed(source = "Grafik/Mover.png")] public var moverGraphic:Class;

public function Mover(X:Number=0, Y:Number=0)
{
super(X, Y, moverGraphic);
velocity.x = 40;

solid = true;
}

override public function update():void
{
super.update();
}

}

Blocker.as:
Code: [Select]
public class Blocker extends FlxSprite
{
//Variables
[Embed(source="Grafik/Blocker.png")] public var blockerGraphic:Class;

public function Blocker(X:Number=0, Y:Number=0)
{
super(X, Y, blockerGraphic);

moves = false;
solid = true;
//immovable = true;
}

}

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: Moving platform collide
« Reply #1 on: Wed, Jun 10, 2015 »
It is slowing down because it is running the default collision function.

There are many ways you could fix it. One option would be to simply use a very high acceleration and lowish maxVelocity instead of changing velocity directly. I noticed a bug in my version where altering velocity directly can mess up collision detection. I haven't fixed it yet, but I think it might be best to stick with acceleration.

uttech2

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Moving platform collide
« Reply #2 on: Thu, Jun 11, 2015 »
I have changed Mover.as and PlayState.as. It seem to work pretty good, thank you.

Code: [Select]

public function Mover(X:Number=0, Y:Number=0)
{
super(X, Y, moverGraphic);
acceleration.x = 500;
maxVelocity.x = 30;
solid = true;
}

Code: [Select]
public function moverHit(m:Mover, b:Blocker):void
{
m.acceleration.x = -m.acceleration.x;
}

uttech2

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Moving platform collide
« Reply #3 on: Sat, Jun 13, 2015 »
I have now another problem.

When the player touches the platform, it floats away.

If I set for the platform:

immovable = true;

it doesn't collide at all.

Is there a way to prevent that the player pushes the platform away?

Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: Moving platform collide
« Reply #4 on: Sun, Jun 14, 2015 »
You are using a slightly different version from me. My version uses "fixed" which does prevent collisions under some circumstances (usually with other fixed objects).

I'm not entirely sure if I understand your problem, but try setting your drag.x and drag.y variables to high levels.

uttech2

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Moving platform collide
« Reply #5 on: Mon, Jun 15, 2015 »
The problem is that when the player jumps on the moving platform it floats away. Also when I jump against it from underneath it flys away ;).

I searched for "fixed" but I couldn't find it. Is it really in Flixel version 2.55?


Minverva

  • Active Member
  • ***
  • Posts: 126
  • Karma: +0/-0
    • View Profile
Re: Moving platform collide
« Reply #6 on: Mon, Jun 15, 2015 »
The problem is that when the player jumps on the moving platform it floats away. Also when I jump against it from underneath it flys away ;).

I searched for "fixed" but I couldn't find it. Is it really in Flixel version 2.55?

My apologies for being unclear. "Fixed" is in an earlier version that I'm using.

As for your problem, you could try overriding the collide function or switch to using overlap. You can set the behavior specific for each "Contact" (e.g. if(Contact is Player)...) when you override the collide functions.

uttech2

  • Member
  • **
  • Posts: 5
  • Karma: +0/-0
    • View Profile
Re: Moving platform collide
« Reply #7 on: Wed, Jun 17, 2015 »
I found now another way, using platforms with defined start and end points. Anyway, thank you very much.