Questions about Classes
Monkey Forums/Monkey Programming/Questions about Classes
| ||
| Couple of questions about classes. Can you determine the type of Class an object is?
Class Sprite
End Class
Class Player Extends Sprite
End Class
Class Enemy Extends Sprite
End Class
Global Objects:=New List<Sprite>
Function Initialise()
Objects.AddLast(New Player())
Objects.AddLast(New Enemy())
End Function
Function Test()
Initialise()
For Local sprite:Sprite = EachIn Objects
If (sprite = Player) Then
Else (sprite = Enemy) Then
EndIf
Next
End Function
Second, once you know what the object's Class is, can you convert to back to the base class? Local pl:Player = sprite I know the above doesn't work but is there some way for it. This stuff is certainly possible in .Net (as an example). |
| ||
| Hi, You need to downcast to determine the exact class of an object, eg: If Player(sprite) Print "sprite is a Player!" Else If Enemy(sprite) Print "sprite is an Enemy!" End |
| ||
| Mark, Ok - great. If I downcast, can I then access the methods/fields etc (yet to try) like so:
Player(sprite).Update()
Update: Ok the above method doesn't appear to work, but you can reference the object first:
Local pl:Player = Player(sprite)
pl.Update()
Nice! Was hoping (and thought) this sort of thing was possible. The shortcut above would be great, but as long as it works no probs |