J2ME游戏开发之精灵--帧动画

package com.slinw.sprite;



import javax.microedition.lcdui.Graphics;

import javax.microedition.lcdui.Image;

import javax.microedition.lcdui.game.Sprite;



import com.slinw.gamecanvas.RootGameCanvas;

import com.slinw.graphics.ImageCanvas;





/**

 * 

 * @author 章伟

 *  帧:

 *      Sprite(Image, int width, int height) :可以把一个图片按照指定的大小绘制成不同的帧

 *  帧序列:

 *      定义了帧以什么样的顺序显示,默认情况下是从0开始

 *  方法:

 *      setFrame(sequenceIndex):将当前帧设置为制定序号的图像

 *      getFrame():得到当前帧的图像序号

 *      prevFrame() :当前帧的前一帧

 *      nextFrame() :当前帧的下一帧

 *  除了从图片获取可运行的帧以外,用户还可以把获得的帧进行自定义

 *  另外要想通过从第几帧开始到第几帧结束,可以通过自定义帧的方式实现

 */

public class SpriteFrameAnim extends RootGameCanvas{



    private Image image;

    private Sprite sprite;

    private Sprite sprite1;

    private int left = 10;

    private int top = 10;

    private int[] FRAME_SEQUENCE ;

    

    public SpriteFrameAnim(){

        initData();

    }

    

    private void initData(){

        

        image = ImageCanvas.getImage("/anim01.png");

        sprite = new Sprite(image,68,60);

        

        sprite1 = new Sprite(sprite);

        userDefineFrameSequence();

    }

    

    

    private void userDefineFrameSequence(){

        FRAME_SEQUENCE =new int[]{0,1,2,3,3,2,1,0};    

        sprite1.setFrameSequence(FRAME_SEQUENCE);

    }

    

    

    public void drawScreen(Graphics g) {

        // TODO Auto-generated method stub

        g.drawImage(image, left,top, Graphics.LEFT |Graphics.TOP);

        

        sprite.setPosition(left, top + 100);   //设置图片绘制的位置

        sprite.paint(g);            //绘制精灵图片

        sprite.nextFrame();   //调用下一帧实现动画

        

        sprite1.setPosition(left, top + 180);

        sprite1.paint(g);

        sprite1.nextFrame();   //调用下一帧实现动画

        



        

        this.flushGraphics();

        

        try {

            Thread.sleep(500);

        } catch (InterruptedException e) {

            // TODO Auto-generated catch block

            e.printStackTrace();

        }    

    }



    public void keyLeftPressedEvent() {

        // TODO Auto-generated method stub

        

    }



    public void keyRightPressedEvent() {

        // TODO Auto-generated method stub

        

    }



    public void keyUpPressedEvent() {

        // TODO Auto-generated method stub

        

    }



    public void keyDownPressedEvent() {

        // TODO Auto-generated method stub

        

    }



}

你可能感兴趣的:(j2me)