Dream Games, RPG games open Source Development lectures (JAVA chapter) [3]--managernent __java

Source: Internet
Author: User
Tags thread class

The book is connected to the previous article. The last book mentions "Picture flicker problem and change of role action" is one of the two difficulties we face at present, this time, will solve the premise condition of picture flicker--change of role action, also namely "animation" Carry on more in-depth analysis.

We all know that the so-called animation is not a "will move the painting", but a group of "continuous changes in the painting", like the need for Flash when the "frame" to adjust the screen movement, in the development of Java games, like to use a similar way to control the screen.

To achieve this, we first need a continuous set of images.

The following figure:

In daily life, we rarely know whether we should take the left foot or the right foot, but for the computer, this is the condition that we must give him a definite hint. So, we also need a variable to act as a "pedometer" to clear the next step state.

Private int count;

and to achieve animation, the most important point is the continuous screen, that is, multi-step operation of the processing, we use the Java thread, that is, threads.

Private Thread Threadanime;

In Java, functions such as C # are not currently supported for direct invocation by threads. Java to implement threads, you must inherit the thread class or implement the Runnable interface.

Let's take the inheritance of the thread class as an example:

 

The inner class, which is used to handle the pedometer action.

Private class Animationthread extends Thread {

Public void Run () {

while (true) {

Count steps

if (count = 0) {

Count = 1;

Else if (count = = 1) {

Count = 0;

}

Redraw the screen.

Repaint ();

Change the action every 300 milliseconds.

Try {

Thread.Sleep (300);

Catch (Interruptedexception e) {

E.printstacktrace ();

}

}

}

}

 

The so-called inheritance can also be simply understood as the entire method of the inherited class under copy, and here we rewrite the run () method without changing the other. That is to say, we will run animationthread this class in our own way.

In addition, when dealing with the Drawrole method, we change the internal changes as follows:

The offset value of the image as Count

G.drawimage (Roleimage, X*cs, Y*cs, X*cs+cs, Y*cs+cs,

Count*cs, 0, Cs+count*cs, CS, this);

The derivation formula is as follows:

       

Finally, we start the thread at the beginning of the Mypanel construction, which makes the thread-related operations active.

Instantiate internal thread Animationthread

Threadanime = new Thread (new animationthread ());

Start thread

Threadanime.start ();

Mypanel The code is as follows:

package Org.loon.chair.example3;

import java.awt.Dimension;

import java.awt.Graphics;

import java.awt.Image;

import java.awt.event.KeyEvent;

import Java.awt.event.KeyListener;

import Javax.swing.ImageIcon;

import Javax.swing.JPanel;

/**

* Custom panel in Example3 to depict the underlying map.

*

* @author Chenpeng

*

* Loon Framework in Game

*

* PS: Please note that this is different from the previous precedent, new keyboard event monitoring

*/

Public class Mypanel extends JPanel implements KeyListener {

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.