Player physics questions for FlxSprite

20 views
Skip to first unread message

Shaun Michael K. Stone

unread,
Nov 15, 2015, 3:16:23 PM11/15/15
to HaxeFlixel
Hi all, so I am using the Flx Analog to move my player around the screen. There are 3 obstacles:

1) When the player collides with it (top, left, bottom, right), he will bounce away in the opposite direction and then regain composure.
2) When the player is running on top of this object (Mud) it will slow down his movement by a maximum of a half.
3) Ice will make the player speed up and go a bit out of control when run over.

I have created collision class handlers for each collision (player to object). Below is the one for Mud. check is called whenever a collision is identified from update method.

class PlayerMudCollision
{
private var MUD_REDUCTION_SPEED = 10;
private var elapsed:Int = 0;
public function new() 
{
trace('instantiated: ' + Type.getClassName(Type.getClass(this)));
}
public function check(playerShadow:FlxSprite, gameObj:GameObjectGraphic, player:Player) 
{
if (gameObj.isType(GameObjectTypes.MUD)) {
if (elapsed > 600) {
SoundPlayer.playSoundByName(GameSounds.SLIDE_MUD);
elapsed = 0;
}
// slow down player here...
}
elapsed++;
}
}

When joystick is moved, this method is called:

function movePlayer():Void
{
if (this.player.canControl() == false) {
return;
}
if (this.player.hasNoHealth()) {
this.player.velocity.x = 0;
this.player.velocity.y = 0;
return;
}
var angle:Float = joystick.getAngle();
var accelerationX = joystick.acceleration.x;
var accelerationY = joystick.acceleration.y;
var velocityX:Float = GameSize.getPositionByPlatform(20) * accelerationX;
if (angle > -90 && angle < 90) {
this.player.moveRight(0);
} else if (angle > 90 || angle < -90) {
this.player.moveLeft(0);
} else {
this.player.returnUpRight();
}
this.player.velocity.x = velocityX;
if (this.player.isInAir()) {
this.player.velocity.y = 0;
return;
}
this.player.velocity.y = GameSize.getPositionByPlatform(20) * accelerationY;
}


Help would be so good right now!

Thanks.
Reply all
Reply to author
Forward
0 new messages