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

📄 moveablesprite.java

📁 系统是一个用JAVA来实现的潜艇大战游戏。厘米那包括可执行源程序
💻 JAVA
字号:
/* 
 * 
 * 创建日期 2006-5-17
 *
 * TODO 要更改此生成的文件的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
package net.wangsong;

import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;

/**
 * @author user
 *
 * TODO 要更改此生成的类型注释的模板,请转至
 * 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class MoveableSprite extends Sprite implements SubObject {
	
	private final int frameChangeTick;   //可以做帧画面变化的次数
	
	//每个帧画面在一下区域变化
	private int xBound;
	private int yBound;
	private int wBound;
	private int hBound;
	
	private int vx;
	private int vy;
	
	private int ticksSinceLastChangeX  = 0;
	private int ticksSinceLastChangeY  = 0;
	
	private LayerManager layerManager;
	
	/** 可移动动画精灵(Sprite)
	 * @param image              传入图像
	 * @param frameWidth         目标帧单元宽度
	 * @param frameHeight        目标帧单元高度
	 * @param frameChangeTick    帧图片数量
	 * 
	 * @param x                  图层横坐标      初始化类图层横坐标加上横向位移
	 * @param y                  图层纵坐标      初始化类图层纵坐标加上纵向位移
	 * @param vx                 图层移动x轴速度
	 * @param vy                 图层移动y轴速度
	 * @param xBound             图层运动区域 -- 起点x坐标
	 * @param yBound             图层运动区域 -- 起点y坐标
	 * @param wBound             图层运动区域 -- 终点x坐标
	 * @param hBound             图层运动区域 -- 终点y坐标
	 * @param layerManager       图层管理调用器
	 */
	public MoveableSprite(Image image, int frameWidth, int frameHeight, int frameChangeTick, int x, int y, 
			int vx, int vy, int xBound, int yBound, int wBound, int hBound, LayerManager layerManager){
		
		super(image, frameWidth, frameHeight);
		
		this.frameChangeTick = frameChangeTick;
		
		//图层定位及定向
		this.setPosition(x, y);
		if(vx < 0){
			this.setTransform(Sprite.TRANS_MIRROR);
		}else{
			this.setTransform(Sprite.TRANS_NONE);
		}
		
		//设置速度和运动边界
		setSpeed(vx, vy);
		setBound(xBound, yBound, wBound, hBound);
		
		this.layerManager = layerManager;
	}
	
	/** 设置速度
	 * @param vx
	 * @param vy
	 */
	public void setSpeed(int vx, int vy){
		this.vx = vx;
		this.vy = vy;
	}
	
	/** 设置帧图层运动区域
	 * @param xBound
	 * @param yBound
	 * @param wBound
	 * @param hBound
	 */
	public void setBound(int xBound, int yBound, int wBound, int hBound){

		this.xBound = xBound;
		this.yBound = yBound;
		this.wBound = wBound;
		this.hBound = hBound;
	}
	
	/** 根据传入的参数设定时间, 触发Sprite的内部帧转换(动画效果)
	 *  本方法每运行一次, 画面帧改变一次, 同时位置也改变一次
	 * @param tickCount
	 */
	public void tick(int tickCount){
		if((tickCount % this.frameChangeTick) == 0){
			this.nextFrame();
		}
		
		//画面移动
		this.movePosition();
	}
	
	/**
	 * 没有传入参数的movePosition, 表明没有从外界接受移动指令
	 * 而是自身自动移动
	 */
	public void movePosition(){
		this.ticksSinceLastChangeX++;
		this.ticksSinceLastChangeY++;
		
		//获取当前位置
		int x = getX();
		int y = getY();
		
		//有的时候改变一下速度vx 及 vy
		if((this.ticksSinceLastChangeX > 20) && SubMIDlet.createRandom(20) == 0){
		    //vx置为-1 或者 1
		    this.vx = SubMIDlet.createRandom(2) * 2 - 1;
		    this.ticksSinceLastChangeX = 0;
		}
		if((this.ticksSinceLastChangeY > 20) && SubMIDlet.createRandom(20) == 0){
		    //vx置为-1, 0 或者 1
		    this.vy = SubMIDlet.createRandom(3) - 1;
		    this.ticksSinceLastChangeY = 0;
		}
		
		//当触壁时, 速度反向, 速度变化累加器置为0
		if(((vx < 0) && (x + vx < xBound))  ||  (vx > 0) && (x + getWidth() > (xBound + wBound))){
		    vx = -vx;
		    this.ticksSinceLastChangeX = 0;
		}
		if(((vy < 0) && (y + vy < yBound))  ||  (vy > 0) && (y + getHeight() > (yBound + hBound))){
		    //此时y轴速度既可能反向, 也可能停止
		    if(SubMIDlet.createRandom(2) == 0){
		        vy = -vy;
		    }else{
		        vy = 0;
		    }
		    this.ticksSinceLastChangeY = 0;
		}
		
		
		
		//当x轴速度反向时, 图形反向
		if(vx < 0){
		    this.setTransform(Sprite.TRANS_MIRROR);
		}else{
		    this.setTransform(Sprite.TRANS_NONE);
		}
		
		//重置图形位置
		x = x + vx;
		y = y + vy;
		
		//由于是类图层运动先重新定位, 有可能造成在类图层定位后
		//某个Sprite的坐标被抛出了setBound()重定义后的, 自身运动区域,就需要做轻微调整
		if(x < xBound){
		    x = xBound;
		}
		if(x > xBound + wBound){
		    x = xBound + wBound - 1;
		}
		if(y < yBound){
		    y = yBound;
		}
		if(y > yBound + hBound){
		    y = yBound + hBound - 1;
		}
		
		this.setPosition(x, y);
	}

    /**
     * 接口方法
     */
    public void collideStuff() {
        
    }
	
}









⌨️ 快捷键说明

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