Looking for help with my keyPressed

edited November 2014 in Programming Questions

Hello again,

I'm progressing with my Processing SMS simulator, but I've hit a problem with my code.

The code pulls text from a txt file and should move from a static display to typical sms scroll-down animation. But, it will only do it once.

I have two functions: one (holdBubbles) which shows the three recent texts and another (animateBubbles) which runs the scroll animation. Now, the holdBubbles pops up fine and, when I hit SPACE, the scroll-down animation runs and it returns to the holdBubbles.

The problem is that it won't rerun the animation after that. I'm thinking it may be in the way I have my draw() function set up, but it may also be in the animate function. I've been trying 'all' angles, but I've just hit a wall of confusion.

Could anyone cast an eye over the code and help me see where I'm going wrong? Much appreciated at this stage!

boolean callMethod = true;

PImage backGround;
PImage bubble;

int move = 0;

String[] lines;
String[] latestText;
String prevText;
String currentText;
int caser = 0;
int firstText = 0;
int j =0;

void setup(){
  size(displayWidth, displayHeight);
  backGround = loadImage("background.jpg");
  bubble = loadImage("bubble.png");
  imageMode(CENTER);
  textSize(32);
  textAlign(CENTER);

}

void draw(){

  if(caser == 1){
    println("animate");
    animate();
  }
  else if(caser == 0){
    println("holdBubbles");
    holdBubbles();
  }

  }


void keyPressed(){
  if (keyCode==32){
    caser = 1;
  }
  else{
    caser = 0;
  }
}

void animate(){
    if(callMethod){
    j++;
    animateBubbles();
      if (j==24){
        callMethod = false;
        j = 0;
        return;
       }
    }

  holdBubbles();
  return;
}

//scroll animation
void animateBubbles(){
  tint(255);
  imageMode(CENTER);
  image(backGround, displayWidth/2, displayHeight/2, displayWidth, displayHeight);
  //Load from text file
  String[] lines = loadStrings("sms.txt");
  image(bubble, ((displayWidth/3)*2)-move, displayHeight/2);
  image(bubble, (displayWidth/2)-move, displayHeight/2);
  tint(255, move);
  image(bubble, (displayWidth/3)*2, displayHeight/2);
  tint(255, 255-move);
  image(bubble, displayWidth/3, displayHeight/2);
  rotate(radians(90));
  text(lines[3],425,-960+move);
  text(lines[6],425,-725+move);

  move+=10;
  image(backGround, displayWidth/2, displayHeight/2, displayWidth, displayHeight);
  return;  
}

//static screen
void holdBubbles(){
  tint(255);
  String[] lines = loadStrings("sms.txt");
  image(backGround, displayWidth/2, displayHeight/2, displayWidth, displayHeight);
  image(bubble, ((displayWidth/3)*2), displayHeight/2);
  image(bubble, (displayWidth/2), displayHeight/2);
  image(bubble, displayWidth/3, displayHeight/2);
  rotate(radians(90));
  text(lines[0],425,-965);
  text(lines[3],425,-720);
  text(lines[6],425,-485);
  return;
}

Answers

  • You never reset the value of your move variable.

    Also, do you really need to load the files every time you call the draw() function?

  • edited November 2014

    Thanks for your time, Kevin, Accidentally hit 'accept' there...

    I've reset the move variable:

    void animate(){
        if(callMethod){
        j++;
        animateBubbles();
          if (j==24){
            move = 0;
            callMethod = false;
            j = 0;
            return;
           }
        }
    
      holdBubbles();
    
    }
    

    ...and it seems to be fine. No change on my problem, though. Can't seem to trigger the animation a second time.

    I've been trying to load the sms.txt file before setup(), which would be ideal for checking the for file changes, but it keeps throwing an error: "The file "sms.txt" is missing or inaccessible...".

    I'll keep trying alternative ways, but I'm at the stage of just trying random stuff, as my knowledge has hit a wall...

  • You only ever set callMethod to false.

  • edited November 2014

    you could also reset caser to 0 after line 55

    after line 42 also

    callMethod = true;
    

    ;-)

  • I guess I should have said "You only ever set callMethod to false, after setting it to false the first time."

  • edited November 2014

    you were faster than me

    ;-)

  • Yes! I'm so happy. You may have even cured my cold!

    This is what works:

    void keyPressed(){
      if (keyCode==32){
        callMethod = true;
        caser = 1;
      }
      else{
        caser = 0;
      }
    }
    

    Can't believe I didn't see that. Fried.

    First time caller, very thankful.

    Cheers.

Sign In or Register to comment.