停止执行方法以移动到下一个方法

时间:2013-02-22 13:42:49

标签: java arrays if-statement

对这个有点困惑所以我以为我会发帖。对不起,如果我的标题不清楚,非常新的java,并不知道如何解释它。无论如何这是我到目前为止的代码集(我猜的问题)

int currentImageIndex = 0; // Assuming [0] is your first image.
int[] nextImageList = { 2, 4, 5, 4, 5, 4, 5, 0, 1 };

public void nekoRun() {
    moveIn();
    scratch();
    moveOut();

private void moveIn() {
    for (int i = 0; i < getWidth()/2; i+=10) {
        xPos = i;
        // swap images
        if (currentImage == nekoPics[0]) 
            currentImage = nekoPics[1];
        else 
            currentImage = nekoPics[0];
        repaint();
        pause(150);

private void scratch() {
    for (int i = xPos; i < getWidth();) {
        xPos = i;

        // Swap images.
        currentImageIndex = nextImageList[currentImageIndex];
        currentImage = nekoPics[currentImageIndex];



            repaint();
            pause(150);
        }
}

private void moveOut() {
    for (int i = xPos; i < getWidth(); i+=10) {
        xPos = i;
        // swap images
        if (currentImage == nekoPics[0]) 
            currentImage = nekoPics[1];

        else 
            currentImage = nekoPics[0];
        repaint();
        pause(150);
    }   
}

所以基本上发生了什么(这不是所有的代码只是“多汁的部分”,一只猫会跑过屏幕然后坐下来它应该划伤两次,我得到了一些帮助,因为我只是使用一大堆其他if语句,我知道这是多余的。猫会跑到中心,它会划伤,并且在一个循环中继续划伤,原因很明显,我只是对我怎么弄得很困惑它移动到moveOut方法而不是保持循环划痕。抱歉,如果这有点不清楚,我对此很新,所以请耐心等待。

提前致谢

2 个答案:

答案 0 :(得分:2)

您的问题被称为无限循环...而不是

private void scratch() {
    for (int i = xPos; i < getWidth();) {

这应该是

private void scratch() {
    for (int i = 0; i < 2*<how many frames the scratching takes>; i++) {
    // **UPDATE** the xPos=i; shouldn't be here!!!

说明:

似乎你已经从moveIn()函数中复制了循环定义,这在这种情况下似乎是合法的。有些事情发生了,它会到达屏幕中间。但是在scratch()函数中,精灵不会移动,它不会到达屏幕的endo ......所以ozu必须重复绘制步骤两次,划痕跨越多少帧。你必须把那个numnber放到<how many frames the scratching takes>占位符中,它应该可以工作。

编辑 xPos=i;不应出现在scratch() ...

答案 1 :(得分:-1)

你没有在头上递增i()把i ++放在for语句的末尾。

for (int i = xPos; i < getWidth(); i++)