【问题标题】:Game with bitmaps randomly appearing from the right位图从右侧随机出现的游戏
【发布时间】:2015-10-26 05:04:00
【问题描述】:

所以我正在尝试制作一个游戏,其中有一个上下移动的正方形,然后从右侧出现红色正方形并向左滑动。到目前为止,我只设法让一个红色方块随机出现并向左滑动,但我似乎无法让其他方块出现。我想做的是设置一个类似计时器的东西,这样每隔一秒左右就会随机出现一个新的红色方块。当我运行游戏时,所发生的只是看到正方形,并且有一个从右到左的红色正方形。请告诉我我可以添加什么以使更多红色方块出现。请帮忙。

代码:

public class Game extends Activity implements OnTouchListener {

GameView gameView;
Bitmap square;
Bitmap redSquare;
float x, y;
Random random;
float redSquareY, redSquareX;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gameView = new GameView(this);
    gameView.setOnTouchListener(this);
    square = BitmapFactory.decodeResource(getResources(), R.drawable.square);
    redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.redsquare);
    x = y = 200;
    setContentView(gameView);
    redSquare = BitmapFactory.decodeResource(getResources(), R.drawable.redsquare);
    random = new Random();
    redSquareX = 500;
    redSquareY = random.nextInt(400);
}

@Override
protected void onPause() {
    super.onPause();
    gameView.pause();
}

@Override
protected void onResume() {
    super.onResume();
    gameView.resume();
}

public class GameView extends SurfaceView implements Runnable{

    Thread thread = null;
    SurfaceHolder holder;
    boolean running = false;
    Canvas canvas;

    public GameView(Context context) {
        super(context);
    holder = getHolder();

    }

    @Override
    public void run() {

        while(running == true){ 
            if(!holder.getSurface().isValid()){
                continue;
            } 

            canvas = holder.lockCanvas();
            canvas.drawRGB(100, 100, 100);
            canvas.drawBitmap(square, x - (square.getWidth()/2), y - (square.getHeight()/2), null);
            canvas.drawBitmap(redSquare, redSquareX, redSquareY, null);
            holder.unlockCanvasAndPost(canvas);
            y = y + 4;
            redSquareX = redSquareX - 1;

        }

        }

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

    public void resume(){
        running = true;
        thread = new Thread(this);
        thread.start();
    }

 }

@Override
public boolean onTouch(View view, MotionEvent event) {

    try {
        Thread.sleep(50);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    switch(event.getAction()){
    case MotionEvent.ACTION_DOWN:
        y = y - 100;
        break;

    }

    return true;
}
}

【问题讨论】:

    标签: java android eclipse random bitmap


    【解决方案1】:

    我对您的设置有点困惑,但我会试一试。

    if(System.nanoTime() - lastSquareTime > 1500000000) { 
        startRedSquares();  
    }
    

    这段代码不应该在 onCreate 中。 onCreate 只被调用一次,所以它永远不会用这个 if 语句开始创建更多的红色方块。您应该在 run 方法中检查该计时器,即游戏循环。所以,更像这样:

    while(running == true){ 
                if(!holder.getSurface().isValid()){
                    continue;
                } 
    
                canvas = holder.lockCanvas();
                canvas.drawRGB(100, 100, 100);
                canvas.drawBitmap(square, x - (square.getWidth()/2), y - (square.getHeight()/2), null);
                canvas.drawBitmap(redSquare, redSquareX, redSquareY, null);
                holder.unlockCanvasAndPost(canvas);
                y = y + 4;
    
                if(SOME TIMER HAS EXPIRED) {
                    //add another red square to the list
                    //reset timer 
                }
                //DECREMENT THE TIMER 
                //CHECK IF ANY SQUARE IS OFF THE SCREEN AND REMOVE IT FROM LIST
                //LOOP THROUGH LIST AND MOVE ALL REDSQUARES
    
                redSquareX = redSquareX - 1;
    
    }
    

    另外,我还建议为您的 redsquares 创建一个类,然后创建 RedSquare 对象的 ArrayList。然后,当您遍历每个 RedSquare 对象时,可以调用它们自己的方法(updateMovement、checkIfOffScreen、drawRedSquare)或您想调用的任何方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 2014-06-19
      相关资源
      最近更新 更多