background problems
in
Programming Questions
•
1 year ago
I wasn't sure whether to post this in the other topic I made earlier but as it's a different problem I decided to make a new one. The problem I'm having is that when the animation moves the previous one is still there until the background is redrawn making it look like it has a clone following it, I solved this by drawing a new background each time the key is pressed to move the animation however when this happens it draws over the stars that are scrolling in the background making them disappear for a split second and causing them to flash. How can I get round this so that the stars are permanent and there is no clone following the animation?
- Star [] starCollection = new Star [200];
- Animation aniFly, aniTurnL, aniTurnR;
- float xpos = 500;
- float ypos = 500;
- void setup() {
- size(1000,600);
- frameRate(60);
- aniFly = new Animation("sprite_shipm2_fly",4);
- aniTurnL = new Animation("sprite_shipm2_turnL", 3);
- aniTurnR = new Animation("sprite_shipm2_turnR", 3);
- for (int i = 0; i < 200; i++) {
- float sX = random (200, 800);
- float sY = random (600);
- float sZ = random (2, 8);
- starCollection[i] = new Star (sX, sY, sZ);
- }
- }
- void draw() {
- background (0);
- for (int j = 0; j < 200; j++) {
- starCollection [j].run();
- }
- fill(185, 81, 138);
- rect (0, 0, 200, 600);
- rect (800, 0, 200, 600);
- aniFly.display(xpos, ypos);
- print(xpos);
- }
- void keyPressed() {
- if (key == CODED) {
- if (keyCode == LEFT) {
- background(0);
- fill(185, 81, 138);
- rect (0, 0, 200, 600);
- rect (800, 0, 200, 600);
- aniTurnL.display(xpos, ypos);
- xpos -= 5;
- if (xpos <= 237) {
- xpos = 237;
- }
- } else if (keyCode == RIGHT) {
- background(0);
- fill(185, 81, 138);
- rect (0, 0, 200, 600);
- rect (800, 0, 200, 600);
- aniTurnR.display(xpos, ypos);
- xpos += 5;
- if (xpos >= 765) {
- xpos = 765;
- }
- }
- }
- }
- class Animation {
- PImage[] images;
- int imageCount;
- int frame;
- Animation(String imagePrefix, int count) {
- imageCount = count;
- images = new PImage[imageCount];
- for (int i = 0; i < imageCount; i++) {
- // Use nf() to number format 'i' into four digits
- String filename = imagePrefix + nf(i, 4) + ".gif";
- images[i] = loadImage(filename);
- }
- }
- void display(float xpos, float ypos) {
- frame = (frame+1) % imageCount;
- imageMode(CENTER);
- image(images[frame], xpos, ypos);
- }
- }
- class Star {
- float x = 0;
- float y = 0;
- float z = 0;
- float speed = 2.5;
- Star (float _x, float _y, float _z) {
- x = _x;
- y = _y;
- z = _z;
- }
- void run() {
- display();
- movement();
- }
- void display() {
- noStroke();
- fill (255);
- ellipse (x, y, z, z);
- }
- void movement() {
- y += speed;
- if (y > 600) {
- y = random (-30, 0);
- }
- }
- }
1