Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
6.6 adding boundary checks to player movement so they can't leave the screen
' character.monkey
Method Update(game_width:Float, game_height:Float)
Local x_speed:Float = Self.box.GetSpeedX()
Local y_speed:Float = Self.box.GetSpeedY()
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment