2016-06-11 65 views
1

嘿傢伙即時通訊的一種,所以不要對我很難。 我正在爲Android設計一款2D遊戲我有一些麻煩。 它幾乎完成,但我想在我的遊戲中收集硬幣,並且我希望它們隨機產生我的障礙物。我已經完成了硬幣的主要代碼,他們在我的障礙物上盤旋,你可以收集它們,但我不知道我怎麼能讓他們隨機產卵:我希望你能幫助我在這裏是我的代碼: 我的硬幣類:與我的2D安卓遊戲有問題

enter code herepublic class Coin extends GameObject{ 
private Bitmap spritesheet; 
private double dya; 
private boolean playing; 
private long startTime; 
private Animation animation = new Animation(); 
private int num = 0; 
private Obsticals obsticals; 

public Coin(Bitmap res, int w, int h, int numFrames) { 
    x = GamePanel.WIDTH + 20; 
    y = GamePanel.HEIGHT - GamePanel.HEIGHT/4 - 200; 
    dy = 0; 
    dx = +GamePanel.MOVESPEED; 
    height = h; 
    width = w; 


    Bitmap[] image = new Bitmap[numFrames]; 
    spritesheet = res; 

    for (int i = 0; i < image.length; i++) 
    { 
     image[i] = Bitmap.createBitmap(spritesheet, i*width, 0, width, height); 
    } 
    animation.setFrames(image); 
    animation.setDelay(100); 

} 
public void update() 
{ 
     if (y <= 50) { 
      resetO(); 
     } 
     x += dx; 
     long elapsed = (System.nanoTime() - startTime)/1000000; 
     if (elapsed > 10000) { 
      dx = dx - 1; 
      startTime = System.nanoTime(); 
      System.out.println(dx); 
      num = num + 30; 
     } 
     if (dx <= -25) { 
      dx = -24; 
     } 
     if (num >= 200) { 
      num = 200; 
     } 

} 
public void draw(Canvas canvas) 
{ 
    canvas.drawBitmap(animation.getImage(),x,y,null); 


} 
public void resetO(){ 
    x = GamePanel.WIDTH + 20; 
    y = GamePanel.HEIGHT - GamePanel.HEIGHT/4 - 200; 
    dy = 0; 
    dx = +GamePanel.MOVESPEED; 
    num = 0; 

} 
public void coinCollected(){ 

    resetO(); 
} 


} 

這裏是我的GamePanel代碼:

公共類的GamePanel擴展SurfaceView實現SurfaceHolder.Callback {

public static final int WIDTH = 856; 
public static final int HEIGHT = 480; 
public static int MOVESPEED = -15; 

//other classes 
private MainThread thread; 
private background bg; 
private busch busch; 
private Ground ground; 
private Player player; 
private Obsticals obsticals; 
private sun sun; 
private Coin coin; 
private CoinSymbol coinSymbol; 

private Random rand = new Random(); 

public boolean sliden = false; 

private int coinCounter = 0; 

//SharedPreferences for coins 
SharedPreferences coinprefs; 
SharedPreferences.Editor coinprefsEditor; 

// SharedPreferences for highscore 
SharedPreferences prefs; 
SharedPreferences.Editor prefsEditor; 
int highscore; 
int Highstring; 
final String KEY1 = "key1"; 

public enum STATE{ 
    MENU, 
    INGAME, 
    GAMEOVER; 
}; 
private STATE State = STATE.MENU; 

public GamePanel(Context context) { 

    super(context); 
    //add the callback to the surfaceholder to intercept events 
    getHolder().addCallback(this); 

    coinprefs = context.getSharedPreferences("Coinprefsdatei", Context.MODE_PRIVATE); 
    coinprefsEditor = coinprefs.edit(); 

    prefs = context.getSharedPreferences("prefsdatei", Context.MODE_PRIVATE); 
    prefsEditor = prefs.edit(); 

    thread = new MainThread(getHolder(), this); 

    //make gamePanel focusable so it can handle events 
    setFocusable(true); 
} 
public void GameOver(){ 
    State = STATE.GAMEOVER; 
} 
public void newgame(){ 
    MOVESPEED = -15; 
} 
@Override 
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { 
} 

@Override 
public void surfaceDestroyed(SurfaceHolder holder) { 
    boolean retry = true; 
    while (retry) { 
     try { 
      thread.setRunning(false); 
      thread.join(); 

     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     retry = false; 
    } 

} 

@Override 
public void surfaceCreated(SurfaceHolder holder) { 
    //checkt highscore 
    loadScore(); 
    loadCoins(); 
    //malt alle bilder 
    sun = new sun(BitmapFactory.decodeResource(getResources(), R.drawable.sonne)); 
    bg = new background(BitmapFactory.decodeResource(getResources(), R.drawable.baum)); 
    busch = new busch(BitmapFactory.decodeResource(getResources(), R.drawable.applandschaft)); 
    ground = new Ground(BitmapFactory.decodeResource(getResources(), R.drawable.weg)); 
    coinSymbol = new CoinSymbol(BitmapFactory.decodeResource(getResources(), R.drawable.coinsymbol)); 
    //Breite, Höhe, Frames 
    player = new Player(BitmapFactory.decodeResource(getResources(), R.drawable.test), 115, 120, 8); 
    obsticals = new Obsticals(BitmapFactory.decodeResource(getResources(), R.drawable.hindernisnew), 75, 150, 1); 
    coin = new Coin(BitmapFactory.decodeResource(getResources(), R.drawable.coin), 40, 40, 4); 
    //we can safely start the game loop 
    thread.setRunning(true); 
    thread.start(); 

} 
public void saveHighscore(){ 
    highscore = player.getScore(); 
    prefsEditor.putInt(KEY1,highscore); 
    prefsEditor.commit(); 
} 
public void loadScore(){ 
    Highstring = prefs.getInt(KEY1,0); 
} 
public void loadCoins() { coinCounter = coinprefs.getInt(KEY1,0); } 
public void saveCoins() { 
    coinprefsEditor.putInt(KEY1,coinCounter); 
    coinprefsEditor.commit(); 
} 

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    if(event.getAction()==MotionEvent.ACTION_DOWN){ 
     if(State == STATE.MENU){ 
      State = STATE.INGAME; 

     } 
     if(!player.getPlaying() && State == STATE.INGAME) 
     { 
      player.setPlaying(true); 

     } 
     else if (State == STATE.INGAME) 
     { 
      player.setUp(true); 
     } 
     else if (State == STATE.GAMEOVER) 
     { 
      player.resetScore(); 
      player.resetP(); 
      player.setPlaying(true); 
      State = STATE.INGAME; 
      obsticals.resetO(); 
      coin.resetO(); 
      //newgame(); 
     } 
     return true; 

    } 
    if(event.getAction()==MotionEvent.ACTION_UP) 
    { 
     player.setUp(false); 
     return true; 
    } 

    return super.onTouchEvent(event); 
} 

public void update() { 
    if (player.getPlaying() == true && State == STATE.INGAME) { 
     bg.update(); 
     busch.update(); 
     player.update(); 
     ground.update(); 
     obsticals.update(); 
     coin.update(); 
     collisionboolen(player, obsticals); 
     collectCoin(player, coin); 
    } 
    } 

public boolean collisionboolen(GameObject player, GameObject obsticals) 
{ 
    if(Rect.intersects(player.getRectangle(),obsticals.getRectangle())) 
    { 
     checkScore(); 
     saveCoins(); 
     State = STATE.GAMEOVER; 
     ground.restartGround(); 
     return true; 


    } 
    return false; 
} 
public boolean collectCoin(GameObject player, GameObject coin) 
{ 
    if(Rect.intersects(player.getRectangle(),coin.getRectangle())) 
    { 
     coidcollectet(); 
     coinCounter += 1; 
     return true; 
    } 
    return false; 
} 
public void coidcollectet(){coin.coinCollected();} 
public void stopPlay(){ 
    player.setPlaying(false); 

} 
public void checkScore(){ 
    if (player.score > Highstring) 
    { 
     saveHighscore(); 
     loadScore(); 
    }else { 
     loadScore(); 
    } 
} 
public void startMusik(){ 

} 

@Override 
public void draw(Canvas canvas) { 
    final float scaleFactorX = getWidth()/(WIDTH*1.f); 
    final float scaleFactorY = getHeight()/(HEIGHT*1.f); 
    if (canvas != null && State == STATE.INGAME) { 
     final int savedState = canvas.save(); 

     canvas.scale(scaleFactorX, scaleFactorY); 
     sun.draw(canvas); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(50); 
     paint.setTextAlign(Paint.Align.RIGHT); 
     canvas.drawText(""+coinCounter, WIDTH - 50 , 50 , paint); 
     bg.draw(canvas); 
     coinSymbol.draw(canvas); 
     busch.draw(canvas); 
     ground.draw(canvas); 
     player.draw(canvas); 
     obsticals.draw(canvas); 
     coin.draw(canvas); 
     canvas.restoreToCount(savedState); 


    }else if (canvas != null && State == STATE.MENU){ 

     canvas.scale(scaleFactorX, scaleFactorY); 
     sun.draw(canvas); 
     bg.draw(canvas); 
     coinSymbol.draw(canvas); 
     busch.draw(canvas); 
     ground.draw(canvas); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(100); 
     paint.setTextAlign(Paint.Align.CENTER); 
     canvas.drawText("Start Game", WIDTH/2 , HEIGHT/2 , paint); 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(50); 
     canvas.drawText("touch to start!", WIDTH/2 , HEIGHT/2 + 100 , paint); 
     paint.setTextAlign(Paint.Align.RIGHT); 
     canvas.drawText(""+coinCounter, WIDTH - 50 , 50 , paint); 
     paint.setTextAlign(Paint.Align.CENTER); 
     paint.setTextSize(20); 
     canvas.drawText("Highscore = "+Highstring, WIDTH/2 , HEIGHT/2 + 150 , paint); 


    }else if (canvas != null && State == STATE.GAMEOVER){ 

    canvas.scale(scaleFactorX, scaleFactorY); 
     canvas.drawColor(Color.BLACK); 
     sun.draw(canvas); 
     bg.draw(canvas); 
     coinSymbol.draw(canvas); 
     busch.draw(canvas); 
     ground.draw(canvas); 
     Paint paint = new Paint(); 
     paint.setColor(Color.BLACK); 
     paint.setTextSize(100); 
     paint.setTextAlign(Paint.Align.CENTER); 
     canvas.drawText("GAME OVER", WIDTH/2 , HEIGHT/2 , paint); 
     paint.setTextSize(50); 
     canvas.drawText("touch to restart!", WIDTH/2 , HEIGHT/2 + 100 , paint); 
     paint.setTextAlign(Paint.Align.RIGHT); 
     canvas.drawText(""+coinCounter, WIDTH -50 , 50 , paint); 
     paint.setTextAlign(Paint.Align.CENTER); 
     paint.setTextSize(20); 
     canvas.drawText("Highscore = "+Highstring, WIDTH/2 , HEIGHT/2 + 150 , paint); 




    } 
} 
} 

我希望你能幫幫我!

回答

0

這裏是一個函數,它將一個球生成一個隨機位置。希望你能適應你的需求:

public void spawn() { 
    /** 
    * Spawns the ball at a random position 
    */ 
    Random random = new Random(); 

    radius = random.nextFloat() * 100f + 50f; 

    float velocity_x = random.nextFloat() * 70.0f + 40.0f; 
    float position_y = random.nextFloat() * (screen_height - 120 - radius); 

    position = new Vector(screen_width, position_y); 
    paused_velocity = velocity = new Vector(-velocity_x, 0.0f); 

} 

來源:Virajmahesh | GitHub:Falling Balls