2011-11-23 56 views
1

在我的Android遊戲中,子彈顯得太快太靠近對方!我如何調整他們的行爲? 下面是動畫對象太多子彈(版本2.0)

package game.objects.animation; 

import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Rect; 

abstract class AnimatedObject { 
    protected Bitmap bitmap;  // the animation sequence 
    protected Rect sourceRect; // the rectangle to be drawn from the animation bitmap 
    private int frameNr;  // number of frames in animation 
    private int currentFrame; // the current frame 
    private long frameTicker; // the time of the last frame update 
    private int framePeriod; // milliseconds between each frame (1000/fps) 

    protected int spriteWidth; // the width of the sprite to calculate the cut out rectangle 
    protected int spriteHeight; // the height of the sprite 

    private int x;    // the X coordinate of the object (top left of the image) 
    private int y;    // the Y coordinate of the object (top left of the image) 

    public boolean exist; 

    public AnimatedObject(Bitmap bitmap, int x, int y, int width, int height, int fps, int frameCount) { 

     this.bitmap = bitmap; 
     this.x = x; 
     this.y = y; 
     currentFrame = 0; 
     frameNr = frameCount; 
     spriteWidth = bitmap.getWidth()/frameCount; 
     spriteHeight = bitmap.getHeight(); 
     sourceRect = new Rect(0, 0, spriteWidth, spriteHeight); 
     framePeriod = 1000/ fps; 
     frameTicker = 0l; 

     exist = true; 
    } 

    private void update(long gameTime) { 

     //if (gameTime > frameTicker + framePeriod) { 
      //frameTicker = gameTime; 
      // increment the frame 
      currentFrame++; 
      if (currentFrame >= frameNr) { 
       currentFrame = 0; 
       } 
      //} 
     // define the rectangle to cut out sprite 
     this.sourceRect.left = currentFrame * spriteWidth; 
     this.sourceRect.right = this.sourceRect.left + spriteWidth; 

    } 

    public void animate(){ 

     if (currentFrame <= frameNr){ 
      update(0); 
     } 
    } 

    public void draw(Canvas canvas) { 
     // where to draw the sprite 
     Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight); 
     canvas.drawBitmap(bitmap, sourceRect, destRect, null); 
    } 

    public boolean checkCollisons(Rect another) 
    { 
     return Rect.intersects(new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight), another); 
    } 

    public void destroy() 
    { 
     exist = false; 
    } 


    public Bitmap getBitmap() { 
     return bitmap; 
    } 
    public void setBitmap(Bitmap bitmap) { 
     this.bitmap = bitmap; 
    } 
    public Rect getSourceRect() { 
     return sourceRect; 
    } 
    public void setSourceRect(Rect sourceRect) { 
     this.sourceRect = sourceRect; 
    } 
    public int getFrameNr() { 
     return frameNr; 
    } 
    public void setFrameNr(int frameNr) { 
     this.frameNr = frameNr; 
    } 
    public int getCurrentFrame() { 
     return currentFrame; 
    } 
    public void setCurrentFrame(int currentFrame) { 
     this.currentFrame = currentFrame; 
    } 
    public int getFramePeriod() { 
     return framePeriod; 
    } 
    public void setFramePeriod(int framePeriod) { 
     this.framePeriod = framePeriod; 
    } 
    public int getSpriteWidth() { 
     return spriteWidth; 
    } 
    public void setSpriteWidth(int spriteWidth) { 
     this.spriteWidth = spriteWidth; 
    } 
    public int getSpriteHeight() { 
     return spriteHeight; 
    } 
    public void setSpriteHeight(int spriteHeight) { 
     this.spriteHeight = spriteHeight; 
    } 
    public int getX() { 
     return x; 
    } 
    public void setX(int x) { 
     this.x = x; 
    } 
    public int getY() { 
     return y; 
    } 
    public void setY(int y) { 
     this.y = y; 
    } 


} 

類子彈

package game.objects.animation; 


import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Rect; 
import android.graphics.drawable.Drawable; 

public class Bullet extends AnimatedObject{ 

    private int mSpeedY; 

    public Bullet(Bitmap bitmap, int x, int y, int width, int height, int fps, 
      int frameCount) { 
     super(bitmap, x, y, width, height, fps, frameCount); 
     // TODO Auto-generated constructor stub 
     mSpeedY = 10; 
     setFramePeriod(5000/ fps); 

    } 

    public void draw(Canvas canvas) { 
     // where to draw the sprite 
     setY(getY() - mSpeedY); 
     Rect destRect = new Rect(getX(), getY(), getX() + spriteWidth, getY() + spriteHeight); 
     canvas.drawBitmap(bitmap, sourceRect, destRect, null); 

    } 

    public void animate()//анимация движения 
    { 
     super.animate(); 
     setY(getY() - mSpeedY); 
     // checkBorders(rect); 
    } 



} 

類遊戲主循環,或者它的英文怎麼叫(我不是母語的人,對不起)

a picture of it

package game.view; 

import game.main.R; 
import game.objects.animation.Bullet; 
import game.objects.animation.Explosion; 
import game.objects.animation.ScrollingElement; 
import game.objectsmain.Player; 

import java.util.ArrayList; 

import android.app.Activity; 
import android.content.Context; 
import android.content.res.Resources; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.drawable.Drawable; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.SurfaceHolder; 

public class ViewManager extends Thread 
{ 
    private static final int FIELD_WIDTH = 480; 
    private static final int FIELD_HEIGHT = 800; 
    private static final int dist = 100; 

