Adam's Lair Forum

game development and casual madness
It is currently 2020/01/21, 13:11

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: 2015/05/29, 15:21 
Forum Adept
Forum Adept
User avatar

Joined: 2015/03/15, 02:49
Posts: 354
Location: Out there
Role: Hobbyist
Hi guys! I posted (or published?) an article on the wiki.
Here it is as well, for you to propose improvements.
Though my first article ever, shoot criticisms.

Quote:
Introduction

Very few games are made that don't need animations. Duality supports animations through a dedicated Component that quickly allows the creation of animations through spritesheets.

Overview

So, what is needed for an animation? A spritesheet. A spritesheet is nothing more than a sequence of images that when shown in sequence form an animation. So, let's just use this one (Downloaded from OpenGameArt, character created by Bevouliin):

Image

Save it somewhere on your computer...

And open Duality.

Drag and drop the spritesheet to the project view.

Looking to the right you see a few fields that relate to animation. In our example, you see Billy six times doing different things.

Image

Insert 3 in the AnimCols and 2 in the AnimRows. AnimFrameBorder is only if you have borders between the frames.

Create a new GameObject by right-clicking in the scene view and give it a name: Billy (I call it that) In Billy, add a new AnimSpriteRenderer Component.

Image

You will Duality's logo appearing. That's ok. Drag Billy's Pixmap over AnimSpriteRenderer and you'll see Billy in your scene.

Pressing play, you will see the full animation running, slowly.

Image

By tweaking the values in AnimSpriteRenderer, you can control the animation speed, when it starts, when it ends, if it starts automatically, etc.

Controlling Animations

So, knowing now how to create an animated character, we need to slice the spritesheet into the various segments, like walking, jumping, running, etc.

Our example has two segments that we must define so that our character doesn't just loop through every frame in the animation nonstop.

Now, let's bring Billy to life. Look closely at Billy's spritesheet and notice that the first 4 frames are of him running, and 2 are of him idle. It's important.

Now, we need to code a bit. Just open Visual Studio, rename YourCustomComponentType to BillyAnimation (for example, entirely optional).

I always implement ICmpInitializable and ICmpUpdatable in my components and then proceed to rearrange the methods, first OnInit(), then OnUpdate() and finally, OnShutdown().

Remove the "throw not Implemented Exception" line wherever you find it.

Then, write this on your OnInit() method:

Code:
if (context == InitContext.Activate)
{
     DualityApp.Keyboard.KeyUp+=Keyboard_KeyUp;
     PlayAnimation(4, 2);
}


So, what is going on here? This

Code:
if (context == InitContext.Activate)


just makes this particular block of code run when the game object is loaded, and only once.

Code:
DualityApp.Keyboard.KeyUp+=Keyboard_KeyUp;


Is an EventHandler we will implement so that when there is no key pressed, something will happen.

Code:
PlayAnimation(4, 2);


Is a method we will implement that will simply play the animation with the values we provide.

Here is the method:

Code:
private void PlayAnimation(int firstFrame, int frameCount)
{
     var animation = this.GameObj.GetComponent<AnimSpriteRenderer>();
     animation.AnimFirstFrame = firstFrame;
     animation.AnimFrameCount = frameCount;

}


As you can see, he have a very simple routine to play an animation as we please. Because we defined this on on startup if you build your solution and add the component to Billy and run your game, you'll see Billy, animated, ready to go.

Add this to the rest of your class:

Code:
public void OnUpdate()
{
    if (DualityApp.Keyboard[Key.Right])
    {
        PlayAnimation(0, 4);
    }
}

void Keyboard_KeyUp(object sender, KeyboardKeyEventArgs e)
{
     PlayAnimation(4, 2);
}


Build, and when pressing Right arrow key on your keyboard, you'll see Billy running. By releasing the key, he'll just stay in the idle position.

This is just an example of going about this.

_________________
Hello there! Nice to meet you!
I have github. Apologies in advance for bad coding practices.


Top
 Profile  
 
PostPosted: 2015/06/01, 11:12 
One-Hit Wonder
One-Hit Wonder

Joined: 2015/06/01, 09:37
Posts: 1
Location: Philippines
Role: Gamer
Thank you so much for this guide! :D It is simple and easy to understand which is very helpful for a newbie like me :D

_________________
:D I'm a novice programmer and an aspiring Game Dev. :D


Top
 Profile  
 
PostPosted: 2015/06/01, 14:57 
Forum Adept
Forum Adept
User avatar

Joined: 2015/03/15, 02:49
Posts: 354
Location: Out there
Role: Hobbyist
Hey! A reply! Thank you. I'm glad it is useful. ^^

I'm in the process if cooking up a few more tutorials for common processes.

_________________
Hello there! Nice to meet you!
I have github. Apologies in advance for bad coding practices.


Top
 Profile  
 
PostPosted: 2015/11/08, 14:32 
One-Hit Wonder
One-Hit Wonder

Joined: 2015/11/07, 20:05
Posts: 1
Role: Observer
Thank you! I am just getting into working with Duality and this guide was very useful.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: MSN [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  
cron
Powered by phpBB® Forum Software © phpBB Group