little thing with life.

Monkey Forums/Monkey Programming/little thing with life.

Paul - Taiphoz(Posted 2012) [#1]
[monkeycode]
If Self.lifebit>1

If Self.life>= Self.lifebit*2
Self.frame=0
Else If Self.life>=Self.lifebit And Self.life<=Self.lifebit*2

Self.frame=1
Else If Self.life<=Self.lifebit
Self.frame=2
End If

End If
[/monkeycode]

In a nut shell I am changing the appearence of my sprite based on its life , given the fact that each alien might have different amounts of life.

At the moment as the player damages down a boss the sprite changes 3 times to reflect the amount of damage done , but I now want to catch the point it actually switches frames so I can emphasis it a bit by throwing off some particles.

I know a way of doing it already but its not as nice as I would like and this bit of code just dont look right to me anyway so thought I would share.


matt(Posted 2012) [#2]
keep track of the previous/old value of Self.frame, compare the old and current values and if they differ then trigger the particles.


Paul - Taiphoz(Posted 2012) [#3]
yeah that's exactly what I was going to do, but it was when I went to type that into the space after the first else if that I realized I was setting that frame value every single loop.

And it only really needs set once each change. I know I'm probably on going to shave a tiny amount of performance out of it but.. still!. lol


ziggy(Posted 2012) [#4]
Use a Property. As simple as it gets. When the value of the Property is modified, act accordingly. That what properties are for.
Quick sample:
[monkeycode]Function Main()
Local a:StatusItem
a.Status = 354 'The Setter is called.
Print a.Status 'The Getter is called.
End

Class StatusItem

'This is the getter:
Method Status:Int() Property
Return _status
End

'This is the setter
Method Status:Void(value:Int) Property
if value<>_status Then
'It has changed.
'action here!!!
EndIf
_status = value
End

Private
Field _status:int
End[/monkeycode]