Created
December 6, 2016 12:57
-
-
Save codetravis/e8308c25cdd1e3927f24f5f22052605c to your computer and use it in GitHub Desktop.
7.2 Modify the character class to only respond to controls if it is a player character
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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