My Game Flashes black for a frame when a certain class is added

edited November 2013 in Questions about Code

I'm working on a game and whenever I add a "meteor" entity, my screen flashes black. Could this be by too many commands while creating the instance, or a render issue, or something else? Here is a snippet of code from the class.

class Meteor {
  float radius, meteorSpeed, meteorSize, meteorX, meteorY, value, targetX, targetY, direction;
  float velocityX, velocityY, distance;
  PVector meteorPos;
  boolean big, small, medium, destroyed, rock, metal, gem, godstone, captured;
  int rarity;
  color[] myColors = new color[4];


  Meteor(float _meteorSize) {
    meteorSize = _meteorSize;

    myColors[0] = color(100, 81, 42);
    myColors[1] = color(183, 180, 170); 
    myColors[2] = color(173, 162, 162);
    myColors[3] = color(random(0, 255), random(0, 255), random(0, 255));

    if (meteorSize <=70) {
      small = true;
      radius = random(4, 15);
    }

    if ( 70 < meteorSize && meteorSize <= 95) {
      medium = true; 
      radius = random(25, 45);
    }

    if (meteorSize > 95) {
      big = true; 
      radius = random(50, 75);
    }

    rarity = int(random(1, 100));

    if (rarity >= 0 && rarity <= 65) {
      rock = true;
      value = 3*radius;
    }

    if (rarity > 65 && rarity <= 90) {
      metal = true;
      value = 3*radius*2;
    }

    if (rarity > 90 && rarity < 99) {
      gem = true;
      value = 3*radius*5;
    }

    if (rarity >= 99) {
      godstone = true;
      value = 3*radius*50;
    }


    meteorSpeed = 1;
    meteorX = (random(radius, width-radius));
    meteorY = -radius;


    targetX = meteorX;
    targetY = height + radius + 10;

    rotate(atan2(targetY-meteorY, targetX-meteorX));
    direction = (atan2(targetY-meteorY, targetX-meteorX));
    velocityX = cos(direction)*meteorSpeed;
    velocityY = sin(direction)*meteorSpeed;
  }

  void draw() {
    display();
    update();
  }

  void display() {


    if (rock== true) {
      fill(myColors[0]);
    }

    if (metal == true) {
      fill(myColors[1]);
    }

    if (gem == true) {
      fill(myColors[2]);
    }

    if (godstone == true) {
      fill(myColors[3]);
      fill(random(0, 255), random(0, 255), random(0, 255));
    }

    if (meteorY > height + radius) {
      destroyed = true;
    }

    ellipse(meteorX, meteorY, radius*2, radius*2);
  }

  void update() {

    velocityX = cos(direction)*meteorSpeed;
    velocityY = sin(direction)*meteorSpeed;

    meteorX += velocityX;
    meteorY += velocityY;

    float dx = playerPositionX - meteorX;
    float dy = playerPositionY - meteorY;

    distance = (sqrt((dx*dx)+(dy*dy)));

    if(distance < playerRadius + radius){
      destroyed = true;
      minerals += int(value);
    }
  }
}

Answers

  • Forgot to add, the error started to happening after I added all of the trigonometry functions for movement. Any help is appreciated, thanks.

  • are you drawing anything in your main draw() that isn't setting fill() first?

    fill() sets the global colour and will remain in effect until it's changed (or noFill() is called). perhaps meteor is setting the fill colour and something somewhere else is using it by mistake.

    that said, none of those colours are black...

  • I can't seem to find anything that isn't given a fill, all of my objects on the screen are from classes, which each have their own color. I could post the whole code for my "Level 1" if you would like. To take a look. It mostly consist of Arraylist interacting with each-other checking for collisions and whatnot, nothing really graphically important.

  • Answer ✓

    Does the screen only flash once?

  • I just figured out that the rotation caused the screen to flash for some reason. I now have a separate thread about another error i'm getting with rotation strangely, thanks for all of your answers.

Sign In or Register to comment.