keyPressed and for loop issues

edited November 2015 in Using Processing

Hey there,

I've been trying to figure out how to connect a specific keyPressed function with an Array loop: when you press the d key, connect previous mouse pressed pulses with current mouse pressed, and* increase the stroke each time. I figured a key Presssed boolean might get me there, but I don't seem to understand the order or placement of the two functions. Do I keep everything in draw or should the line increase only be in keyPressed, which would eliminate the need for a boolean?

Any help would make my day!

//start the pulse pulseTotal at 0
int pulseTotal =0;

Pulse[] pulseArray = new Pulse[1000];

//connected lines is initialized to false
boolean lineConnect = false;

void setup() {
  size(500, 500);

void draw() {
  //if i is equal to 0, make i less than the pulseTotal number of pulses and increase it by 1 each time
  for (int i =0; i < pulseTotal; i++) {
    if (i>0 && lineConnect == true) {
      //if i is greater than 0 and connected lines is true, connect line from current (i) pulse to previous pulse x and y
      //..and increase strokeWeight with each i increase
      line(pulseArray[i-1].xPosition, pulseArray[i-1].yPosition, pulseArray[i].xPosition, pulseArray [i].yPosition);

// --------------------------
//Mouse Pressed interaction 
// --------------------------
void mousePressed() {

  pulseArray[pulseTotal] = new Pulse(mouseX, mouseY, 25, 200);
  //we increment the pulseTotal pulseArray by 1
  pulseTotal = pulseTotal +1;

// --------------------------
//Keyboard interaction 
// --------------------------
void keyPressed() {

  //if key is equal to 32 (32 as a symbol that represents the space bar// 
  //...set background to white (reset the background)
  if (key == 32) { // 32 is the ASCII decimal value representing the space bar

  //if D is pressed, make line connect (displays lines and increases stroke each time) true
  if (key == 68) {
    lineConnect = true;
// Pulse Class
// draws a pulsing dot on the screen
class Pulse {
  /* properties */
  // scale of the pulse
  int pulseScale = 25;         
  // x-coordinate of the pulse  
  float xPosition;              
  // y-coordinate of the pulse  
  float yPosition;            
  // width of the pulse  
  float pulseWidth;         
  // height of the pulse  
  float pulseHeight;            
  // size of the pulse  
  float pulseSize;      
  // fill color of pulse  
  color fillColor;                

  /* methods */
  // constructor 
  Pulse(float newPulseX, float newPulseY, int newPulseScale, color newColor) {
    xPosition = newPulseX;
    yPosition = newPulseY;
    pulseScale = newPulseScale;
    fillColor = newColor;
    pulseSize = 20;

  // draws the pulse and updates its size
  void display() {

    // set the width of the pulse using the sine function 
    pulseWidth = sin(pulseSize)*pulseScale;
    // set the height of the pulse using the sine function 
    pulseHeight = sin(pulseSize)*pulseScale;

    // draw the ellipse at the center
    //no stroke on the ellipse
    //fill the pulse by the fill color variable
    //draw the ellipse
    ellipse(xPosition, yPosition, pulseWidth, pulseHeight);

    //increment size
    pulseSize = pulseSize + 0.1;


  • Answer ✓

    you never reset lineConnect to false. Is that intentionally?

    since you have no array of lineConnect either all lines are there or none?

    you are checking for D, not for d

  • It was intentional, but an Array would probably be nice to try so they are seen one at a time. Ah! so I would need

    if (key == 68 || key == 100)

    for both D and d..

    I haven't set it to false because I want it to increment along with the new pulses all the time, but again I'm sure an Array could make this more flexible. I'm still trying to figure out the most modular way to go about code so certain solutions are alien to me

  • edited November 2015 Answer ✓

    Unless lowercase letters are requested, stick w/ keyCode only and never use key: :>
    Also favor characters like 'A', '5', ' ' instead of 65, 53, 32.
    Use constants like ENTER, RETURN, TAB in place of 10, 13, 8 as well. B-)

  • Thank you! I was over- complicating things a bit..

Sign In or Register to comment.