2016-11-15 5 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); 
} 

편집에 대해 확인해야합니다 : 당신은 새로운 라인이 갈 때 선이 문자 뒤로 만 점수까지 이미있는 추적 할 필요가 과거. 이미 득점 한 행의 색인을 유지해야합니다. 따라서 해당 색인을 유지할 필드를 작성하십시오. 그것을 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입니다. 그러나 마지막 줄만 테스트하기 때문에 내 대답은 잘못되었습니다. (내 대답은 당신이 "lines"을 언급했을 때'obstacles' 배열의 요소와 같은 것이라고 가정합니다.) –

+0

@nilsllucans - 버그를 수정하기 위해 답을 업데이트했습니다. –

관련 문제