[Solved] how to add one at a time ? Logic Problem

edited March 2014 in Questions about Code

Here I am adding an element with every four incremental hits. Here, only one element should add every set = 4 ,8 ,12,16,20 ..... but it is not happening can any one help me with this

void draw(){
         sets = hits;
          if (sets > 0 && sets%4==0) {
            Balls P = new Balls();
            Ballscollection.add(P);
            println(sets);
          }
        hits++;

}

Answers

  • Answer ✓

    If I change the code as this:

    int sets, hits;
    void draw() {
      sets = hits;
      if (sets > 0 && sets % 4 == 0) {
    //    Balls P = new Balls();
    //    Ballscollection.add(P);
        println(sets);
      }
      hits++;
    }
    

    I see the value to increase by 4.

    Notice I println() sets, not set.

  • edited November 2013

    though it is printing sets correctly but when I add balls in ArrayList it adds more than one I dont know why this is happening.

  • Answer ✓

    Well, it adds one "balls" (why the plural on the class name?) every 4 frames. At 60 frames per second, it adds quickly lot of balls... Not sure what is your problem, exactly.

  • Problem is, I want to add one ball at one time which is not happening but I have the same thought as yours may be because of high frame Rate is adding more balls so can you tell me how to avoid this and yes I can't slow down my frame Rate.

  • Answer ✓

    Just use if (frameCount % FREQUENCY == 0) {}

    Where FREQUENCY is the # of frames to skip.

  • edited November 2013

    Thanks GoToLoop ! but I have to add new ball after score gets increased by 4 unit so it is like as soon as you scored 4,8,12,16,20 ...... a new ball gets added every time in the game.

  • Answer ✓

    Then don't increment hits on each frame! Only when the score is increased.

Sign In or Register to comment.