06. Anim (Animation)
Anim class
Ok, time to make some movement in our screen.
An Anim object let you create an animation from many pictures, so the purpose is that you have many pictures stored somewhere and you will make them shown one by one.
Anim anim1 = new Anim(manager);
manager.BackgroundLayer.Add(anim1);
You can specify Interval property in millisecond that each asset stay shown before switch to the next asset.
There is two ways, eather use a Interval property of anim1 object or use a constructor overload.
Anim anim1 = new Anim(500, manager);
manager.BackgroundLayer.Add(anim1);
Untill now or anim object is empty and does not store the assets sequances, so here is how to add some pictures
anim1.AddCell() with 13 overloads, let's see all params inside the constructor.
Asset is a path of the picture
id is an int number to stack the picture piled one over the other
PosX X position
PosY Y position
Width
Height
Opacity float value, it's the transparency (0F = invisible, 0.5 = 50% half visible, 1F = visible/opaque 100%)
interval time the picture will remain on the screen.
anim1.AddCell(@"c:\asset1.png", 0, 0, 0); // first asset with id = 0, position x =0 & y =0
anim1.AddCell(@"c:\asset2.png", 1, 0, 0); // second asset id = 2
anim1.AddCell(@"c:\asset3.png", 2, 0, 0); // third asset id = 3
Next step is to initialize the object with ini() method that has 4 overloads
anim1.Ini(Manager.TypeGfx.Object, false);
I precise that event we know where to add our object into a specific drawing layer, we still need to store a reference for that information that the object itself know where it is.
We do that with the first argument Manager.typeGfx in the constructor.
Second argument is to precise if the picture is encrypted or not.
anim1.Ini(Manager.TypeGfx.Object, new Rectangle(new Point(0, 0), new Size(50, 60)), false);
This overload is intended to show a picture inside a picture.
Have'nt you seen before a big picture with many stuff inside ?
We call this a SpriteSheet
Size is the size you want to take from the original picture, make attention that this overload is not used to show the original format of the picture as it is.
It's handy when we define all assets inside one picture, and then we just need to crop our image from the original one to the specified rectangle and add it to the sequances.
We will see how to use a single picture with many peace inside later.
IMPORTANT, Ini() method should be placed AFTER adding images with anim1.AddCell, not before, otherwise an exception will raise.
And finally start the animation with 'anim1.Start();'
anim1.Ini(Manager.TypeGfx.Obj, "anim1");
Here is an example of 4 different pictures 'not a spriteSheet'
Manager manager = new Manager(this, "CORE_ENGINE");
manager.Background = Color.Blue;
Anim anim1 = new Anim(100, manager);
player.AddCell("narutoStep1.png", 0, 0, 0);
player.AddCell("narutoStep2.png", 1, 0, 0);
player.AddCell("narutoStep3.png", 2, 0, 0);
player.AddCell("narutoStep4.png", 3, 0, 0);
player.Ini(Manager.TypeGfx.Object, "naruto", false);
player.Bmp.Point = new Point(100, 200);
manager.ObjectLayer.Add(anim1);
anim1.Start();
Anim Class has many parameters like :
AutoResetAnim To play the animation only one time and keep showing last frame "no loop", if you want to hide the last frame, just combine this line with :
anim1.HideAtLastFrame = true;
AutoResetTimer To stop playing the animation and showing the first frame.
anim1.AutoResetTimer = false;
Stop playing animation in the last frame and hide it, notice that both command should combined
anim1.DestroyAfterLastFrame = true;
anim1.AutoResetAnim = false;
Get the Number of current frame
int currentFrame = anim1.counter;
Encrypting index for the image :
int encryptIndex = anim1.Crypt;
Check if anim bitmaps are encrypted or not :
bool crypted = anim1.Crypted;
Pointer to the current Bmp frame:
Bmp currentFrame = anim1.img;
Modifying interval of changing image to make it fast or slower:
anim1.Interval = 50;
It's possible to give a point of each frame of the animation, but for some circumstances, you want that all frames use the same first point given, no matter what the frame has a different coordinate, it'll keep only the coordinate given to the first frame, this value can change during animation depending on logic using:
anim1.PointOfParent = true;
To show a reversed animation, i mean the frames will be started from a reversed order, using counter variable
anim1.Reverse = true;
Want me to dev an app for you ? dont hesitate to contact me.
Are you looking for a coder/teammate for your project ? Let's give it a try.
Have a proposal for me ? we can discuss about it.
You want to buy me a coffe ? m.elharfi@gmail.com