📄 scrollcanvas.java
字号:
import java.io.IOException;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.*;
/**
* The GameCanvas that dispalys the different background layers
*
* This code is part of the Tips & Tricks section at
* www.SonyEricsson.com/developer
*
* Written by J鰊s Weimarck, 2004
*/
public class ScrollCanvas extends GameCanvas implements Runnable {
private boolean isRunning;
private LayerManager layerManager;
// Sets the pixel speed of the backgrounds
private static final int BACKGROUND_HORIZONTAL_MOVE = -2;
private static final int FOREGROUND_HORIZONTAL_MOVE = -4;
private int w = getWidth();
private int h = getHeight();
public ScrollCanvas() throws IOException {
super(true);
w = getWidth();
h = getHeight();
// Create a LayerManager.
layerManager = new LayerManager();
layerManager.setViewWindow(96, 0, w, h);
// The spaceship will get the first LayerManager index, thus being foremost
createSpaceship();
// The Backgrounds will also be appended to the LayerManager
createBackgrounds();
}
/**
* Creates the individual backgrounds and adds them to a LayerManager
*/
private void createBackgrounds()throws IOException {
layerManager.append(createForegroundMountains());
layerManager.append(createBackgroundMountains());
}
private BackgroundLayer createForegroundMountains()throws IOException {
Image backgroundImage = Image.createImage("/mountain.png");
int[] map = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,0,0,0,
0,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,0,1,3,3,2,0,0,0,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,0,1,3,3,2,0,0
};
int maprows=2;
int mapcols=48;
BackgroundLayer background = new BackgroundLayer(mapcols, maprows, backgroundImage, 48, 48 );
background.setHorizontalMove(FOREGROUND_HORIZONTAL_MOVE);
background.setPosition(12, 176-(maprows*48));
background.setCells(map,maprows, mapcols);
return background;
}
private BackgroundLayer createBackgroundMountains()throws IOException {
Image backgroundImage = Image.createImage("/mountain.png");
int[] map = {
0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,0,1,2,0,0,0,
1,3,3,2,0,0,1,3,3,2,0,0,1,3,3,3,3,3,3,3,3,2,0,0,1,3,3,2,0,0,1,3,3,2,0,0,1,3,3,3,3,3,3,3,3,2,0,0
};
int maprows=2;
int mapcols=48;
BackgroundLayer background = new BackgroundLayer(mapcols, maprows, backgroundImage, 48, 48 );
background.setHorizontalMove(BACKGROUND_HORIZONTAL_MOVE);
background.setPosition(12, 176-(maprows*48));
background.setCells(map,maprows, mapcols);
return background;
}
private void createSpaceship()throws IOException {
Image penguinImage = Image.createImage("/penguin.png");
// Even though it's actually a penguin, lets call it a spaceship...
Sprite spaceship = new Sprite(penguinImage, 48, 48);
spaceship.setPosition(96 + (getWidth() - 48) / 2, 96);
spaceship.defineReferencePixel(24, 24);
layerManager.append(spaceship);
}
public void start() {
isRunning = true;
Thread t = new Thread(this);
t.start();
}
public void run() {
Graphics g = getGraphics();
// Main game loop, do this to the bitter end (well, until the backgrounds has reached their ends)
while (isRunning) {
if (isShown()) {
int keyStates = getKeyStates();
// Move the spaceship
if ((keyStates & UP_PRESSED) != 0) {
layerManager.getLayerAt(0).move(0,-2);
}else if ((keyStates & DOWN_PRESSED) != 0) {
layerManager.getLayerAt(0).move(0,2);
}
// Move the Backgrounds
scrollBackgrounds();
// Stop the game when all background have scolled by...
if(EndOfBackgrounds()){
isRunning=false;
}
g.setColor(0x6600CC);
g.fillRect(0, 0, w, h);
layerManager.paint(g, 0, 0);
flushGraphics();
}
try {
Thread.sleep(10);
}catch (InterruptedException e) {System.out.println(e.getMessage());}
}
}
public void stop() {
isRunning = false;
}
/**
* Scrolls the backgrounds individually
*/
private void scrollBackgrounds(){
BackgroundLayer background;
//Don't mind the first Layer of the LayerManager, it's the spaceship
for(int i=1; i<layerManager.getSize(); i++){
background = (BackgroundLayer)layerManager.getLayerAt(i);
background.move();
}
}
/**
* Just checks if the backgrounds have reached their end.
* Returns true if thats the case, false otherwise.
*/
private boolean EndOfBackgrounds(){
//Don't mind the first Layer of the LayerManager, it's the spaceship!
for(int i=1; i<layerManager.getSize(); i++){
if (((BackgroundLayer)layerManager.getLayerAt(i)).endOfBackground(w)){
return true;
}
}
return false;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -