Game Development Community

object creation from code?

by jerryg7rk3w · in General Discussion · 09/08/2011 (9:05 pm) · 3 replies

I was wonder how do you spawn an animation through code without using datablocks?
I know it sounds simple but if you look at the code below ill clarify what the problem is.

function testObject(%object)
{
%object = new t2danimatedSprite() // <===== This seems to be the problem
{

layer = 0;
size = "5 5";
position = "-17 -16";


imagemap = "testanimatedsprite"; //<===== And this seems to be the problem
};
sceneWindow2D.getSceneGraph().addtoScene(%object);
}

For some reason i can spawn t2dstaticsprites using the same code above but can't spawn animated ones.
So could anyone give me some advice on this and point out the code i might be missing, thanks!

#1
09/09/2011 (6:12 am)
I just replied to your other thread. You need to use a t2dAnimationDatablock for it to work, otherwise the t2dAnimtedSprite does not know what animation or imageMap to use.
#2
09/09/2011 (12:50 pm)
Thanks!, sorry about the double post in the other thread man but after about a week i thought the other guy who was going to help gave up.

Also i don't remember the documentation saying that datablocks are required for animated sprites, I may have skimmed over it but don't recall seeing it.
I just want to fully understand the scripting language, so thanks again for the advice couldn't do it without some help.
#3
09/09/2011 (4:31 pm)
I'm happy to help. Sorry this became a confusing issue for you. When it comes to creating an object dynamically through script, there is a general rule of thumb: create it the way the editor does. For example, this is EXACTLY how the editor saved the level when I added a t2dAnimatedSprite into the scene:

new t2dAnimatedSprite() {
   animationName = "dbQuarter_ipadAnimation";
   canSaveDynamicFields = "1";
   PlatformTarget = "UNIVERSAL";
   Position = "373.493 -40.360";
   size = "256.000 256.000";
   CollisionMaxIterations = "3";
   AlphaTestValue = "-1";
      mountID = "12";
};

Generally, that is what you want to write in your object creation code. The only difference is setting the values you want. Common dynamic properties are positions and datablocks.

new t2dAnimatedSprite() {
   animationName = %deathBallAnim; // Dynamic
   canSaveDynamicFields = "1";
   PlatformTarget = "UNIVERSAL";
   Position = %spawnPosition; // Dynamic
   size = "256.000 256.000";
   CollisionMaxIterations = "3";
   AlphaTestValue = "-1";
      mountID = "12";
};