playMaker

Author Topic: 2d toolkit play animation with events  (Read 4555 times)

msc4tech

  • Playmaker Newbie
  • *
  • Posts: 17
    • View Profile
    • http://www.msc4tech.com
2d toolkit play animation with events
« on: December 06, 2011, 03:12:49 AM »
this code for play animation and receive trigger event (with parameter) and End animation event

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
   [ActionCategory("2D Toolkit")]
   [Tooltip("Plays a sprite animation on a tk2dAnimatedSprite")]
   public class PlayAnim : FsmStateAction {
      [RequiredField]
      public FsmOwnerDefault gameObject;   
      public FsmString clipName;
      public bool enableTriggerEvent;
      public FsmEvent TriggerEvent;
      public bool enabelEndEvent;
      public FsmEvent EndEvent;
      
      private GameObject go;
      private tk2dAnimatedSprite spriteAnim;
      
      public override void Reset() {
         gameObject = null;
         clipName = null;
      }
      
      public override void OnEnter() {
         go = gameObject.OwnerOption == OwnerDefaultOption.UseOwner ? Owner : gameObject.GameObject.Value;
         spriteAnim = go.GetComponent<tk2dAnimatedSprite>();
         if (spriteAnim.isPlaying()==false)
         {
            spriteAnim.Play(clipName.Value);
            if (enableTriggerEvent)
               spriteAnim.animationEventDelegate= AnimationEventDelegate;
            if (enabelEndEvent)
               spriteAnim.animationCompleteDelegate=AnimationCompleteDelegate;
         }
      }
      
   void AnimationEventDelegate(tk2dAnimatedSprite sprite, tk2dSpriteAnimationClip clip, tk2dSpriteAnimationFrame frame,int frameNum)
      {
         Fsm.EventData.IntData=frame.eventInt;
         Fsm.EventData.StringData=frame.eventInfo;
         Fsm.EventData.FloatData=frame.eventFloat;
         Fsm.Event(TriggerEvent);
      }
   void AnimationCompleteDelegate(tk2dAnimatedSprite sprite, int clipId)
      {
         Fsm.Event(EndEvent);      
      }
   }
   
}