Turn on and off an animation controlled by keys

Hello,

I have an animation of an windmill which I would like to turn on and off by keys. I'd tried to control the animation with the with loop and noLoop. Unfortunately, I achieve only that the wheel turns and then to stop it but I can't start it again. Below you find the main code and the class wind mill.

Thank you!

* main

//Declaration variables
PImage windmillBase;
PImage windmillWheel;

boolean rotation1 = true;
boolean rotation2 = true;

windmill windmill;
windmill windmill2;

void setup()

  {
    size (840,595);
        //draw background  
    background(#BCFCA1);


     // Load images

     windmillBase=loadImage("pic_windmill_1.png");
     windmillWheel=loadImage("pic_windmill_2.png");

     // call classes + parameter
     windmill = new windmill (550, 500); // new instanz windmill base 1
     windmill2 = new windmill (700, 500); // new instanz windmill 2

  }

void draw(){ 

    imageMode(CENTER);

//draw windmill 1

    if (rotation1){
      windmill.drawWindmill();
      windmill.rotationWindmill();
      loop();  
  }
    else{
      windmill.drawWindmill();
      windmill.rotationWindmill();
      noLoop(); 
  }

     //draw windmill 2

    if (rotation2){
      windmill.drawWindmill();
      windmill.rotationWindmill2();
      loop();  
  }
    else{
      windmill.drawWindmill();
      windmill.rotationWindmill2();
      noLoop(); 
  }
}

*class windmill

public class windmill

{

int xpos;
int ypos;

//declaration for rotation
float angle; 


 // constructor + parameter of windmill base
  windmill(int windmillx, int windmilly){
  xpos = windmillx;
  ypos = windmilly;
}

void drawWindmill(){

//draw windmill base to screen
image(windmillBase,xpos,ypos);
}



void rotationWindmill()
  {
    pushMatrix();
    fill(#BCFCA1);
    noStroke();
    rect(500,390,100,100);
    translate(549,442); // define center
    rotate(radians(angle));
    image(windmillWheel,0,0,95,95);
    angle += 1;
    popMatrix();
    loop();
  }


  void rotationWindmill2()
  {
    pushMatrix();
    fill(#BCFCA1);
    noStroke();
    rect(650,390,100,100);
    translate(699,442); // define center
    rotate(radians(angle));
    image(windmillWheel,0,0,95,95);
    angle += 1;
    popMatrix();
  }
}

Answers

  • edited April 2014 Answer ✓

    Read loop()'s reference to understand it correctly:
    http://processing.org/reference/loop_.html

    If you wanna assign a particular key as a windmill switch, you should do something like this:

    boolean rotation1 = true, rotation2 = true;
    
    void keyPressed() {
      final int k = keyCode;
    
      if      (k == '1')  rotation1 = !rotation1;
      else if (k == '2')  rotation2 = !rotation2;
    }
    
  • Thank you, works well now.

Sign In or Register to comment.