Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tutorial on using Java Sprite Animation

Similar presentations


Presentation on theme: "Tutorial on using Java Sprite Animation"— Presentation transcript:

1 Tutorial on using Java Sprite Animation
Sagar Patel

2 Agenda Introduction Basic Sprite Animation Sprite Structure
Object Structure Defining Action Functions Programming Difficulties Examples and ScreenShots 12/4/2018

3 Introduction The idea of sprite animation is simple.
Draw the object in a position. Wait a while. Draw the object in a new position. Go back to second step. 12/4/2018

4 Introduction (cont’d)
A sprite animation differs from traditional video animation. Using the metaphor of a sprite animation as a theatrical play, sprite tracks are the boundaries of the stage, a sprite world is the stage itself, and sprites are actors performing on that stage. Each sprite has properties that describe its location and appearance at a given time. 12/4/2018

5 Introduction (cont’d)
During the course of an animation, you modify a sprite’s properties to cause it to change its appearance and move around the set or stage. Each sprite has a corresponding image and during the animation, you can change a sprite’s image. For example, you can assign a series of images to a sprite in succession to perform cell-based animation. Sprites can also be mixed with still-image graphics to produce a wide variety of effects while using relatively little memory. 12/4/2018

6 Basic Sprite Animation
A simplistic sprite would not be very interesting in a modern computer game. In modern games, we want to look at sprites that have a wider range of motion: a hero sprite, for example, who runs, jumps, kicks, and shoots; or an enemy robot sprite that rolls, bumps into walls, and emits electrical charges. In order for a game to be competitive in the current market, the sprite animation needs to be creative and sophisticated. Maintaining control of such sophisticated sprite action can be a challenge. 12/4/2018

7 Sprite Structure The basic building block of the sprite is the sprite structure, which is defined like this in the C language: /* sprite structure */ typedef struct _sprite {   char *bitmap;   int width;   int height; int xoffset;   int yoffset; } SPRITE; 12/4/2018

8 Sprite Structure (cont’d)
This structure holds the information necessary to display the sprite, including its width and height, the bitmap that defines the image, and the offset values. The offsets are used to adjust the position of the sprite, and are useful with things like explosions, which must be centered around a midpoint, rather than displayed from a corner. 12/4/2018

9 Object Structure The sprite structure is a member of the object structure, which is defined like this: /* forward declarations */ struct OBJstruct; typedef struct OBJstruct OBJ, near *OBJp; /* pointer to object action function */ typedef void near ACTION (OBJp objp); typedef ACTION *ACTIONp; 12/4/2018

10 Object Structure (cont’d)
/* data structure for objects */ typedef struct OBJstruct {   OBJp next; OBJp prev;   int x,y;   int xspeed,yspeed;   int max_xspeed;   int direction;   int frame;   int tile_xmin,tile_xmax;   int tile_ymin,tile_ymax; SPRITE *image;   ACTIONp action; }; 12/4/2018

11 Object Structure (cont’d)
This data structure contains all the information about a particular object in the game, including its position (x and y coordinates), its vertical and horizontal speed, the direction it is facing, the frame of animation (for example, which stage of a six-stage walk), 12/4/2018

12 Object Structure (cont’d)
the tile extents (how close the sprite may approach the edge of the screen), and a pointer to the sprite image, which was defined earlier. The sprite image changes as the sprite moves, and may represent a sprite as walking, running, or shooting, for example. 12/4/2018

13 Defining Action Functions
The last member of the object structure is the pointer to the action function, which is where all the interesting work takes place. The action function is a function which is executed once each frame for each sprite. It performs several tasks. It causes the sprite to move (by changing the object's x and y coordinates), it checks for collisions, it may spawn new objects or kill off old ones, but most importantly, the action function determines what the object will do in the next frame. It does this by specifying the next action function. 12/4/2018

14 Defining Action Functions (cont’d)
Here is an example of an action function in its simplest form: void near sprite_stand(OBJp objp) {   if (fg_kbtest(LEFT_ARROW))     objp->action = sprite_walk; } This is the action function for a sprite standing still. As you can see, the sprite does nothing. Its x and y coordinates do not change, and its sprite image does not change. 12/4/2018

15 Defining Action Functions (cont’d)
Also, as long as no key is pressed, the sprite's action function does not change. As long as the sprite continues to stand still, this action function will continue to be called once each frame. The state of the sprite changes from standing to walking when the left arrow key is pressed. When this happens, the sprite_stand() function is abandoned, and the sprite_walk() function takes over. This transition happens very simply: a pointer in the object structure is reassigned to point to a new action function. 12/4/2018

16 Programming Difficulties
The difficult part in programming sprite animation is deciding what should go in the action functions. Since a sprite can do more than one thing at a time (shooting while jumping, for example), the programmer must make decisions about which action function should be called. The choice would be calling the sprite_shoot() function, with the jumping action being an incidental action happening within the shooting function, or calling the sprite_jump() function, with the shooting action incidentally happening within the jumping function. 12/4/2018

17 Examples and Screen Shots
In this section we are going to see two examples of Sprite Animation. You can also see the examples and run the applet on the following website: 12/4/2018

18 Examples and ScreenShots (cont’d)
Example 1: A sprite animation which moves a rectangle along a diagonal line with a scrollbar for determining the delay between frames. The default delay is set to 100. See the screen shots of this example in the next slide. 12/4/2018

19 Examples and ScreenShots (cont’d)
12/4/2018

20 Examples and ScreenShots (cont’d)
Example 2: A sprite animation applet which allows the user to control the speed of the object by using up and down arrow keys, thus resulting in either of the two stages: crashing or landing safe. Fuel is set to a value of 50 which means when the user presses the up arrow key, the fuel starts decreasing. Speed increases as the object moves towards bottom. See the screen shots of this example in the next slide. 12/4/2018

21 Examples and ScreenShots (cont’d)
12/4/2018

22 Examples and ScreenShots (cont’d)
12/4/2018

23 References Xiaoping Jia “Object-Oriented Software Development Using Java”, Second Edition, Copyright 2003 A Gentle Introduction to Java Programming, Object Oriented Systems A Simple Animation, 12/4/2018


Download ppt "Tutorial on using Java Sprite Animation"

Similar presentations


Ads by Google