我正在尝试做的是写一个类似于“Flappy Bird”的游戏,但是以垂直的方式。这意味着我们拥有玩家和障碍物,从上到下移动,玩家可以在障碍物之间穿过小间隙。我有玩家(现在它是一个小红色矩形),我可以用手指移动它,但是我的障碍物方法不起作用,我看不到它们中的任何一个下降。我在blah
类中写了这样的东西:
ObstacleManager
我认为可能在package com.example.sylwesterpilarz.quackattack;
import android.graphics.Canvas;
import java.util.ArrayList;
import java.lang.System;
public class ObstacleManager {
private ArrayList<Obstacle> obstacles;
private int playerGap;
private int obstacleGap;
private int obstacleHeight;
private int color;
private long startTime;
public ObstacleManager(int playerGap, int obstacleGap, int obstacleHeight, int color) {
this.playerGap = playerGap;
this.obstacleGap = obstacleGap;
this.obstacleHeight = obstacleHeight;
this.color = color;
startTime = System.currentTimeMillis();
obstacles = new ArrayList<>();
populateObstacles();
}
private void populateObstacles() {
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;
}
}
public void update() {
int elapsedTime = (int)(System.currentTimeMillis() - startTime);
startTime = System.currentTimeMillis();
float speed = Constants.SCREEN_HEIGHT/10000.0f;
for(Obstacle ob : obstacles) {
ob.incrementY(speed*elapsedTime);
}
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);
}
}
public void draw(Canvas canvas) {
for(Obstacle ob : obstacles)
ob.draw(canvas);
}
}
方法中存在问题,但删除它不会改变事情。应用程序启动,红色矩形显示,障碍不显示。我的障碍没有出现在起点上是否有可能出错?
答案 0 :(得分:0)
好的,我解决了这个问题。看来这一行:
obstacles.add(0, new Obstacle(obstacleHeight, color, xStart, obstacles.get(0).getRectangle().top - obstacleHeight - obstacleGap, playerGap));
不能有&#34; 0&#34;价值,不知何故,它需要一些其他的加值。将其改为&#34; 10&#34;例如,它就像一个魅力。我以为&#34; 0&#34;是为了起点。