J2ME游戏开发之--精灵旋转

image

image

package com.slinw.sprite;



import java.io.IOException;



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;

import com.slinw.unit.Logs;



/**

 * 

 * @author 章伟

 * 要实现精灵的变换最重要的一个概念为:参考像素点(reference pixel)

 *         参考像素点通过指定其在 Sprite未经变换的帧内的某一点来定义,使用 defineReferencePixel(x,y)方法。缺省的,参考像素点定义在帧的(0,0)像素点

 * 我们拿旋转来举例:参考像素点既是我们旋转的圆点

 * 

 * 

 *

 */

public class SpriteRorateAnim extends RootGameCanvas{



    private Image image = null;

    

    private Sprite sprite = null;

    private int left = 10;

    private int top = 10;

    private int[] kFrameLookup;  

    private int[] kFrameTranslateLookup;

    private int mCount = 0;

    private int mDirection = 0;

    

    public SpriteRorateAnim(){

        

        initData();

            

    }

    

    public void initData() {

        // TODO Auto-generated method stub



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

        

        sprite = new Sprite(image,57,58);

        

        kFrameLookup = new int[]{0,1,2,

                0,1,2,

                0,1,2,

                0,1,2};  //12帧

        

        kFrameTranslateLookup = new int[]{

                Sprite.TRANS_NONE,Sprite.TRANS_NONE,Sprite.TRANS_NONE,

                Sprite.TRANS_ROT90,Sprite.TRANS_ROT90,Sprite.TRANS_ROT90,

                Sprite.TRANS_ROT180,Sprite.TRANS_ROT180,Sprite.TRANS_ROT180,

                Sprite.TRANS_ROT270,Sprite.TRANS_ROT270,Sprite.TRANS_ROT270,

        };

        

    }

    



    public void drawScreen(Graphics g) {

        // TODO Auto-generated method stub

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

        

//        sprite.setPosition(100,300);    

        mCount ++ ;

        mDirection = mCount % 12;

        



        sprite.setFrame(kFrameLookup[mDirection]);

        sprite.setTransform(kFrameTranslateLookup[mDirection]);    

        

                

        sprite.setPosition(100,300);   //记住要先变换以后在设置图像的位置

    

        

        sprite.paint(g);  

        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)