?? sprite.java
字號:
/**
* A state manager for a game sprite. Use in conjunction with an ImageSet in
* order to draw animated, multi-state sprites on screen. For each instance of
* an animated graphics you should create one corresponding Sprite object.
* Graphics are shared using a common ImageSet. To animate you must call this
* class's cycle method.
*/
import javax.microedition.lcdui.Graphics;
public class Sprite
{
private int currentFrame;
private int currentState;
private long currentStateBegan; // time this currentState started
private ImageSet imageSet;
private long lastFrameChange;
private int totalCycles;
public Sprite(ImageSet is, int startingState, int startingFrame)
{
imageSet = is;
setState(startingState, false);
currentFrame = startingFrame;
}
public final void setFrame(int f)
{
currentFrame = f;
}
public final void setState(int s, boolean force)
{
if (currentState != s || force)
{
currentState = s;
currentFrame = 0;
totalCycles = 0;
currentStateBegan = System.currentTimeMillis();
}
}
public final void reset()
{
currentFrame = 0;
totalCycles = 0;
currentStateBegan = 0;
lastFrameChange = 0;
}
public final long getWhenStateBegan()
{
return currentStateBegan;
}
public final long getTimeInCurrentState()
{
return (System.currentTimeMillis() - currentStateBegan);
}
public final int getCurrentState()
{
return currentState;
}
public final int getCurrentFrame()
{
return currentFrame;
}
public final void draw(Graphics target, int targetX, int targetY)
{
imageSet.draw(target, currentState, currentFrame, targetX, targetY);
}
public final void cycle(long deltaMS)
{
// change frame if we are animating (and enough time has passed)
if (imageSet.getTotalFrames(currentState) > 1 &&
imageSet.getAnimTime(currentState) > 0)
{
long deltaTime = System.currentTimeMillis() - lastFrameChange;
if (deltaTime > imageSet.getAnimTimePerFrame(currentState))
{
currentFrame++;
lastFrameChange = System.currentTimeMillis();
if (currentFrame >= imageSet.getTotalFrames(currentState))
{
currentFrame = 0;
totalCycles++;
}
}
}
}
public final int getTotalCycles()
{
return totalCycles;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -