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

📄 worker.java

📁 J2ME程序设计实例教程的源码
💻 JAVA
字号:
import javax.microedition.lcdui.game.*;
import javax.microedition.lcdui.*;

/**
 * 该类描述了游戏中搬运工的状态,及行走规则
 */
public class Worker extends Sprite {
    public static final int EAST = 0;
    public static final int SOUTH = 1;
    public static final int WEST = 2;
    public static final int NORTH = 3;
    public static final int DISTANCE = 2; //搬运工一步的距离
    
    private Storage storage;        //仓库
    private Sprite[] boxes;    //搬运工要搬运的箱子
    
   
    //frameSequence表示搬运工在每个方向的动画帧序列
    private int[][] frameSequences;    
    private int currentDirection;   //搬运工的当前方向
    private int horDistances = 0;   //水平移动
    private int verDistances = 0;   //垂直移动
    private Sprite forcedBox = null;   //被推动的箱子
    
    //构造方法,创建一个搬运工对象
    /**
     * frameSequence[0]表示向东走时的动画帧序列
     * frameSequence[1]表示向南走时的动画帧序列 
     * frameSequence[2]表示向西走时的动画帧序列
     * frameSequence[3]表示向北走时的动画帧序列
     */
    public Worker(Image img, int frameWidth, int frameHeight, int[][] frameSequences) {
        super(img, frameWidth, frameHeight);
        this.frameSequences = frameSequences;
        currentDirection = SOUTH;
        setFrameSequence(frameSequences[currentDirection]);
        horDistances = 0;
        verDistances = DISTANCE;
    }
    
    //进入仓库storage
    public void comeInto(Storage storage) {
        this.storage = storage;
    }
    
    //更新要搬运的箱子数组
    public void updateBoxes() {
        boxes = storage.getBoxes();
    }
    
    //当前搬运工向direction方向移动
    public boolean move(int direction) {
        if(direction != currentDirection) { //改变方向
            changeDirection(direction);
        }
        
        boolean can = true;
        super.move(horDistances, verDistances);
        
        //与仓库围墙进行碰撞检测
        if(collidesWith(storage.getWall(), false)) {
            can = false;
        }
        
        if(can) {   //没有与围墙相碰
            int i = 0;
            //与箱子进行碰撞检测
            while(i<boxes.length && !collidesWith(boxes[i], false)) {
                i++;
            }
            
            //与箱子碰撞,移动箱子
            if(i<boxes.length && !storage.moveBox(boxes[i], horDistances, verDistances)) {    
                can = false;
            }
        }
        
        if(!can) {  //搬运工不能移动
            super.move(-horDistances, -verDistances);
        }
        nextFrame();    //显示下一个动画帧
        return can;
    }
    
    //搬运工改变行走方向
    private void changeDirection(int direction) {
        currentDirection = direction;
        setFrameSequence(frameSequences[currentDirection]);
        if(currentDirection == EAST) {
            horDistances = DISTANCE;
            verDistances = 0;
        }
        else if(currentDirection == SOUTH) {
            horDistances = 0;
            verDistances = DISTANCE;
        }
        else if(currentDirection == WEST) {
            horDistances = -DISTANCE;
            verDistances = 0;
        }
        else if(currentDirection == NORTH) {
            horDistances = 0;
            verDistances = -DISTANCE;
        }
    }
}

⌨️ 快捷键说明

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