2017-05-02 11 views
0

화면을 중심으로 움직이는 스프라이트가있는 안드로이드 게임이 있습니다. 한 명에게 명중하면 점수에 추가되거나 점수에서 벗어나 사라집니다.Android App의 임시 스프 라이트

내가하고 싶은 것은 피가 튈 때 피가 튄다는 것입니다.

나는 이것을 실제로 달성했지만 스프라이트가 닿은 곳에 피가 튀지 않는다. 항상 오른쪽 하단 코너로 이동합니다. 내 문제가 어디 있는지 알 수있는 사람이 있는지 궁금 해서요. 특히 doDraw 및 GameView 방법에서,

public class TempSprite { 

private float x; 
private float y; 
private Bitmap bmp; 
private int life = 12; 
private List<TempSprite> temps; 

public TempSprite(List<TempSprite> temps, GameView gameView, float x, 
        float y, Bitmap bmp) { 
    this.x = Math.min(Math.max(x - bmp.getWidth()/2, 0), 
      gameView.getWidth() - bmp.getWidth()); 
    this.y = Math.min(Math.max(y - bmp.getHeight()/2, 0), 
      gameView.getHeight() - bmp.getHeight()); 
    this.bmp = bmp; 
    this.temps = temps; 
} 

public void onDraw(Canvas canvas) { 
    update(); 
    canvas.drawBitmap(bmp, x, y, null); 
} 

private void update() { 
    if (--life < 1) { 
     temps.remove(this); 
    } 
} 

} 

다른 모든 코드가 GameView 클래스에 있습니다 :

난 그냥 혈액 튄의 TempSprite 클래스가이 아래입니다

public class GameView extends SurfaceView implements SurfaceHolder.Callback { 

private final Bitmap bmpBlood; 
/* Member (state) fields */ 
private GameLoopThread gameLoopThread; 
private Paint paint; //Reference a paint object 
/** The drawable to use as the background of the animation canvas */ 
private Bitmap mBackgroundImage; 
// For creating the game Sprite 
private Sprite sprite; 
private BadSprite badSprite; 
// For recording the number of hits 
private int hitCount; 
// For displaying the highest score 
private int highScore; 
// To track if a game is over 
private boolean gameOver; 
// To play sound 
private SoundPool mySound; 
private int zapSoundId; 
private int screamSoundId; 
// For multiple sprites 
private ArrayList<Sprite> spritesArrayList; 
private ArrayList<BadSprite> badSpriteArrayList; 
// For the temp blood image 
private List<TempSprite> temps = new ArrayList<TempSprite>(); 

//int backButtonCount = 0; 

private void createSprites() { 
    // Initialise sprite object 
    spritesArrayList = new ArrayList<>(); 
    badSpriteArrayList = new ArrayList<>(); 

    for (int i = 0; i < 20; i++) { 

     spritesArrayList.add(new Sprite(this)); 
     badSpriteArrayList.add(new BadSprite(this)); 
    } 


} 

public GameView(Context context) { 
    super(context); 
    // Focus must be on GameView so that events can be handled. 
    this.setFocusable(true); 
    // For intercepting events on the surface. 
    this.getHolder().addCallback(this); 
    // Background image added 
    mBackgroundImage = BitmapFactory.decodeResource(this.getResources(), R.drawable.castle); 
    // Populate multiple sprites 
    //createSprites(); 
    //Vibrator vibe = (Vibrator) sprite.getSystemService(Context.VIBRATOR_SERVICE); 
    //vibe.vibrate(500); 
    // For the temp blood splatter 
    bmpBlood = BitmapFactory.decodeResource(getResources(), R.drawable.blood1); 
    //Sound effects for the sprite touch 
    mySound = new SoundPool(10, AudioManager.STREAM_MUSIC, 0); 
    screamSoundId = mySound.load(context, R.raw.scream, 1); 
    zapSoundId = mySound.load(context, R.raw.zap, 1); 


} 

/* Called immediately after the surface created */ 
public void surfaceCreated(SurfaceHolder holder) { 
    createSprites(); 
    // We can now safely setup the game start the game loop. 
    ResetGame();//Set up a new game up - could be called by a 'play again option' 
    mBackgroundImage = Bitmap.createScaledBitmap(mBackgroundImage, getWidth(), getHeight(), true); 
    gameLoopThread = new GameLoopThread(this.getHolder(), this); 
    gameLoopThread.running = true; 
    gameLoopThread.start(); 
} 



// For the countdown timer 
private long startTime; // Timer to count down from 
private final long interval = 1 * 1000; // 1 sec interval 
private CountDownTimer countDownTimer; // Reference to the class 
private boolean timerRunning = false; 
private String displayTime; // To display the time on the screen 


//To initialise/reset game 
private void ResetGame(){ 
    /* Set paint details */ 
    paint = new Paint(); 
    paint.setColor(Color.WHITE); 
    paint.setTextSize(20); 
    sprite = new Sprite(this); 
    hitCount = 0; 
    // Set timer 
    startTime = 10; // Start at 10s to count down 
    // Create new object - convert startTime to milliseconds 
    countDownTimer = new MyCountDownTimer(startTime*1000, interval); 
    countDownTimer.start(); // Start the time running 
    timerRunning = true; 
    gameOver = false; 


} 



// Countdown Timer - private class 
private class MyCountDownTimer extends CountDownTimer { 

    public MyCountDownTimer (long startTime, long interval) { 
     super(startTime, interval); 
    } 

    public void onFinish() { 
     //displayTime = "Time is up!"; 
     timerRunning = false; 
     countDownTimer.cancel(); 
     gameOver = true; 
    } 
    public void onTick (long millisUntilFinished) { 
     displayTime = " " + millisUntilFinished/1000; 
    } 
} 


//This class updates and manages the assets prior to drawing - called from the Thread 
public void update(){ 


} 

/** 
* To draw the game to the screen 
* This is called from Thread, so synchronisation can be done 
*/ 
@SuppressWarnings("ResourceAsColor") 
public void doDraw(Canvas canvas) { 
    canvas.drawBitmap(mBackgroundImage, 0, 0, null); 

    if (!gameOver) { 

     sprite.draw(canvas); 
     //paint.setColor(R.color.red); 
     canvas.drawText("Time Remaining: " + displayTime, 35, 50, paint); 
     canvas.drawText("Number of hits: " + hitCount, 35, 85, paint); 
     // Draw the blood splatter 
     for (int i = temps.size() - 1; i >= 0; i--) { 
      temps.get(i).onDraw(canvas); 
     } 
     // Draw all the objects on the canvas 
     for (int i = 0; i < spritesArrayList.size(); i++) { 
      Sprite sprite = spritesArrayList.get(i); 
      sprite.draw(canvas); 
     } 
     for (int i = 0; i < badSpriteArrayList.size(); i++) { 
      BadSprite badSprite = badSpriteArrayList.get(i); 
      badSprite.draw(canvas); 
     } 
    } else { 
     canvas.drawText("Game Over!", 35, 50, paint); 
     canvas.drawText("Your score was: " + hitCount, 35, 80, paint); 
     canvas.drawText("To go back home,", 280, 50, paint); 
     canvas.drawText("press the 'back' key", 280, 70, paint); 
    } 


} 


//To be used if we need to find where screen was touched 
public boolean onTouchEvent(MotionEvent event) { 

    for (int i = spritesArrayList.size()-1; i>=0; i--) { 
     Sprite sprite = spritesArrayList.get(i); 
     if (sprite.wasItTouched(event.getX(), event.getY())) { 
      mySound.play(zapSoundId, 1.0f, 1.0f, 0,0, 1.5f); 
      spritesArrayList.remove(sprite); 
      //temps.add(new TempSprite(temps, this, x, y, bmpBlood)); 
      hitCount--; 


      return super.onTouchEvent(event); 
     } 
     for (int i1 = badSpriteArrayList.size()-1; i1>=0; i1--) { 
      BadSprite badSprite = badSpriteArrayList.get(i1); 
      if (badSprite.wasItTouched(event.getX(), event.getY())) { 
       mySound.play(screamSoundId, 1.0f, 1.0f, 0, 0, 1.5f); 
       badSpriteArrayList.remove(badSprite); 
       temps.add(new TempSprite(temps, this, x, y, bmpBlood)); 
       hitCount++; 


       return super.onTouchEvent(event); 
      } 
     } 
    } 
    //else { 
    return true; 
    // } 
} 

public void surfaceDestroyed(SurfaceHolder holder) { 
    gameLoopThread.running = false; 

    // Shut down the game loop thread cleanly. 
    boolean retry = true; 
    while(retry) { 
     try { 
      gameLoopThread.join(); 
      retry = false; 
     } catch (InterruptedException e) {} 
    } 
} 

public int getHitCount() { 

    return hitCount; 

} 

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 

} 


} 

언제나처럼, 어떤 도움을 주시면 감사하겠습니다.

감사

답변

0

이 다음 코드로 해결되었다 아래

public class TempSprite { 

private float x; 
private float y; 
private Bitmap bmp; 
private int life = 12; 
private List<TempSprite> temps; 

public TempSprite(List<TempSprite> temps, GameView gameView, float x, 
        float y, Bitmap bmp) { 
    /*this.x = Math.min(Math.max(x - bmp.getWidth()/2, 0), 
      gameView.getWidth() - bmp.getWidth()); 
    this.y = Math.min(Math.max(y - bmp.getHeight()/2, 0), 
      gameView.getHeight() - bmp.getHeight());*/ 
    this.x = x; 
    this.y = y; 
    this.bmp = bmp; 
    this.temps = temps; 
} 

public void onDraw(Canvas canvas) { 
    update(); 
    canvas.drawBitmap(bmp, x, y, null); 
} 

private void update() { 
    if (--life < 1) { 
     temps.remove(this); 
    } 
} 

} 

주석 처리 된 부분이 어디 당신이 볼 수 있고 그들이으로 대체되었다 무엇인가. 나는 일을 좀 복잡하게하려고 노력했다.