How to Use the sequence of three genie frames in the north education learning instance

Source: Internet
Author: User

Use the sprite class in the game package in midp2.0 to move people in the upper, lower, and left directions. The main function is to classify each group of actions of a character into a frame sequence array and set the corresponding frame sequence Array Based on the current state of the character. Recyclically switches the frames in the sequence array of each frame, changes the coordinates of the genie, and draws the frames on the screen.

 

PS: The images used in the instance are taken from the network. If there is any infringement, please let us know.

 

:

 

 

 

The MIDlet class is omitted. The spritecanvas class is as follows:

Package sprite; </P> <p> Import javax. microedition. lcdui. canvas; <br/> Import javax. microedition. lcdui. graphics; <br/> Import javax. microedition. lcdui. image; <br/> Import javax. microedition. lcdui. game. sprite; </P> <p>/** <br/> * genie frame sequence <br/> * @ author kf156 (Asian Day) <br/> */<br/> public class spritecanvas extends canvas implements runnable {<br/> Public int [] Key = new int [4]; </P> <p> Public final Static int key_up =-1; // up </P> <p> Public final static int key_down =-2; // bottom </P> <p> Public final static int key_left =-3; // left </P> <p> Public final static int key_right =-4; // right </P> <p> Public final static int none = 0; // standing </P> <p> Public final static int up = 1; </P> <p> Public final static int down = 2; </P> <p> Public final static int left = 3; </P> <p> Public final static int right = 4; </P> <p> Public int dir ECT = none, ndirect = none; // current direction, and next action direction </P> <p> Public int width, height; </P> <p> Public thread; // main thread </P> <p> public image; // image </P> <p> Public sprite msprite; // genie </P> <p> // defines the frame sequence array <br/> int [] [] frame_sequence = {0, 1, 2, 1 }, // frame sequence going up <br/> {3, 4, 5, 4}, // frame sequence going down <br/> {6, 7, 8, 7}, // frame sequence to the left <br/> {9, 10, 11, 10}, // frame sequence to the right <br/> {1 }, // upward frame <br/> {4}, // downward frame <br/> {7 },/ /Left standing frame <br/> {10} // right standing frame <br/>}; </P> <p> int curx; // X coordinate </P> <p> int Cury; // y coordinate </P> <p>/** <br/> * structure <br/> */<br/> Public spritecanvas () {<br/> setfullscreenmode (true); <br/> width = getwidth (); <br/> Height = getheight (); <br/> Init (); <br/> startthread (); <br/>}</P> <p>/** <br/> * initialization <br/> */<br/> Public void Init () {</P> <p> try {<br/> image = image. createimage ("/run.png"); // load sprit E's original image <br/>}catch (exception e) {<br/> system. out. println ("load image error:" + E); <br/>}< br/> msprite = new sprite (image, 21, 31); // create an genie, specify the width and height of the sprite as 21 <br/> msprite. setframesequence (frame_sequence [5]); // default frame sequence <br/> curx = (getwidth ()-msprite. getwidth ()> 1; // obtain the X coordinate of the genie placed in the center of the screen <br/> Cury = (getheight ()-msprite. getheight ()> 1; // obtain the Y coordinate of the genie placed in the center of the screen </P> <p> msprite. setrefpixelposition (curx, Cury ); // Set the sprite position to the center of the screen <br/>}</P> <p>/** <br/> * press the key to enter <br/> * <br /> */<br/> Public void input () {<br/> key [0] = Key [2]; <br/> key [1] = Key [3]; <br/> key [2] = 0; <br/>}</P> <p> Public void Update () {<br/> msprite. nextframe (); </P> <p> switch (Key [1]) {<br/> case none: // No buttons <br/> switch (ndirect) {// determine the next direction <br/> case up: <br/> msprite. setframesequence (frame_sequence [4]); // sets the sprite frame sequence <br/> break; <br/> case down: <br /> Msprite. setframesequence (frame_sequence [5]); <br/> break; <br/> case left: <br/> msprite. setframesequence (frame_sequence [6]); <br/> break; <br/> case right: <br/> msprite. setframesequence (frame_sequence [7]); <br/> break; <br/>}< br/> direct = none; <br/> break; </P> <p> case key_up: <br/> ndirect = up; <br/> If (direct! = Ndirect) {// if the direction changes, reset the frame sequence <br/> msprite. setframesequence (frame_sequence [0]); // Add the genie sequence to the genie attribute <br/> direct = ndirect; <br/>}< br/> msprite. move (0,-4); <br/> If (msprite. getrefpixely () <-msprite. getheight () <br/> msprite. setposition (msprite. getrefpixelx (), height); </P> <p> break; <br/> case key_down: <br/> ndirect = down; <br/> If (direct! = Ndirect) {<br/> msprite. setframesequence (frame_sequence [1]); // Add the genie sequence to the genie attribute <br/> direct = ndirect; <br/>}< br/> msprite. move (0, 4); <br/> If (msprite. getrefpixely ()> height) <br/> msprite. setposition (msprite. getrefpixelx (),-msprite <br/>. getheight (); <br/> break; <br/> case key_left: <br/> ndirect = left; <br/> If (direct! = Ndirect) {<br/> msprite. setframesequence (frame_sequence [2]); // Add the genie sequence to the genie attribute <br/> direct = ndirect; <br/>}< br/> msprite. move (-4, 0); <br/> If (msprite. getrefpixelx () <-msprite. getwidth () <br/> msprite. setposition (width, msprite. getrefpixely (); <br/> break; <br/> case key_right: <br/> ndirect = right; <br/> If (direct! = Ndirect) {<br/> msprite. setframesequence (frame_sequence [3]); // Add the genie sequence to the genie attribute <br/> direct = ndirect; <br/>}< br/> msprite. move (4, 0); <br/> If (msprite. getrefpixelx ()> width) <br/> msprite <br/>. setposition (-msprite. getwidth (), msprite <br/>. getrefpixely (); <br/> break; </P> <p >}< br/>}</P> <p> protected void paint (Graphics g) {<br/> G. setcolor (0); <br/> G. fillrect (0, 0, getwidth (), getheight (); <br/> msprite. paint (g); // paint genie </P> <p >}</P> <p> Public void startthread () {<br/> If (thread = NULL) <br/> thread = new thread (this); <br/> thread. start (); <br/>}</P> <p> Public void run () {<br/> int DEBUG = 0; <br/> // system. out. println ("Debug:" + debug ++); <br/> try {<br/> while (true) {<br/> // use this method on a real machine <br/> long stare = system. currenttimemillis (); <br/> // system. out. println ("Debug:" + debug ++); <br/> input (); <br/> // system. out. println ("Debug:" + debug ++); <br/> Update (); <br/> // system. out. println ("Debug:" + debug ++); <br/> repaint (); <br/> servicerepaints (); <br/> long end = system. currenttimemillis ()-stare; <br/> If (end <150) <br/> thread. sleep (150-end); <br/>}< br/> // spritemidlet. quitapp (); <br/>}catch (exception e) {<br/> system. err. println ("RUN error:" + E + "Debug:" + Debug ); <br/>}</P> <p> /********************** **************************************** * ************ <br/> * how to trigger fullcanvas keys that are reloaded <br/> * @ Param keycode <br/> * INT system input key code <br/> ************************** **************************************** * *******/<br/> protected void keypressed (INT keycode) {<br/> key [2] = Key [3] = keycode; <br/>}</P> <p> /*************************** **************************************** * ******* <br/> * method for triggering a duplicate fullcanvas key <br/> * @ Param keycode <br/> * system-passed key code <br/> ****************************** **************************************** * ***/<br/> protected void keyrepeated (INT keycode) {<br/>}</P> <p> /************************** **************************************** * ******** <br/> * method for triggering the release of a reloaded fullcanvas by pressing a key <br/> * @ Param keycode <br/> * system-passed key code <br/> ***************************** **************************************** * ***/<br/> Public void keyreleased (INT keycode) {<br/> key [3] = 0; <br/>}</P> <p >}< br/>

 

 

If the project has been uploaded and you find it inconvenient to see it on the webpage, you can download it directly (because this project was uploaded early, some comments of the Code are unknown ):

Http://download.csdn.net/source/987436

 

 

 

 

 

 

 

 

 

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.