In player.hx:
public function new(X, Y, _upKey:String, _downKey:String){
super(X, Y);
makeGraphic(20, 20, FlxColor.RED);
immovable = true;
}
In PlayState.hx:
override public function create():Void
{
super.create();
add(new Enemy(300, FlxG.height - 20, 10, 20));
add(new Enemy(500, FlxG.height - 40, 10, 40));
add(player = new Player(60, FlxG.height - 40, "UP", "DOWN"));
}
It returns to me with the errors "Unknown identifier: upKey" and "Unknown identifier: downKey" in the Player.hx file, even after I already set those in the function. How do I fix this?
Function arguments are only available in that particular function (this is known as the scope of the variable) - so just because your constructor has arguments named upKey
and downKey
, that doesn't mean you can also automatically use them in another function like update()
.
To be able to do that, you need to save the arguments to member variables of the Player
class:
class Player extends FlxSprite
{
var upKey:String;
var downKey:String;
public function new(X, Y, upKey:String, downKey:String)
{
super(X, Y);
this.upKey = upKey;
this.downKey = downKey;
}
override public function update():Void
{
super.update();
trace(upKey, downKey);
}
}