查看: 1382|回复: 4
|
J2ME 或 JAVA GAME 开发者请帮忙。。。
[复制链接]
|
|
请各位帮忙下。
我正在做J2ME的GAME。但GAME ACTION 怎么写也有问题。
我是用ARRAY 来 SET GAME ACTION 的 FRAME SEQUENCE。
private static final int[] walkingseq = {0,1,2};
private static final int[] fireseq = {3,4,5};
public void input(){
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
Essprite.move(-6, 0);
Essprite.nextFrame();
}
if ((keyState & RIGHT_PRESSED) != 0) {
Essprite.move(6, 0);
Essprite.nextFrame();
}
if ((keyState & UP_PRESSED) != 0) {
Essprite.move(0, -6);
Essprite.nextFrame();
}
if ((keyState & DOWN_PRESSED) != 0) {
Essprite.move(0, 6);
Essprite.nextFrame();
}
if ((keyState & FIRE_PRESSED) != 0) {
Essprite.setFrameSequence(fireseq)
Essprite.nextFrame();
Essprite.setFrameSequence(walkingseq);
}
}
每次我按FIRE BUTTON 后想INITIALIZE FRAME SEQUENCE 去 WALKING FRAME SEQUENCE。 但是那些FRAME 的好象不会再跟SEQUENCE 了。有人可以帮忙吗?? |
|
|
|
|
|
|
|
楼主 |
发表于 1-11-2005 12:05 PM
|
显示全部楼层
原帖由 SotongJiang 于 1-11-2005 06:56 AM 发表
假设你的input()是在while(true)的loop里头。
Essprite.setFrameSequence(walkingseq); 可以放在FIRE_PRESSED以外的某个地方。
通常nextFrame()给它自己跑,跑完与否用sprite.getFrame()==someSequence.length- ...
public class Escanvas extends GameCanvas implements Runnable {
private boolean isPlay;
private long delay;
private Sprite Essprite;
private Display display;
private static final int[] walkingseq = {0,1,2};
private static final int[] fireseq = {3,4};
public Escanvas(Display d){
super(true);
display = d;
delay = 180;
isPlay=true;
}
public void start() {
display.setCurrent(this);
try {
Essprite= new Sprite(Image.createImage("/res/test1.png"),17,17);
Essprite.setFrameSequence(walkingseq);
}catch (Exception e) {System.out.println("Error Loading Image");}
Thread running = new Thread(this);
running.start();
}
public void stop(){
isPlay=false;
}
public void run() {
Graphics g = getGraphics();
while (isPlay==true) {
input();
drawScreen(g);
try {
Thread.sleep(delay);
}
catch (InterruptedException ie) {}
}
}
public void input(){
int keyState = getKeyStates();
Essprite.setFrameSequence(walkingseq);
if ((keyState & LEFT_PRESSED) != 0) {
Essprite.move(-6, 0);
Essprite.nextFrame();
}
if ((keyState & RIGHT_PRESSED) != 0) {
Essprite.move(6, 0);
Essprite.nextFrame();
}
if ((keyState & UP_PRESSED) != 0) {
Essprite.move(0, -6);
Essprite.nextFrame();
}
if ((keyState & DOWN_PRESSED) != 0) {
Essprite.move(0, 6);
Essprite.nextFrame();
}
if ((keyState & FIRE_PRESSED) != 0) {
Essprite.setFrameSequence(fireseq);
Essprite.nextFrame();
}
}
private void drawScreen(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight() );
Essprite.paint(g);
flushGraphics();
}
}
其实已经试过了。把Essprite.setFrameSequence(walkingseq); 的INITIALIZATION 放在 if ((keyState & FIRE_PRESSED) != 0) 之外。包括在while(true)的loop INPUT的过后。但还是一样得不到想要行动的效果。
原本Essprite.setFrameSequence(walkingseq);的 sequence 是 {0,1,2};。
但我按了FIRE然后initialize Essprite.setFrameSequence(walkingseq); 就只是会走{0,2};。
请帮帮忙~~~ |
|
|
|
|
|
|
|
楼主 |
发表于 3-11-2005 09:33 AM
|
显示全部楼层
原帖由 SotongJiang 于 2-11-2005 07:09 AM 发表
那就要多加一个boolean tag,命名为isFire.
if ((keyState & FIRE_PRESSED) != 0) {
if (isFire==false) {
Essprite.setFrameSequence(fireseq);
isFire=true;
}
if (isFire=true) {
...
对不起,我是JAVA新人。想问下其实fireseq.length-1有什么用处?
我看了很多SAMPLE CODE,都有-1的存在。
尤其在COLLISION DETECTION的CODE里。。。 |
|
|
|
|
|
|
|
楼主 |
发表于 3-11-2005 09:49 AM
|
显示全部楼层
经过一轮参考,我的CODE变成酱了:
public class Escanvas extends GameCanvas implements Runnable {
private boolean isPlay;
private long delay;
private Sprite Essprite;
private Display display;
private static final int[] walkingseq = {0,1,2};
private static final int[] fireseq = {3,4};
private int[] currentSeq;
private int[] newSeq;
public Escanvas(Display d){
super(true);
display = d;
delay = 180;
isPlay=true;
}
public void start() {
display.setCurrent(this);
try {
Essprite= new Sprite(Image.createImage("/res/test1.png"),17,17);
Essprite.setFrameSequence(walkingseq);
}catch (Exception e) {System.out.println("Error Loading Image");}
Thread running = new Thread(this);
running.start();
}
public void stop(){
isPlay=false;
}
public void run() {
Graphics g = getGraphics();
while (isPlay==true) {
input();
drawScreen(g);
try {
Thread.sleep(delay);
}
catch (InterruptedException ie) {}
}
}
public void input(){
int keyState = getKeyStates();
if ((keyState & LEFT_PRESSED) != 0) {
Essprite.move(-6, 0);
newSeq = walkingseq;
}
if ((keyState & RIGHT_PRESSED) != 0) {
Essprite.move(6, 0);
newSeq = walkingseq;
}
if ((keyState & UP_PRESSED) != 0) {
Essprite.move(0, -6);
newSeq = walkingseq;
}
if ((keyState & DOWN_PRESSED) != 0) {
Essprite.move(0, 6);
newSeq = walkingseq;
}
if ((keyState & FIRE_PRESSED) != 0) {
newSeq = fireseq;
}
if( newSeq == currentSeq ){
Essprite.nextFrame();
}
else{
currentSeq = newSeq;
Essprite.setFrameSequence( currentSeq );
}
}
private void drawScreen(Graphics g) {
g.setColor(0xffffff);
g.fillRect(0,0,getWidth(),getHeight() );
Essprite.paint(g);
flushGraphics();
}
}
SotongJiang,想请问下,就算我没在initilization时set currentSeq = walkingseq;
但以下的IF还是能走???
if( newSeq == currentSeq ){
Essprite.nextFrame();
}
else{
currentSeq = newSeq;
Essprite.setFrameSequence( currentSeq );
}
currentSeq 不是=null的吗?
[ 本帖最后由 Ppsprov 于 3-11-2005 09:51 AM 编辑 ] |
|
|
|
|
|
|
|
楼主 |
发表于 5-11-2005 12:50 AM
|
显示全部楼层
原帖由 SotongJiang 于 4-11-2005 12:24 AM 发表
首先讨论arrayName.length-1的用意:-
JAVA里array的index都是从0开始,换句话说,如果array有5个elements,它的index排开来就成了0,1,2,3,4.
最后一个是4,而不是5。广义来说index排开就是0,1,2,...,N-1.最后 ...
明白了!!谢谢!! |
|
|
|
|
|
|
| |
本周最热论坛帖子
|