I’ve been trying to recreate Flappy Bird using Processing within Java. Whenever I try to add spikes to it, it always will mess up with the sizes after the first 3 pairs of spikes (seems to be after spikeCount+1 pairs) and every now and then afterwards. If you know the problem or could help me fix my code, that would be great.
This is the “glitch”:
Picture of normal size in comments.
This is the code of my main class where the spikes are created:
public void fillSpikes() {
float xPos = this.width;
for(int i = 0; i < spikeCount; i++) {
spikes[i] = new Spike(this, speed, xPos);
xPos = xPos + spikeGap;
}
}
@Override
public void settings() {
size(screenWidth, screenHeight);
background = new Background(this, speed);
ground = new Ground(this, speed);
spikeGap = this.width / 2f;
fillSpikes();
}
@Override
public void draw() {
background.step();
background.render();
for(int i = 0; i < spikeCount; i++) {
spikes[i].step();
spikes[i].render();
}
ground.step();
ground.render();
}
And here is the code from the Spike class itself.
scaleX does stay the same all the time.
public Spike(PApplet sketch, float speed, float xPos) {
this.sketch = sketch;
this.speed = speed;
spikeX = xPos;
// Point in the middle of the gap
spikeY = sketch.random(sketch.height / 8f, 5f * (sketch.height / 8f));
scaleX = sketch.width / 10.0f;
gap = sketch.height / 4f;
spikeTop = sketch.loadImage(imagePathTop);
spikeBot = sketch.loadImage(imagePathBot);
}
public float getX() {
return spikeX;
}
public float getWidth() {
return scaleX;
}
public void step() {
spikeX = spikeX - speed;
if(spikeX <= -(this.getWidth())) {
spikeX = sketch.width;
}
}
public void render() {
sketch.image(spikeBot, spikeX, spikeY + gap / 2f, scaleX, sketch.height);
sketch.image(spikeTop, spikeX, spikeY - gap / 2f - sketch.height, scaleX,
sketch.height);
}