Setting both x and y velocity

  • Hello,

    This is surely a simple problem but I'm having problems setting both an x and y velocity. I'm trying to do a pretty simple runner, to learn to use Haxeflixel, with two types of obstacles : 'static' and 'moving'. Actually both are moving toward the player (although the player's supposed to think he's going towards them). The difference between mobile and static being that 'static' only move on the x axis whereas 'mobile' move on both the x and y axis making them more difficult to avoid.

    The probleme is when I set both a x and y velocity the mobile obstacle only move on the x axis. I know an alternate solution would be to just set an x velocity and then rotate the obstacle at something like a 45° angle. The only problem with this method is that the x and y speed is the same which is not what I want.

    Here is the code of the class :

    class MobileObstacle extends FlxSprite
    	public var verticalScroll:Float = 100;
    	public function new(?X:Float=0, ?Y:Float=0)
    		super(X, Y);
    	 	makeGraphic(110, 110, FlxColor.GREEN);
    	override public function update(elapsed:Float):Void
                    // the obstacle goes up and down
    		if (y > 20 || y >= 580)
    			verticalScroll = -verticalScroll;
    		velocity.set(-PlayState.scrollSpeed, verticalScroll);

    Any help or best way to handle the problem would be greatly appreciated, thanks in advance!

  • Maybe this line?

    if (y < 20 || y >= 580)

  • Oh yeah! Geez I'm tired, that was a really stupid mistake, sorry. Of course the line should have been :

    if (y < 20 || y > 580)

Log in to reply

Looks like your connection to HaxeFlixel was lost, please wait while we try to reconnect.