• Animation

    Animations are one of the most important game items, don't you think?
    The JPlay has a specific class to handle the animations.
    This class is called Animation. Isn't this a suggestive name?

    1 - How to create an animation?

    To create an animation we need one image which has to contain some frames. The number of frames is your choice.

    A frame is a piece of the image, responsible for the movement of the animation.

    Example: we have the image below:
    MegaMan

    Note that there are four drawings of the Megaman in a single image. Such that each one of these drawings can be called a frame. Therefore, this image has four frames.

    The concept of frame is very important in animation, keep it in mind.

    2 - Instantiating the class Animation.

    The Animation class has three constructors.

    To create an object of this class, we proceed as follows:

    3 - Setting the time between the change of frames.

    In animation, frames must change after a certain time. To inform the time between the change of frames or the time in which each frame will be displayed on the screen, we use the method:


    Example:

    If we have 4 frames and the total time is 1000 milliseconds, then each frame will be displayed on the screen during 1000 / 4 = 250 milliseconds,

    Note: Remember that 1 second equals to 1000 milliseconds, 1s = 1000ms and that is always necessary to set the total duration of the animation.

    4 - Running the animation.

    To make the animation run, we use the method below that is responsible for the change of frames:

    Up until now we have:
    Animation animation = new Animation( "animation01.png ", 4);
    animation.setTotalDuration(125);
    animation.update();

    We are ready to create our first animation.

    Example: Running an animation.

    5 - Animating a subset of frames

    To animate a subset of frames we can use the methods:

    If we had an image with 28 frames and execute animation.setSequence(0,13), the frames to be used in the animation will be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.

    However if we execute animation.setSequence(14, 27) the frames to be used in the animation will be 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 25, 25, 26, 27.

    Example: running a subset of frames.

    Example: Accelerate or decelerate the execution of the frames.

    6 - Changing the frames manually.

    To set which frame to be draw we use the method:

    Note: If you are using this method it is not necessary to set the duration of the animation.

    Example: Changing the frames manually.

    7 - Setting a time for each frame.

    In some animations certain part of the animation must be executed faster or slower than other part. For this, there is the following method:

    The parameters are: the number of the frame and the value of the duration time in which the frame must be shown.

    Example: Frames with different change times.

    8 - Hiding the animation.

    Note: Even if the animation is not shown on the screen, if the update() method is being called, the frames will continue to be changed.

    9 - Other methods.

    The Animation class has no methods that make the animation move across the screen. For this you can use the public variables 'x' and 'y'.

    UFF - Universidade Federal Fluminense - Institudo de Computação - Ciência da Computação