Create a gist now

Instantly share code, notes, and snippets.

Embed
7.2 Modify the character class to only respond to controls if it is a player character
Import fantomX
Class Character
Field box:ftObject
Field is_player:Bool
Method New(box:ftObject, player:Bool=False)
Self.box = box
Self.is_player = player
End
Method Update(game_width:Float, game_height:Float)
Local x_speed:Float = Self.box.GetSpeedX()
Local y_speed:Float = Self.box.GetSpeedY()
If (Self.is_player)
If (KeyDown(KEY_LEFT))
Self.box.SetSpeedX(x_speed - 1.0)
Else If (KeyDown(KEY_RIGHT))
Self.box.SetSpeedX(x_speed + 1.0)
End
If (KeyDown(KEY_UP))
Self.box.SetSpeedY(y_speed - 1.0)
Else If (KeyDown(KEY_DOWN))
Self.box.SetSpeedY(y_speed + 1.0)
End
If (Self.box.GetPosX() > game_width)
Self.box.SetPosX(game_width-1)
Self.box.SetSpeedX(0)
Else If (Self.box.GetPosX() < 0)
Self.box.SetPosX(1)
Self.box.SetSpeedX(0)
End
If (Self.box.GetPosY() > game_height)
Self.box.SetPosY(game_height-1)
Self.box.SetSpeedY(0)
Else If (Self.box.GetPosY() < 0)
Self.box.SetPosY(1)
Self.box.SetSpeedY(0)
End
End
End
End
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment