Game Development Community

T2dAnimatedSprite animationdata swapping

by David Everhart · in Torque X 2D · 01/26/2008 (8:01 pm) · 1 replies

I just recently got my animation system working pretty well. It can create an on the fly T2DAnimationData based upon the players current configuration, so if I change direction it automatically pulls 60 frames, merges them into one strip with the appropriate texture divider.

Now here is the problem, and this may be a conceptual one on my part. I have been trying to use one animated sprite for my Player (custom object) and swap out the T2DAnimationData on the fly, but the new animation is not showing. The newly created T2DAnimatedDatas are being popped into the sprite , and then called as such:

public void UpdateAnimation()
        {
            // Get the AnimationConfiguration
            AnimationConfiguration configuration = GetAnimationConfiguration();

            // Get the T2dAnimationData
            T2DAnimationData animationData = AnimationManager.Instance.CreateT2DAnimationData(configuration, ImageFormat.Png);

            // Set the new animation data
            _sprite.AnimationData = animationData;

            // Intialize it
            _sprite.AnimationData.Init();

            // Check to see if it is registered or not. If its not, it a first time initialization
            if (!_sprite.IsRegistered)
            {
                // Setup our Sprite
                TorqueObjectDatabase.Instance.Register(_sprite);
                T2DSceneCamera currentCamera = (T2DSceneCamera)TorqueObjectDatabase.Instance.FindObject("Camera");
                currentCamera.Mount(_sprite, "", true);
            }

            // Play the new animation
            _sprite.PlayAnimation();
        }

I checked the tutorials, and it looks like it is creating a new animatedsprite every time. Is that a best practice for Torque? I can adjust my code to do that , but that seems like more overhead ,versus keeping one in memory and simply swapping the animated datas. Thoughts?

#1
02/02/2008 (9:58 pm)
Ok, after doing some testing and research, I was able to get it working. I didnt check the scenegraph, that was what was causing my animations to not show. After reworking it, I had this below which works just fine, using one T2DAnimatedSprite (_sprite) and on the fly animationdatas.

public void UpdateAnimation()
        {
            DateTime startTime = DateTime.Now;
            Console.WriteLine("UpdateAnimation::Start Time is " + startTime.ToLongTimeString());

            _sprite = TorqueObjectDatabase.Instance.FindObject<T2DAnimatedSprite>("PlayerAnimatedSprite");

            if (_sprite.SceneGraph == null)
            {
                T2DSceneGraph currentSceneGraph = TorqueObjectDatabase.Instance.FindObject<T2DSceneGraph>("DefaultSceneGraph");
                _sprite.SceneGraph = currentSceneGraph;
            }
           
            T2DSceneCamera currentCamera = (T2DSceneCamera)TorqueObjectDatabase.Instance.FindObject("Camera");
            // Get the Current Camera
            if (currentCamera.MountedTo == null)
            {
                currentCamera.Mount(_sprite, "", true);
            }
          

            // Get the AnimationConfiguration
            AnimationConfiguration configuration = GetAnimationConfiguration();

            // Make sure we have 4, if we dont, then the AnimationConfiguration is not valid
            if (configuration.AnimationLayers.Count == 4)
            {
                // Get the T2dAnimationData
                T2DAnimationData newAnimationData = AnimationManager.Instance.CreateT2DAnimationData(configuration);
                // Play the new animation
                _sprite.PlayAnimation(newAnimationData);
            }
            

            DateTime endTime = DateTime.Now;
            Console.WriteLine("UpdateAnimation::End Time is " + endTime.ToLongTimeString());
            TimeSpan timeToRun = endTime.Subtract(startTime);
            Console.WriteLine("UpdateAnimation::Time to run is " + timeToRun.TotalMilliseconds.ToString());
        }