精灵每隔几秒绘制一次

时间:2013-10-11 21:45:01

标签: java android

我想在定义的时间间隔内一次又一次地在以下代码中执行此代码

  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) {}
         }
   }
    }

1 个答案:

答案 0 :(得分: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();
       }