[SOLVED] My Character move to right direction alone
-
I got something strange when learning tutorial in HaxeFlixel Groundwork
It is that my character move to right direction alone.however i dont think the code is wrong.
I am using VSCode:
package; import flixel.FlxG; import flixel.FlxSprite; import flixel.system.FlxAssets.FlxGraphicAsset; import flixel.util.FlxColor; import flixel.math.FlxPoint; class Player extends FlxSprite { public var speed:Float = 200; public function new(?X:Float=0, ?Y:Float=0) { super(X, Y); makeGraphic(16, 16, FlxColor.BLUE); drag.x = drag.y = 1600; } private function movement():Void { var _up:Bool = false; var _down:Bool = false; var _left:Bool = false; var _right:Bool = false; _up = FlxG.keys.anyPressed([UP, W]); _down = FlxG.keys.anyPressed([DOWN, S]); _left = FlxG.keys.anyPressed([LEFT, A]); _right = FlxG.keys.anyPressed([RIGHT, D]); if (_up && _down) _up = _down = false; if (_left && _right) _left = _right = false; if (_up || _down || _left || _right) velocity.x = speed; velocity.y = speed; var mA:Float = 0; if (_up) { mA = -90; if (_left) mA -= 45; else if (_right) mA += 45; } else if (_down) { mA = 90; if (_left) mA += 45; else if (_right) mA -= 45; } else if (_left) mA = 180; else if (_right) mA = 0; velocity.set(speed, 0); velocity.rotate(FlxPoint.weak(0, 0), mA); } override public function update(elapsed:Float):Void { movement(); super.update(elapsed); } }
anyone know how to solve this problem?
-
if (_up || _down || _left || _right)
is missing the brackets. All code that comes after it should only be executed if that if-statement is true:if (_up || _down || _left || _right) { velocity.x = speed; velocity.y = speed; var mA:Float = 0; if (_up) { mA = -90; if (_left) mA -= 45; else if (_right) mA += 45; } else if (_down) { mA = 90; if (_left) mA += 45; else if (_right) mA -= 45; } else if (_left) mA = 180; else if (_right) mA = 0; velocity.set(speed, 0); velocity.rotate(FlxPoint.weak(0, 0), mA); }
-
@Gama11 said in My Character move to right direction alone:
if (_up || _down || _left || _right)
is missing the brackets. All code that comes after it should only be executed if that if-statement is true:if (_up || _down || _left || _right) { velocity.x = speed; velocity.y = speed; var mA:Float = 0; if (_up) { mA = -90; if (_left) mA -= 45; else if (_right) mA += 45; } else if (_down) { mA = 90; if (_left) mA += 45; else if (_right) mA -= 45; } else if (_left) mA = 180; else if (_right) mA = 0; velocity.set(speed, 0); velocity.rotate(FlxPoint.weak(0, 0), mA); }
after giving the bracket to that part, its fixed! my character stop moving to right direction alone.
thanks Gama11. I need to learn more this haxeflixel programming. :)