ads1

четверг, 10 декабря 2015 г.

Introduction to Animation spritesheet

Animation based on spritesheet'ah used in games for quite some time. Including the popular games such as Legend of Zelda: A Link to the Past or Cut the Rope. In this article we will talk about how this works and how its animation program. As a programming language will be using JavaScript, but you can choose any convenient for you.
Before we talk about how to program spritesheet animation, we need to define three terms: animation, and sprite, indeed, spritesheet.

Animation

Back in 1872 the British and American photographer Eadweard Muybridge was commissioned to find out if the horse raises all its 4 legs while running. Meybridzh for that use multiple cameras to take photos of a horse with a rider ran past. As a result, the photographer got 16 images horse. By the way, in a couple of them are really four feet in the air.
Muybridge_race_horse_sheet
Later Meybridzh repeated the experiment, but put all the photos in a device that could project them in rapid succession so that the resulting illusion of running horses. This device later called the film projector.
Muybridge_race_horse_animated
How to quickly change the images to create the illusion of movement called animation.

Sprite

Sprite is a graphical representation that is placed on the big stage. That is, the sprite is part of the scene.
sprite
Sprites are a popular way to create a large and complex scenes, as you can manipulate and control each image individually. This allows better control of what is happening on stage.
It is not uncommon in the game, there are dozens, even hundreds of sprites. Loading each of them as an individual and self-image will consume a lot of memory and computing power. In order to avoid these inconveniences and used spritesheet'y.

Spritesheet'y

When you put a lot of sprites into a single image, you get spritesheet.
spritesheet
Spritesheet'y used to accelerate the process of displaying images on the screen. It is much more efficient and faster upload one big picture and show it to the part you download a lot of sprites and show them.
Animation based on spritesheet, is nothing more than an ordinary spritesheet, which is not shown entirely, and frame by frame. And these images change with a sufficiently high velocity.The principle of this animation is similar to the principle of the film projector, created Meybridzhem.

Part spritesheet'a

Spritesheet itself consists of two parts: a frame and cycles.
A frame is a normal picture (or sprite). For example, a horse Meybridzha: each image of a horse across a frame animation.
If you put the pictures in the right order, you get a continuous movement. That is the cycle.

Programming spritesheet animation

There are 3 stages of creating spritesheet animation:
  1. Create an image.
  2. Update image for each frame of animation.
  3. Drawing animation on the screen.

Create an image

Start by creating a function (or class) that will handle our spritesheet animation. This function will create an image and establish its way to ensure that we can use it for further drawing on the screen.
Since spritesheet'y may have different sizes, knowing the size of one frame, we will know the number of shots in a row and column.
It is important that each frame of animation has the same size, otherwise the animation will not genuine.

Update image

To update spritesheet animation, we need to change the picture that is drawn at the moment.Below spritesheet divided into frames, each of which is numbered.
spritesheet_numbered
To change the current frame to the next need not immediately, but after some time. That is, when the show of one of the frames you want to make some delay before you change it.
It is important to note that not everyone is available to sprite spritesheet in each of his shots.For example, in spritesheet'e Meybridzha 16th sprite offline. If we did not consider this, the animation would have been a flash.
Avoid spikes easily: we will not draw these empty picture:
If you use an operation to take the remainder of the division (%) for currentFrame we create a continuous cycle of numbers from 0 to endFrame. Thus, we reproduce only those frames, images for which we have in spritesheet'e.

Drawing animation

We calculate the row with the required overs, taking the remainder from dividing the number of the current frame by the number of frames in a row. Also, we calculate the column with the desired frame by dividing the number of the current frame by the number of frames in a row.
Using the obtained values ​​we can find the coordinates of the framework within which the frame is required:
Now we are ready to create the animation:

Several cycles in one spritesheet

The above code will work for any spritesheet'a containing exactly one cycle. However, there are several cycles spritesheet'y (animation).
To work with multiple animations in one spritesheet'e, we need to change our principle.

Create an image

To store information about the image and its size:

Updating and drawing animation

Feature Animation () will be responsible for updating and drawing pictures:
Since spritesheet comprises a plurality of frames for the different animations, we need to know the number of frames the animation which we now want to play.

Afterword

We wrote a simple game-runner, using the most animated, as discussed in the article. The source code presented in this article, you can see by clicking on thislink.

Комментариев нет:

Отправить комментарий