Clearing a target can be very handy on objects with very simple behaviours. I recently had a requirement where an object would perform an animation every time another object collided with it.
As per other tutorials, my code called orxObject_SetTargetAnim and away it animates. However, because the object is not under any kind of input control, there is no opportunity to clear the target, and therefore, the object will animate over and over.
This is where the cleartarget property becomes useful.
Let's work through an example to see how it works
See below the following spritesheet of a spring loaded bumper.
The job of this bumper is to spring up and knock away any objects that collide with it. After it has sprung up, it needs to compress down again and stay compressed until the next collision.
First, make the object, graphics, and animations (body collision information left out):
[BumperObject] Graphic = BumperGraphic AnimationSet = BumperAnimationSetAll Position = (100, 100, -0.1) [BumperGraphic] Texture = bumper.png Pivot = top left TextureSize = (114, 117, 0) [BumperGraphic1@BumperGraphic] TextureOrigin = (0, 0, 0) [BumperGraphic2@BumperGraphic] TextureOrigin = (114, 0, 0) [BumperGraphic3@BumperGraphic] TextureOrigin = (228, 0, 0) [BumperGraphic4@BumperGraphic] TextureOrigin = (342, 0, 0) [BumperGraphic5@BumperGraphic] TextureOrigin = (456, 0, 0) [BumperGraphic6@BumperGraphic] TextureOrigin = (0, 117, 0) [BumperGraphic7@BumperGraphic] TextureOrigin = (114, 117, 0) [BumperGraphic8@BumperGraphic] TextureOrigin = (228, 117, 0) [BumperGraphic9@BumperGraphic] TextureOrigin = (342, 117, 0) [BumperGraphic10@BumperGraphic] TextureOrigin = (456, 117, 0) [BumperGraphic11@BumperGraphic] TextureOrigin = (0, 234, 0) [BumperGraphic12@BumperGraphic] TextureOrigin = (114, 234, 0) [BumperGraphic13@BumperGraphic] TextureOrigin = (228, 234, 0) [BumperGraphic14@BumperGraphic] TextureOrigin = (342, 234, 0) [BumperGraphic15@BumperGraphic] TextureOrigin = (456, 234, 0) [BumperGraphic16@BumperGraphic] TextureOrigin = (0, 351, 0) [BumperGraphic17@BumperGraphic] TextureOrigin = (114, 351, 0) [BumperGraphic18@BumperGraphic] TextureOrigin = (228, 351, 0) [BumperGraphic19@BumperGraphic] TextureOrigin = (342, 351, 0) [BumperGraphic20@BumperGraphic] TextureOrigin = (456, 351, 0) [BumperAnimationSetAll] AnimationList = BumperCompressedAnim #BumperSpringUpAnim #BumperCompressAnim LinkList = BumperCompressedLoop # BumperDownToUp #BumperUpToDown #BumperCompressingToCompressed [BumperCompressedAnim] DefaultKeyDuration = 0.1 KeyData1 = BumperGraphic20 [BumperSpringUpAnim] DefaultKeyDuration = 0.008 KeyData1 = BumperGraphic20 KeyData2 = BumperGraphic19 KeyData3 = BumperGraphic18 KeyData4 = BumperGraphic17 KeyData5 = BumperGraphic16 KeyData6 = BumperGraphic15 KeyData7 = BumperGraphic14 KeyData8 = BumperGraphic13 KeyData9 = BumperGraphic12 KeyData10 = BumperGraphic11 KeyData11 = BumperGraphic10 KeyData12 = BumperGraphic9 KeyData13 = BumperGraphic8 KeyData14 = BumperGraphic7 KeyData15 = BumperGraphic6 KeyData16 = BumperGraphic5 KeyData17 = BumperGraphic4 KeyData18 = BumperGraphic3 KeyData19 = BumperGraphic2 KeyData20 = BumperGraphic1 [BumperCompressAnim] DefaultKeyDuration = 0.025 KeyData1 = BumperGraphic1 KeyData2 = BumperGraphic2 KeyData3 = BumperGraphic3 KeyData4 = BumperGraphic4 KeyData5 = BumperGraphic5 KeyData6 = BumperGraphic6 KeyData7 = BumperGraphic7 KeyData8 = BumperGraphic8 KeyData9 = BumperGraphic9 KeyData10 = BumperGraphic10 KeyData11 = BumperGraphic11 KeyData12 = BumperGraphic12 KeyData13 = BumperGraphic13 KeyData14 = BumperGraphic14 KeyData15 = BumperGraphic15 KeyData16 = BumperGraphic16 KeyData17 = BumperGraphic17 KeyData18 = BumperGraphic18 KeyData19 = BumperGraphic19 KeyData20 = BumperGraphic20 [BumperCompressedLoop] Source = BumperCompressedAnim Destination = BumperCompressedAnim Priority = 9 [BumperDownToUp] Source = BumperCompressedAnim Destination = BumperSpringUpAnim Property = immediate [BumperUpToDown] Source = BumperSpringUpAnim Destination = BumperCompressAnim [BumperCompressingToCompressed] Source = BumperCompressAnim Destination = BumperCompressedAnim
So that completes the setup. If in code, the spring bumper animation is activated with:
The bumper target is being set to BumperSpringUpAnim which means that the animation graph system will always seek it constantly. BumperSpringUpAnim will always remain the target, and therefore be seeked, until it is somehow cleared.
In code, if the object was under keyboard control, you could issue the following on keyup to clear a target from an object:
But our object is not cleared from code. We want it to clear after our target animation has completed.
Note that when the object is first initialised, “BumperCompressedAnim” is the animation that will play. This is because it appears first in the AnimationList.
So in the present state, setting the target to “BumperSpringUpAnim”, the animation graph will take the object through the following chain of animations via the Loops trying to get to “BumperSpringUpAnim”:
See how it is caught in a loop?
The secret is to change the BumperCompressingToCompressed loop to include Property = cleartarget:
[BumperCompressingToCompressed] Source = BumperCompressAnim Destination = BumperCompressedAnim Property = cleartarget
The same behaviour will apply. But this time, after “BumperSpringUpAnim” has played, the graph system will seek to find its way around to play it again. As part of that, BumperCompressedAnim is played via the BumperCompressingToCompressed loop. And while doing this, the target is cleared with cleartarget.
The object will no longer have a target, and BumperCompressedAnim will be the last animation to play.
That is until the object is given an animation target again.
This is a very handy property for objects that animate only occasionally.