📄 car.java
字号:
/*
* Copyright 2003, 2004 Symbian Ltd.
* For License terms see http://www.symbian.com/developer/techlib/codelicense.html
*/
package demoracer;
/**
* <p>Title: DemoRacer</p>
* <p>Description: Game API Demo for Symbian MIDP 2.0 Book.</p>
* @author Alan Newman - alan@sensibledevelopment.net
* @version 1.0
*/
/*
* This class extends Sprite and provides the car animation for the app.
* It is made up from 4 frames which are supplied by the image car.png
* LayerManager instatiates the class and also makes calls to its tick method
* to set the next frame to be shown on the display.
* tick() also checks for collisions with other sprites on the display
* such as 'StartFinish' and 'Puddle'
*/
import javax.microedition.lcdui.Image;
import javax.microedition.lcdui.game.Sprite;
public class Car extends Sprite {
static final int RAW_FRAMES = 4;
static final int DRIVE_NORMAL = 0;
static final int DRIVE_WET = 1;
private int frameOrder[][] = {{0, 1}, {2, 3}};
private StartFinish startFinish;
private Puddle puddle;
private RacerLayerManager layerManager;
private int puddleCount;
private boolean wet = false;
public Car(Image image, int width, int height, int x, int y, RacerLayerManager layerManager) {
super(image, width, height);
//Set the frame sequence according to frameOrder this is a 2x2 array, row 0 is used
//for normal animation whereas row 1 is used when the car hits a puddle
setFrameSequence(frameOrder[DRIVE_NORMAL]);
setPosition(x, y);
// define the part of the car that needs to detect whether it has "hit" anything
// this saves it checking the whole sprite
defineCollisionRectangle(getWidth()-1,0,1,getHeight());
layerManager=layerManager;
puddle=layerManager.getPuddle();
startFinish=layerManager.getStartFinish();
}
public void tick() {
checkCollisions();
nextFrame();
}
public void checkCollisions() {
//if the start finish line is visible then check for a collision
//if collision is true then set "Lap Complete" toggle to true, elses false
//LayerManager will draw the message as required
if(startFinish.isVisible()) {
if (this.collidesWith(startFinish, true)) {
startFinish.setLapComplete(true);
} else {
startFinish.setLapComplete(false);
}
}
//if the puddle is visible on the display then check for a collision with the car
//if true then change the row of the array that is the frame to be displayed.
//row 0 for normal and row 1 for puddle collision true.
if(puddle.isVisible()) {
if (!wet && this.collidesWith(puddle, true)) {
setFrameSequence(frameOrder[DRIVE_WET]);
puddleCount = puddle.getWidth()/2;
wet = true;
} else if (--puddleCount == 0) {
setFrameSequence(frameOrder[DRIVE_NORMAL]);
wet = false;
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -