2016-11-15 8 views
0

私はあなたのキャラクターを動かすことができ、そのキャラクターとラインを渡す必要があるゲームを作成しましたが、1つの問題があります。最初の行が画面の一番下に達すると、スコアがカウントを開始します。私はキャラクターがラインギャップを通過するときにスコアが+1になるようにしたい。私は何をすべきか? EDIT:最後の行が下に達したときにもスコアがカウントされます。私は+スコアシステムを作成したい

if (obstacles.get(obstacles.size()-1).getRectangle().top >= Constants.SCREEN_HEIGHT){ 
     int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap)); 
     obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap)); 
     obstacles.remove(obstacles.size()-1); 

     score++; 

    } 

} 

public void draw(Canvas canvas){ 
    for(Obstacle ob : obstacles) 
     ob.draw(canvas); 
    Paint paint = new Paint(); 
    paint.setTextSize(100); 
    paint.setColor(Color.GREEN); 
    canvas.drawText("" + score, 50, 100 + paint.descent()- paint.descent(), paint); 

EDIT:

int currY = -5*Constants.SCREEN_HEIGHT/4, rect; 
    while(currY < 0){ 
     int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH-  playerGap)); 
     obstacles.add(new Obstacle(obstacleHeight, color, xStart, currY, playerGap)); 
     currY += obstacleHeight + obstacleGap; 
    } 
    if (obstacles.get(obstacles.size() - 1).getRectangle().top >= currY) { 
     score++; 
    } 

EDIT2:

private void populateObatacles(){ 
    int currY = -5*Constants.SCREEN_HEIGHT/4; 
    while(currY < 0){ 
     int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap)); 
     obstacles.add(new Obstacle(obstacleHeight, color, xStart, currY, playerGap)); 
     currY += obstacleHeight + obstacleGap; 
    } 
} 

EDIT3:

if (obstacles.get(lastLineScored-1).getRectangle().top >= currY) { 
      score++; 
      lastLineScored--; 
     } 

     if (obstacles.get(obstacles.size()-1).getRectangle().top >= Constants.SCREEN_HEIGHT){ 
      int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap)); 
      obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap)); 
      obstacles.remove(obstacles.size()-1); 
      lastLineScored++; 
     } 
     } 
    } 

public void draw(Canvas canvas){ 
    for(Obstacle ob : obstacles) 
     ob.draw(canvas); 
    Paint paint = new Paint(); 
    paint.setTextSize(100); 
    paint.setColor(Color.GREEN); 
    canvas.drawText("" + score , 50, 100 + paint.descent()- paint.descent(), paint); 

EDIT4: 障害マネージャhttp://pastebin.com/6E77QtHj O bstacle http://pastebin.com/p33mPrat

答えて

0

テストを変更してください。行の先頭がSCREEN_HEIGHTより大きいかどうかを調べる代わりに、文字のY座標より大きいかどうかを確認してください。また、あなたは各

if (obstacles.get(obstacles.size()-1).getRectangle().top >= /** character Y coordinate */) { 
    score++; 
} 

if (obstacles.get(obstacles.size()-1).getRectangle().top >= Constants.SCREEN_HEIGHT){ 
    int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap)); 
    obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap)); 
    obstacles.remove(obstacles.size()-1); 
} 

EDITに対してチェックする必要があります:あなたは、新しい行が行くとき、行が文字を過ぎてのみ、スコアアップ、すでにであるを追跡する必要があります過去。あなたは既にどのラインにスコアがつけられているかの指標を保持する必要があります。そのインデックスを保持するフィールドを作成します。 lastScoredLineとしましょう。 obstacles.size()に初期化する必要があります。ラインがスクリーンから落ちるときは常にそのフィールドを調整する必要があります。

if (obstacles.get(lastLineScored-1).getRectangle().top >= /** character Y coordinate */) { 
    score++; 
    lastLineScored--; 
} 

if (obstacles.get(obstacles.size()-1).getRectangle().top >= Constants.SCREEN_HEIGHT){ 
    int xStart = (int) (Math.random()*(Constants.SCREEN_WIDTH- playerGap)); 
    obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap)); 
    obstacles.remove(obstacles.size()-1); 
    lastLineScored++; 
} 
+0

この投稿を編集しました。それはY座標でなければなりませんか? –

+0

@nilsllucans - それが文字のY座標ならば、yesです。しかし、私の答えは間違っているのは、最後の行だけをテストするからです。 (私の答えは、あなたが "行"を参照したとき、 'obstacles'配列の要素と同じことです)。 –

+0

@nilsllucans - 私はバグを修正するために私の答えを更新しました。 –

関連する問題