⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 character.java

📁 这是一款功能完备的横版闯关手机游戏
💻 JAVA
字号:
//package bushfighting;

import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.Graphics;

/*
 这个类用于处理场景中人物的基本行为
 */
public class Character extends SceneUnit {
    protected int mState = HF.STATE_STILL;
    protected int mFrameIndex = 0; //当前的动作号,对应不同方向的行走姿态
    protected int mPicIndex = 0;//当前的图片索引号
    protected int mAttackDelay = 0;//每次攻击之间的延时间隔
    
    protected boolean mHurtCount =false;
    
    protected int desX;
    protected int desY;
    
    protected Image[] mImages = null;
    protected int[] mFrames = null;
    
    public Character( int[] imgFrames) {
        super();
        desX= -1;
        desY= -1;
        //mImages = images;
        mFrames = imgFrames;
    }
    public void setImages(Image[] images) {
        mImages = images;
    }
    //设置角色的状态的方向
    protected void setState(int state) {
        if(state>=HF.STATE_MOVE_UP&&state<=HF.STATE_MOVE_RIGHT){
            if(state == HF.STATE_MOVE_UP||state == HF.STATE_MOVE_DOWN) {
                if(mDirection == HF.STATE_MOVE_LEFT) {
                    mPicIndex = mDirection;
                    mDirection = state;
                } else if(mDirection == HF.STATE_MOVE_LEFT) {
                    mPicIndex = mDirection;
                    mDirection = state;
                } else if(mDirection == HF.STATE_MOVE_UP||mDirection == HF.STATE_MOVE_DOWN) {
                    mDirection = state;
                }
            } else {
                mDirection = state;
                mPicIndex = state;
            }
        } else if(state == HF.STATE_ATTACK) {
            if(mPicIndex==HF.STATE_MOVE_LEFT) {
                mDirection = HF.STATE_MOVE_LEFT;
            } else if(mPicIndex==HF.STATE_MOVE_RIGHT) {
                mDirection = HF.STATE_MOVE_RIGHT;
            }
        } else if(mDirection != HF.STATE_MOVE_LEFT){
            mDirection = HF.STATE_MOVE_RIGHT;//默认的方向向右
        }
        
        if(state == HF.STATE_ATTACK){
            mFrameIndex = 0;
            if(mDirection == HF.STATE_MOVE_LEFT){
                mPicIndex = 6;
            } else{
                mPicIndex = 7;
            }
        } else if(state == HF.STATE_DIE){
            mFrameIndex = 0;
            if(mDirection == HF.STATE_MOVE_LEFT){
                mPicIndex = 4;
            } else{
                mPicIndex = 5;
            }
        }
        mState = state;
    }
    
    //切换动作帧
    protected void setFrame() {
        if(mState != HF.STATE_STILL)
            mFrameIndex = (mFrameIndex+1)%mFrames[mPicIndex];
        if(mFrameIndex== 0&&mState == HF.STATE_DIE){
            mUsed = false;//显示死亡的样子完毕后,标记未不存在
        }
        if(mFrameIndex == 0&&mState == HF.STATE_ATTACK){
            //等到攻击的状态结束后,自动切换成静止状态
            mState = HF.STATE_STILL;
            mPicIndex = mDirection;
        }
    }
    
    protected void setPictureIndex(int index) {
        mPicIndex = index;
        mFrameIndex = 0;
    }
    
    //绘制角色
    public void draw(Graphics g) {
        if(mType == HF.PLAYER&&mHurtCount ==true) {
            Image img;
            int fCount;
            if(mPicIndex%2==0) {
                img = mImages[4];
                fCount = mFrames[4];//当前图片含有的帧数目
            } else {
                img = mImages[5];
                fCount = mFrames[5];//当前图片含有的帧数目
            }
            int width = img.getWidth()/fCount;
            int height = img.getHeight();
            int x = mX - width/2;
            int y = mY - height/2;
            mHurtCount = false;
            g.setClip(x,y,width,height);
            if(mPicIndex%2==0){
                g.drawImage(img,x,y,Graphics.LEFT|Graphics.TOP);
            }
            else{
                g.drawImage(img,x-width,y,Graphics.LEFT|Graphics.TOP);
            }
        } else {
            // if(mType == HF.PLAYER && mState==HF.STATE_ATTACK){
            //     if()
            //  }
            Image img = mImages[mPicIndex];
            int fCount = mFrames[mPicIndex];//当前图片含有的帧数目
            int width = img.getWidth()/fCount;
            int height = img.getHeight();
            int x = mX - width/2;
            int y = mY - height/2;
            g.setClip(x,y,width,height);
            int tmf=mFrameIndex;
            if(mPicIndex == 3 || mPicIndex == 5 || mPicIndex == 7){
                tmf=fCount-mFrameIndex-1;
            }
            g.drawImage(img,x-tmf*width,y,Graphics.LEFT|Graphics.TOP);
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -