1

このコードを次のコードで定義された間隔で何度も実行したい

  for (Sprite sprite : sprites) {
         sprite.onDraw(canvas);             

          }

私はいくつかの方法を試しましたが、エラーで立ち往生しています。スリープとスレッド拡張クラスですべてのアニメーションのタイミングを合わせているため

    package com.okok;
 import java.util.ArrayList;
 import java.util.List;
 import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;
public class GameView extends SurfaceView {
   private Bitmap bmp;
   private SurfaceHolder holder;
   private GameLoopThread gameLoopThread;
   private List<Sprite> sprites = new ArrayList<Sprite>();
   private long lastClick;
private Bitmap bmpBlood;
private List<TempSprite> temps = new ArrayList<TempSprite>();
private int mint;
   public GameView(Context context) {
         super(context);
         gameLoopThread = new GameLoopThread(this);
         holder = getHolder();
         holder.addCallback(new Callback() {
                @Override
                public void surfaceDestroyed(SurfaceHolder holder) {
                }
                @Override
                public void surfaceCreated(SurfaceHolder holder) {
                       createSprites();
                       gameLoopThread.setRunning(true);
                       gameLoopThread.start();
                }
                @Override
                public void surfaceChanged(SurfaceHolder holder, int format,
                              int width, int height) {
                }
         });
         bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blast);
   }
   private void createSprites() {
         sprites.add(createSprite(R.drawable.greenenact));
         sprites.add(createSprite(R.drawable.greenenact));
         sprites.add(createSprite(R.drawable.greenenact));

   }
   private Sprite createSprite(int resouce) {
         Bitmap bmp = BitmapFactory.decodeResource(getResources(), resouce);
         return new Sprite(this, bmp);
   }
   @Override
   protected void onDraw(Canvas canvas) {
         canvas.drawColor(Color.rgb(21, 181, 195));
         for (int i = temps.size() - 1; i >= 0; i--) {
             temps.get(i).onDraw(canvas);
       }
         for (Sprite sprite : sprites) {
             sprite.onDraw(canvas);             

              }

   }

@Override
   public boolean onTouchEvent(MotionEvent event) {
         if (System.currentTimeMillis() - lastClick > 500) {
                lastClick = System.currentTimeMillis();
             synchronized (getHolder()) {
             float x = event.getX();
                float y =event.getY();

                   for (int i = sprites.size() - 1; i >= 0; i--) {
                        Sprite sprite = sprites.get(i);
                        if (sprite.isCollition(x, y)) {
                              sprites.remove(sprite);
                            temps.add(new TempSprite(temps, this, x, y, bmpBlood));
                              break;
                        }
                    }
                }
         }
         return true;
   }
   }

これは、物を動かすために使用した GameLoopThread クラスです

  package com.okok;

   import android.graphics.Canvas;
   public class GameLoopThread extends Thread {
   static final long FPS = 10;
   private GameView view;
   private boolean running = false;

   public GameLoopThread(GameView view) {
         this.view = view;
   }

   public void setRunning(boolean run) {
         running = run;
   }

   @Override
   public void run() {
         long ticksPS = 1000 / FPS;
         long startTime;
         long sleepTime;
         while (running) {
                Canvas c = null;
                startTime = System.currentTimeMillis();
                try {
                       c = view.getHolder().lockCanvas();
                       synchronized (view.getHolder()) {
                              view.onDraw(c);
                       }
                } finally {
                       if (c != null) {
                              view.getHolder().unlockCanvasAndPost(c);
                       }
                }
                sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
                try {
                       if (sleepTime > 0)
                              sleep(sleepTime);
                       else
                              sleep(10);
                } catch (Exception e) {}
         }
   }
    }
4

1 に答える 1

1

次のようなものを試してください:

 private void createSprites() {
       {
        new Thread(new Runnable() {
            public void run() {
                for (int z =0; z<20; z++ ) // total of 20 sprites
                try
                    {
                    Thread.sleep(5000);   // new enemy every 5 seconds
                    sprites.add(createSprite(R.drawable.image));
                    z++;

                    }
                catch(InterruptedException e)
                    {
                    e.printStackTrace();
                    }
            }
        }).start();
       }
于 2013-11-15T00:00:27.753 に答える