Getting started with the j2_2d games (2) improving the surrounding tools

Source: Internet
Author: User

2. Improve peripheral tools (images, GameObject, and Font)
Although we have the support of midp2.0, sometimes we still need some auxiliary tools for our convenience. This is perhaps the most interesting before a real game design.
1. First, it is an ImageTools tool class that provides a method to help call the Image
Public class ImageTools {
Protected ImageTools (){
}
Public static Image getImage (String str ){
Image img = null;
Try {
Img = Image. createImage (str );
}
Catch (Exception ex ){
System. out. println (ex );
}
Finally {
Return img;
}
}
}
2. GameObject: provides a common game object.
With the Sprite class, why do we need GameObject? In fact, we generally regard Sprite as an advanced Image. A Sprite is often called by multiple game objects, and GameObject is actually a Sprite state class. GameObject provides a simple concept of lifecycle and animation update speed;
Public class GameObject {
Public Sprite sprite; // built-in Sprite
Public boolean alive; // survival tag
Private int lifecount = 0; // life cycle counter
Public int lifetime = 0; // life cycle, in bytes
Public int speed = 0; // The animation refresh speed. (0 to infinity. 0 indicates that each record is followed by a new screen)
Private int animcount = 0; // The animation counter is updated.
Public GameObject (Image img, int width, int height ){
Sprite = new Sprite (img, width, height );
Reset ();
}
Public void move (int dx, int dy) {// relative movement
Sprite. move (dx, dy );
}
Public void moveto (int x, int y) {// absolutely move
Sprite. setPosition (x, y );
}
Public void update () {// update Status, animation lifecycle update, and lifecycle update
If (! Alive)
Return;
If (++ animcount> speed ){
Animcount = 0;
Sprite. nextFrame ();
If (lifetime! = 0 & ++ lifecount> lifetime)
Alive = false;
}
}
Public void paint (Graphics g) {// Paint
If (! Alive)
Return;
Sprite. paint (g );
}
Public void reset () {// reset
Alive = true;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.