    public int touchedX, touchedY; 

    /** Область, на которой будем рисовать */ 
    private SurfaceHolder mSurfaceHolder; 

    /** Состояние потока (выполняется или нет. Нужно, чтобы было удобнее прибивать поток, когда потребуется) */ 
    private boolean mRunning; 

    /** Стили рисования */ 
    private Paint mPaint; 

    /** The drawable to use as the background of the animation canvas */ 
    private Bitmap mBackgroundImage; 
    private Bitmap mLinesImage; 
    private Bitmap mExplosionImage; 
    private Bitmap mBulletImage; 
    private Drawable mPlayerImage; 

    private ArrayList<ScrollingElement> mScrollEls = new ArrayList<ScrollingElement>(); 
    private ArrayList<Bullet> mBullets = new ArrayList<Bullet>(); 
    private ArrayList<Explosion> mBang = new ArrayList<Explosion>(); 
    private Player mHero; 

    // desired fps 
    private final static int MAX_FPS = 50; 
    // maximum number of frames to be skipped 
    private final static int MAX_FRAME_SKIPS = 5; 
    // the frame period 
    private final static int FRAME_PERIOD = 1000/MAX_FPS; 

    private Explosion mBoom;//объект класса взрыв 

    public ViewManager(SurfaceHolder surfaceHolder, Context context) 
    { 
     mSurfaceHolder = surfaceHolder; 
     mRunning = false; 
     Resources res = context.getResources(); 
     mExplosionImage = BitmapFactory.decodeResource(res, R.drawable.explosion); 
     mBackgroundImage = BitmapFactory.decodeResource(res, R.drawable.bckgr1); 
     mLinesImage = BitmapFactory.decodeResource(res, R.drawable.lines3); 
     mPlayerImage = res.getDrawable(android.R.drawable.star_big_on); 
     mBulletImage = BitmapFactory.decodeResource(res, R.drawable.bullet3); 
     InitElements(res); 
     mHero = new Player(mPlayerImage, 240, 400); 
     mBoom = new Explosion(mExplosionImage, 200, 500, 64, 64, 7, 7); 

    } 


    /** 
     * Задание состояния потока 
     * @param running 
     */ 
     public void setRunning(boolean running) 
     { 
      mRunning = running; 
     } 

     public void run() 
     { 
      while (mRunning) 
      { 
       Canvas canvas = null; 
       try 
       { 
        // подготовка Canvas-а 
        canvas = mSurfaceHolder.lockCanvas(); 
        synchronized (mSurfaceHolder) 
        { 

         mHero.update(); 
         // собственно рисование 
         addBullets(); 
         doDraw(canvas); 
         //mImage.draw(canvas); 

         synchronized (mBullets) { 
          for (Bullet bullet: mBullets) { 
           //explosion if collides 
           if(bullet.checkCollisons(mHero.getRect())) 
           { 
            mBang.add(new Explosion(mExplosionImage, mHero.getXCoord(), mHero.getYCoord(), 64, 64, 7, 7)); 
            mBang.get(0).draw(canvas); 
            mBang.get(0).animate(); 
           }; 
           //bullet.animate(); 
          } 
         } 

        } 
       } 
       catch (Exception e) { } 
       finally 
       { 
        if (canvas != null) 
        { 
         mSurfaceHolder.unlockCanvasAndPost(canvas); 
        } 
       } 
      } 
     } 

     public boolean gettouch = false;//in class with surface golder there are touchlistener works with this variable 

     private void doDraw(Canvas canvas) { 
     // TODO Auto-generated method stub 
     canvas.drawBitmap(mBackgroundImage, 0, 0, null); 
     //отрисовка анимации 
      synchronized (mScrollEls) { 
       for (ScrollingElement element: mScrollEls) { 
        element.doDraw(canvas, mLinesImage); 
        element.animate(mLinesImage); 
       } 
      } 
      mHero.draw(canvas); 
      //mBoom.draw(canvas); 
      /*if(gettouch == true){ 

       mBoom.draw(canvas); 
       mBoom.getCurrentFrame(); 
       mBoom.animate(); 
     }*/ 

      synchronized (mBullets) { 
       for (Bullet bullet: mBullets) { 

        bullet.animate(); 
        bullet.draw(canvas); 
        //bullet.animate(); 
       } 
      } 

    } 

    private void addBullets()//элементы для анимации заднего плана 
     { 
      if(gettouch) 
      { 
       mBullets.add(new Bullet(mBulletImage, touchedX, touchedY, 22, 52, 3, 3)); 
      } 
     } 



    public boolean touched(boolean x) 
    { 
     return x; 
    } 
} 
+1

你需要描述你想要發生的事情,而不僅僅是發生了什麼。 –

回答

1

您正在每幀添加項目符號。相反,只能添加每隔幾幀或每隔一段時間。例如,如果您想要在某個時間間隔內完成此操作,則在添加項目符號之前,請檢查當前時間是否與上次添加項目符號加上間隔時間相同。

// fire every 500 ms 
private static long INTERVAL = 500; 
private long mLastFiredTime; 
private void addBullets()//элементы для анимации заднего плана 
{ 
    if(gettouch) 
    { 
     long now = System.currentTimeMillis(); 
     if (now > mLastFiredTime + INTERVAL) { 
      mBullets.add(new Bullet(mBulletImage, touchedX, touchedY, 22, 52, 3, 3)); 
      mLastFiredTime = now; 
     } 
    } 
} 
+0

現在我明白了!謝謝! – MarkMark