为什么我不能乘以一个物体?

时间:2014-09-12 21:07:58

标签: java android class object

我正在使用Android(Java),我想要的是拿一个类,然后从中创建3个实例,但是将一些变量设置为随机。

public class MainActivity extends Activity{

    Player ourView; 

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ourView = new Player(this);
        setContentView(ourView);
    }

    protected void onPause() {
        super.onPause();
        ourView.pause();
    }

    protected void onResume() {
        super.onResume();
        ourView.resume();
    }
}

另一类......

public class Player extends SurfaceView implements Runnable {

    Canvas canvas = new Canvas();
    SurfaceHolder ourHolder;
    Thread ourThread = null;
    boolean isRunning = true;

    public Player(Context context) {
        super(context);
        ourHolder = getHolder();
        ourThread = new Thread(this);
        ourThread.start();
    }

    public void pause() {
        isRunning = false;
        while(true){
        try{
            ourThread.join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        break;
    }
    ourThread = null;
}

    public void resume() {
        isRunning = true;
    }

    public void run() {
        while(isRunning) {
            if(!ourHolder.getSurface().isValid())
                continue;

                canvas = ourHolder.lockCanvas();
                canvas.drawRGB(30, 30, 200);

                Enemy[] enemy = new Enemy[3];
                for(int i = 0; i<enemy.length; i++){
                    enemy[i] = new Enemy();
                    enemy[i].draw();
                }

            ourHolder.unlockCanvasAndPost(canvas);
        }           
    }
}

&#34; Enemy&#34;类:

public class Enemy{

    Canvas canvas = new Canvas();

    float x = (float) (Math.random()*200);
    float y = (float) (Math.random()*200);

    public void draw(){
        Bitmap bitmap = (Bitmap) BitmapFactory.decodeResource(getContext(), R.drawable.ic_launcher);
        canvas.drawBitmap(bitmap, x, y, null);

    }
}

我可以看到创建了3个位图,很酷。但它们不会保持静止,即使条件不再满足,for循环也不会停止。

我之前问过这个问题,但我找不到任何答案。希望你能帮助我,谢谢。

2 个答案:

答案 0 :(得分:3)

你在while循环的每个循环中创建一个包含3个敌人的新数组,一旦while循环完成执行,敌人阵列将离开范围并被销毁并创建一个新数组。如果你想保留这些敌人,你需要将它们存储在循环范围之外。

**编辑** - 试试这个

public class Player extends SurfaceView implements Runnable {

Canvas canvas = new Canvas();
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = true;
Enemy[] enemy;

public Player(Context context) {
    super(context);
    ourHolder = getHolder();
    ourThread = new Thread(this);
    ourThread.start();
    enemy = new Enemy[3];
    for(int i = 0; i<enemy.length; i++){
        enemy[i] = new Enemy();
    }
}

public void pause() {
    isRunning = false;
    while(true){
    try{
        ourThread.join();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    break;
}
ourThread = null;
}

public void resume() {
    isRunning = true;
}

public void run() {
    while(isRunning) {
        if(!ourHolder.getSurface().isValid())
            continue;

            canvas = ourHolder.lockCanvas();
            canvas.drawRGB(30, 30, 200);


            for(int i = 0; i<enemy.length; i++){
                enemy[i].draw();
            }

        ourHolder.unlockCanvasAndPost(canvas);
    }           
}
}

答案 1 :(得分:1)

正如你所说,就像for循环永远不会停止。 因为for循环是在while(isrunning)循环中,它永远不会停止并一遍又一遍地触发你的for循环。

您需要重新组织代码。