Issues in JavaScript

29 views
Skip to first unread message

emf...@gmail.com

unread,
May 3, 2018, 11:38:18 AM5/3/18
to Processing.js
My code should open the screen, then immediately change to purple when run as it enters the first if statement - however, it seems to run all of these and skip right to the end slowly!!!

Any help on this would be very much appreciated, just can't seem to work out why it's not running any of the if statements and pausing for the mousePressed events.

void setup()
{
  Bwidth=1000;
  Blength=400;
  size(1000,400);
  background(0,0,0);
  fill(255,255,255);
 
  dClicked="NoClick";
  VehicleType="Lorry";
  VSelect="Incomplete";
  dCalc="NoCalc";
  RotateVehicle="NoRotate";
  gravity=9.81;
  AllowableStress=(1.3*(10^(-6)));
}
void draw()
{
  BridgeInput();
}

void mousePressed()
{
  if(dClicked=="NoClick")
  {
    Blength=mouseX-27;
    background(116,108,250);
    ellipse(27,373,25,25);
    dClicked="Click1";
  }
 
  if(dClicked=="Click1")
  {
    Bwidth=373-mouseY;
    background(139,139,139);
    fill(255,255,255);
    dClicked="Clicked";
  }
  else
  {
    if (VSelect=="Round1")
    {
      V1x= mouseX;
      V1y=mouseY;
    }
   
    if (VSelect=="Round2")
    {
      V2x=mouseX;
      V2y=mouseY;
    }
   
    if (VSelect=="Round3")
    {
      V3x=mouseX;
      V3y=mouseY;
    }
   
    if(VSelect=="Round4")
    {
      V4x=mouseX;
      V4y=mouseY;
    }
   
    if (VSelect=="Round5")
    {
       V5x=mouseX;
       V5y=mouseY;
    }
  } 
}
void keyPressed()
{
  if (key=='a'||key=='A')
  {
    VehicleType="Lorry";
  }
 
  if (key=='b'||key=='B')
  {
    VehicleType="Car";
  }
  if (key=='c'||key=='C')
  {
    VehicleType="Ped";
  }
  if (key=='1')
  {
    FinishInput();
  }
  if (key=='0');
  {
    dCalc="Calculate";
  }
  if (key=='2')
  {
    RotateVehicle="YesRotate";
  }
  else
  {
    text("Invalid selection.",150,150);
    VSelect="Incomplete";
    dCalc="NoCalc";
    RotateVehicle="NoRotate";
  }
}

void BridgeInput()
{
  if (dClicked=="NoClick")
  {  
    background(116,108,250);
    text("Select bridge dimensions from the corner ..... length horizontally (looking at the plan)",100,180,900,300);
    ellipse(27,373,25,25);
   
    mousePressed();
  }
 
  if (dClicked=="Click1")
  {
    text("Select bridge dimensions from the corner ..... width vertically (looking at the plan)",100,180,900,300);
    ellipse(27,573,25,25);
   
    mousePressed();
  }

Lee Brunjes

unread,
May 3, 2018, 12:54:26 PM5/3/18
to proces...@googlegroups.com
You are calling the mouseClicked event in the draw loop. this will simulate someone clicking.

The keyPressed and mousePressed evens are called by processing when the events happen. You dont need to call them:



With regard to your other two qeustions, if you want to stop the draw loop there is a noLoop/loop call to stop drawing.


Hope that helps,
-Lee

--
You received this message because you are subscribed to the Google Groups "Processing.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to processingjs+unsubscribe@googlegroups.com.
To post to this group, send email to proces...@googlegroups.com.
Visit this group at https://groups.google.com/group/processingjs.
For more options, visit https://groups.google.com/d/optout.

emf...@gmail.com

unread,
May 4, 2018, 4:19:20 AM5/4/18
to Processing.js
So if I'm understanding you correctly, I want to remove the mousePressed & keyPressed calls within the draw function, and replace them with the loop that should pause the program until the mouse is pressed? Or do I want to set up a custom listener that will only continue to run the code once the mouse is pressed?

emf...@gmail.com

unread,
May 4, 2018, 9:16:55 AM5/4/18
to Processing.js
void setup()
{
  Bwidth=1000;
  Blength=400;
  size(1000,400);
  background(0,0,0);
  fill(255,255,255);
 
  dClicked="NoClick";
  VehicleType="Lorry";
  VSelect="Incomplete";
  dCalc="NoCalc";
  RotateVehicle="NoRotate";
  gravity=9.81;
  AllowableStress=(1.3*(10^(-6)));
 
  v1x=100;
  v2x=230;
  v3x=400;
  v4x=770;
  v5x=900;
 
  mousePressed=false;
}
  if (key=='z')
  {
    BridgeInput();

  }
  else
  {
    text("Invalid selection.",150,150);
    VSelect="Incomplete";
    dCalc="NoCalc";
    RotateVehicle="NoRotate";
  }
}

void BridgeInput()
{
  if (dClicked=="NoClick")
  {  
    background(116,108,250);
    text("Select bridge dimensions from the corner ..... length horizontally (looking at the plan)",100,180,900,300);
    ellipse(27,373,25,25);
   
    MouseListen();
    mousePressed=false;

  }
 
  if (dClicked=="Click1")
  {
    text("Select bridge dimensions from the corner ..... width vertically (looking at the plan)",100,180,900,300);
    ellipse(27,573,25,25);
   
    MouseListen();
    mousePressed=false;
  }
 
  if (dClicked=="Clicked")
  {
    text("Now input on the keyboard your vehicle type - a for lorry, b for car or c for pedestrian",150,150);
    text("You want the most vehicles on the bridge without causing excessive bending stresses - be careful!",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VSelect=="Incomplete")
    {
      background(139,139,139);
      text("Please input a proper vehicle type - a for lorry, b for car or c for pedestrian",150,150);
      KeyListen();
      keyPressed=false;
    }
    background(139,139,139);
    VSelect="Round1";
    text("Select where on the bridge you want your vehicle",150,150);
    text("Or press 2 to rotate your chosen vehicle - remember you're aiming for low stress!",200,150);
   
    KeyListen();
    keyPressed=false;
  
    if (VehicleType=="Lorry")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,330,50);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,50,330);
      }
     
      MouseListen();
      mousePressed=false;
     
      V1type=1;
      V1m=3500;
      V1rad=150;
    }
   
    if (VehicleType=="Car")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,126,48);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,48,126);
      }
      MouseListen();
      mousePressed=false;
     
      V1type=2;
      V1m=1200;
      V1rad=60;
    }
   
    if (VehicleType=="Ped")
    {
      if (RotateVehicle=="NoRotate")
      {
        ellipse(mouseX,mouseY,88,23.2);
      }
      if (RotateVehicle=="YesRotate")
      {
        ellipse(mouseX,mouseY,23.2,88);
      }
     
      MouseListen();
      mousePressed=false;
     
      V1type=3;
      V1m=100;
      V1rad=7;
    }
   
    background(139,139,139);
   
    RotateVehicle="NoRotate";
   
    text("Select your second vehicle type - a for lorry, b for car or c for pedestrian",150,150);
    text("Or press 1 to exit vehicle input",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VSelect=="Incomplete")
    {
      background(139,139,139);
      text("Please input a proper vehicle type - a for lorry, b for car or c for pedestrian",150,150);
      KeyListen();
      keyPressed=false;
    }
   
    VSelect="Round2";
    background(139,139,139);
    text("Select where on the bridge you want your second vehicle",150,150);
    text("Vehicle 1",V1x,V1y);
    triangle((V1x-15),(V1y-15),(V1x+15),(V1y-15),V1x,(V1y+15));
    text("Or press 2 to rotate your chosen vehicle - remember you're aiming for low stress!",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VehicleType=="Lorry")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,330,50);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,50,330);
      }
      MouseListen();
      mousePressed=false;
     
      V2type=1;
      V2m=3500;
      V2rad=150;
    }
    if (VehicleType=="Car")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,126,48);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,48,126);
      }
      MouseListen();
      mousePressed=false;
     
      V2type=2;
      V2m=1200;
      V2rad=60;
    }
    if (VehicleType=="Ped")
    {
      if (RotateVehicle=="NoRotate")
      {
        ellipse(mouseX,mouseY,88,23.2);
      }
      if (RotateVehicle=="YesRotate")
      {
        ellipse(mouseX,mouseY,23.2,88);
      }
     
      MouseListen();
      mousePressed=false;
     
      V2type=3;
      V2m=100;
      V2rad=7;
    }
   
    RotateVehicle="NoRotate";
   
    text("Select your third vehicle type - a for lorry, b for car or c for pedestrian",150,150);
    text("Or press 1 to exit vehicle input",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VSelect=="Incomplete")
    {
      background(139,139,139);
      text("Please input a proper vehicle type - a for lorry, b for car or c for pedestrian",150,150);
      KeyListen();
      keyPressed=false;
    }
   
    VSelect="Round3";
    background(139,139,139);
    text("Select where on the bridge you want your third vehicle",150,150);
    text("Vehicle 1",V1x,V1y);
    triangle((V1x-15),(V1y-15),(V1x+15),(V1y-15),V1x,(V1y+15));
    text("Vehicle 2",V2x,V2y);
    triangle((V2x-15),(V2y-15),(V2x+15),(V2y-15),V2x,(V2y+15));
    text("Or press 2 to rotate your chosen vehicle - remember you're aiming for low stress!",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VehicleType=="Lorry")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,330,50);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,50,330);
      }
     
      MouseListen();
      mousePressed=false;
     
      V3type=1;
      V3m=3500;
      V3rad=150;
    }
    if (VehicleType=="Car")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,126,48);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,48,126);
      }
      MouseListen();
      mousePressed=false;
     
      V3type=2;
      V3m=1200;
      V3rad=60;
    }
    if (VehicleType=="Ped")
    {
      if (RotateVehicle=="NoRotate")
      {
        ellipse(mouseX,mouseY,88,23.2);
      }
      if (RotateVehicle=="YesRotate")
      {
        ellipse(mouseX,mouseY,23.2,88);
      }
     
      MouseListen();
      mousePressed=false;
     
      V3type=3;
      V3m=100;
      V3rad=7;
    }
   
    RotateVehicle="NoRotate";
   
    text("Select your fourth vehicle type - a for lorry, b for car or c for pedestrian",150,150);
    text("Or press 1 to exit vehicle input",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VSelect=="Incomplete")
    {
      background(139,139,139);
      text("Please input a proper vehicle type - a for lorry, b for car or c for pedestrian",150,150);
      KeyListen();
      keyPressed=false;
    }
   
    VSelect="Round4";
    background(139,139,139);
    text("Select where on the bridge you want your fourth vehicle, ",150,150);
    text("Vehicle 1",V1x,V1y);
    triangle((V1x-15),(V1y-15),(V1x+15),(V1y-15),V1x,(V1y+15));
    text("Vehicle 2",V2x,V2y);
    triangle((V2x-15),(V2y-15),(V2x+15),(V2y-15),V2x,(V2y+15));
    text("Vehicle 3",V3x,V3y);
    triangle((V3x-15),(V3y-15),(V3x+15),(V3y-15),V3x,(V3y+15));
    text("Or press 2 to rotate your chosen vehicle - remember you're aiming for low stress!",200,150);
   
    keyListen();
    keyPressed=false;
   
    if (VehicleType=="Lorry")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,330,50);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,50,330);
      }
     
      MouseListen();
      mousePressed=false;
     
      V4type=1; 
      V4m=3500;
      V4rad=150;
    }
    if (VehicleType=="Car")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,126,48);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,48,126);
      }
     
      MouseListen();
      mousePressed=false;
     
      V4type=2;
      V4m=1200;
      V4rad=60;
    }
    if (VehicleType=="Ped")
    {
      if (RotateVehicle=="NoRotate")
      {
        ellipse(mouseX,mouseY,88,23.2);
      }
      if (RotateVehicle=="YesRotate")
      {
        ellipse(mouseX,mouseY,23.2,88);
      }
      MouseListen();
      mousePressed=false;
     
      V4type=3;
      V4m=100;
      V4rad=7;
    }
   
    RotateVehicle="NoRotate";
   
    text("Select your final vehicle type - a for lorry, b for car or c for pedestrian",150,150);
    text("Or press 1 to exit vehicle input",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VSelect=="Incomplete")
    {
      background(139,139,139);
      text("Please input a proper vehicle type - a for lorry, b for car or c for pedestrian",150,150);
      KeyListen();
      keyPressed=false;
    }
   
    VSelect="Round5";
    background(139,139,139);
    text("Select where on the bridge you want your final vehicle, ",150,150);
    text("Vehicle 1",V1x,V1y);
    triangle((V1x-15),(V1y-15),(V1x+15),(V1y-15),V1x,(V1y+15));
    text("Vehicle 2",V2x,V2y);
    triangle((V2x-15),(V2y-15),(V2x+15),(V2y-15),V2x,(V2y+15));
    text("Vehicle 3",V3x,V3y);
    triangle((V3x-15),(V3y-15),(V3x+15),(V3y-15),V3x,(V3y+15)); 
    text("Vehicle 4",V4x,V4y);
    triangle((V4x-15),(V4y-15),(V4x+15),(V4y-15),V4x,(V4y+15));
    text("Or press 2 to rotate your chosen vehicle - remember you're aiming for low stress!",200,150);
   
    KeyListen();
    keyPressed=false;
   
    if (VehicleType=="Lorry")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,330,50);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,50,330);
      }
     
      MouseListen();
      mousePressed=false;
     
      V5type=1; 
      V5m=3500;
      V5rad=150;
    }
    if (VehicleType=="Car")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,126,48);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,48,126);
      }
     
      MouseListen();
      mousePressed=false;
     
      V5type=2;
      V5m=1200;
      V5rad=60;
    }
    if (VehicleType=="Ped")
    {
      if (RotateVehicle=="NoRotate")
      {
        ellipse(mouseX,mouseY,88,23.2);
      }
      if (RotateVehicle=="YesRotate")
      {
        ellipse(mouseX,mouseY,23.2,88);
      }
     
      MouseListen();
      mousePressed=false;
     
      V5type=3;
      V5m=100;
      V5rad=7;
    }
   
    background(139,139,139);
    text("No further vehicle input allowed",150,150);
    FinishInput();
  }
}
 void FinishInput()
{
  CheckVehicleClash();
  background(116,108,250);
  text("You've inputted all your vehicles. Press 0 to calculate the arch forces.",150,150);
 
  KeyListen();
  keyPressed=false;
 
  if (dCalc=="Calculate")
  {
    ArchRad=(Blength/2);
   
    VehicleGeometry();
    ArchGeometry();
    LoadArch();
    BridgeOutput();
  }
  else
  {
    text("Your vehicle input has been stored, but no calculations carried out. Thank you and goodbye.",150,150);
    exit();
  }
}
 
  
 void CheckVehicleClash()
  // allowing a 30m distance between vehicles in both directions
 
 {
   if (abs(V1x-V2x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V1x-V3x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V1x-V4x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V1x-V5x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V2x-V3x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V2x-V4x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V2x-V5x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V3x-V4x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V3x-V5x)<30)
   {
     ClashScreenSetup();
   }
   if (abs(V4x-V5x)<30)
   {
     ClashScreenSetup();
   }
 }
 
 void ClashScreenSetup()
 {
     background(232,28,28);
     text("Vehicles clash. Please recomplete vehicle input and ensure sufficient spacing.",150,150);
     text("Press z to restart.",300,150);
 }
 
 void VehicleGeometry()
{
  l1x=(V1x/1000)*Blength;
  w1y=(V1y/400)*Bwidth;
  l2x=(V2x/1000)*Blength;
  w2y=(V2y/400)*Bwidth;
  l3x=(V3x/1000)*Blength;
  w3y=(V3y/400)*Bwidth;
  l4x=(V4x/1000)*Blength;
  w4y=(V4y/400)*Bwidth;
  l5x=(V5x/1000)*Blength;
  w5y=(V5y/400)*Bwidth;
 
  V1z=sqrt(sq(ArchRad)+sq(ArchRad-V1x));
  V2z=sqrt(sq(ArchRad)+sq(ArchRad-V2x));
  V3z=sqrt(sq(ArchRad)+sq(ArchRad-V3x));
  V4z=sqrt(sq(ArchRad)+sq(ArchRad-V4x));
  V5z=sqrt(sq(ArchRad)+sq(ArchRad-V5x));
 
  thetaV1=atan(V1z/l1x);
  thetaV2=atan(V2z/l2x);
  thetaV3=atan(V3z/l3x);
  thetaV4=atan(V4z/l4x);
  thetaV5=atan(V5z/l5x);
 
}
void ArchGeometry()
{
  segments=9;
 
// using a span to rise ratio of 0.4 for a parabolic arch shape (according to MEXE method)
  ArchHeight=(0.4*Blength);
 
  deltaX = Blength/segments;
  deltaZ=(ArchHeight/(segments/2));
 
  A1x=0;
  A2x=A1x+deltaX;
  A3x=A2x+deltaX;
  A4x=A3x+deltaX;
  A5x=A4x+deltaX;
  A6x=A5x+deltaX;
  A7x=A6x+deltaX;
  A8x=A7x+deltaX;
  A9x=A8x+deltaX;
 
  A1z=0;
  A2z=A1z+deltaZ;
  A3z=A2z+deltaZ;
  A4z=A3z+deltaZ;
  A5z=A4z+deltaZ;
  A6z=A5z-deltaZ;
  A7z=A6z-deltaZ;
  A8z=A7z-deltaZ;
  A9z=0;
 
  thetaA1=atan(A1z/A1x);
  thetaA2=atan(A2z/A2x);
  thetaA3=atan(A3z/A3x);
  thetaA4=atan(A4z/A4x);
  thetaA5=atan(A5z/A5x);
  thetaA6=atan(A6z/A6x);
  thetaA7=atan(A7z/A7x);
  thetaA8=atan(A8z/A8x);
  thetaA9=atan(A9z/A9x);
}
void LoadArch()
{
  SimplySupportM();
  LateralForceEffects();
 
// calculating moments based on superposition of simply supported moments and horizontal thrust lever arm 
  A1Mom=A1SMom-(A1Horz*A1z);
  A2Mom=A2SMom-(A2Horz*A2z);
  A3Mom=A3SMom-(A3Horz*A3z);
  A4Mom=A4SMom-(A4Horz*A4z);
  A5Mom=A5SMom-(A5Horz*A5z);
  A6Mom=A6SMom-(A6Horz*A6z);
  A7Mom=A7SMom-(A7Horz*A7z);
  A8Mom=A8SMom-(A8Horz*A8z);
  A9Mom=A9SMom-(A9Horz*A9z);
 
// calculating stress based on (My/I), with standard masonry brick dimensions of 65mm high, 102.5mm wide and 215mm long
  Bricky=0.3;
  BrickI=((1.025*(0.65*0.65*0.65))/12);
 
  A1Stress=(A1Mom*Bricky)/BrickI;
  A2Stress=(A2Mom*Bricky)/BrickI;
  A3Stress=(A3Mom*Bricky)/BrickI;
  A4Stress=(A4Mom*Bricky)/BrickI;
  A5Stress=(A5Mom*Bricky)/BrickI;
  A6Stress=(A6Mom*Bricky)/BrickI;
  A7Stress=(A7Mom*Bricky)/BrickI;
  A8Stress=(A8Mom*Bricky)/BrickI;
  A9Stress=(A9Mom*Bricky)/BrickI;
 
 
}
void SimplySupportM()
{
// calculating the forces for a simply supported beam with the flattened arch geometry
 
  TotalLoad=((V1m+V2m+V3m+V4m+V5m)*gravity);
  VSupport=(TotalLoad/2);
 
  MaxMom=(VSupport*ArchRad);
 
  A1SMom=0;
  A2SMom=((ArchRad-A2x)/ArchRad)*MaxMom;
  A3SMom=((ArchRad-A3x)/ArchRad)*MaxMom;
  A4SMom=((ArchRad-A4x)/ArchRad)*MaxMom;
  A5SMom=MaxMom;
  A6SMom=((A6x-ArchRad)/ArchRad)*MaxMom;
  A7SMom=((A7x-ArchRad)/ArchRad)*MaxMom;
  A8SMom=((A8x-ArchRad)/ArchRad)*MaxMom;
  A9SMom=0;
 
  if (l1x>ArchRad)
  {
    V1SMom=((l1x-ArchRad)/ArchRad)*MaxMom;
  }
  else
  {
    V1SMom=((ArchRad-l1x)/ArchRad)*MaxMom;
  }
 
  if (l2x>ArchRad)
  {
    V2SMom=((l2x-ArchRad)/ArchRad)*MaxMom;
  }
  else
  {
    V2SMom=((ArchRad-l2x)/ArchRad)*MaxMom;
  }
  if (l3x>ArchRad)
  {
    V3SMom=((l3x-ArchRad)/ArchRad)*MaxMom;
  }
  else
  {
    V3SMom=((ArchRad-l3x)/ArchRad)*MaxMom;
  }
 
  if (l4x>ArchRad)
  {
    V4SMom=((l4x-ArchRad)/ArchRad)*MaxMom;
  }
  else
  {
    V4SMom=((ArchRad-l4x)/ArchRad)*MaxMom;
  }
 
  if (l5x>ArchRad)
  {
    V5SMom=((l5x-ArchRad)/ArchRad)*MaxMom;
  }
  else
  {
    V5SMom=((ArchRad-l5x)/ArchRad)*MaxMom;
  }
 
 
}
void LateralForceEffects()
{
// calculating the horizontal component as thrust , then converting this into component force in the horizontal (x) and vertical (y) planes to calculate additional moment and shear
 
    Thrust=(MaxMom/ArchHeight);
   
    V1Horz=cos(thetaV1)*Thrust;
    V1Vert=sin(thetaV1)*Thrust;  
    V2Horz=cos(thetaV2)*Thrust;
    V2Vert=sin(thetaV2)*Thrust;
    V3Horz=cos(thetaV3)*Thrust;
    V3Vert=sin(thetaV3)*Thrust;
    V4Horz=cos(thetaV4)*Thrust;
    V4Vert=sin(thetaV4)*Thrust;
    V5Horz=cos(thetaV5)*Thrust;
    V5Vert=sin(thetaV5)*Thrust;
   
    A1Horz=cos(thetaA1)*Thrust;
    A1Vert=sin(thetaA1)*Thrust;
    A2Horz=cos(thetaA2)*Thrust;
    A2Vert=sin(thetaA2)*Thrust;
    A3Horz=cos(thetaA3)*Thrust;
    A3Vert=sin(thetaA3)*Thrust;
    A4Horz=cos(thetaA4)*Thrust;
    A4Vert=sin(thetaA4)*Thrust;
    A5Horz=cos(thetaA5)*Thrust;
    A5Vert=sin(thetaA5)*Thrust;
    A6Horz=cos(thetaA6)*Thrust;
    A6Vert=sin(thetaA6)*Thrust;
    A7Horz=cos(thetaA7)*Thrust;
    A7Vert=sin(thetaA7)*Thrust;
    A8Horz=cos(thetaA8)*Thrust;
    A8Vert=sin(thetaA8)*Thrust;
    A9Horz=cos(thetaA9)*Thrust;
    A9Vert=sin(thetaA9)*Thrust; 
   
}
 
 void BridgeOutput()
 {
   line(A1x,A2x,A1z,A2z);
   line(A2x,A3x,A2z,A3z);
   line(A3x,A4x,A3z,A4z);
   line(A4x,A5x,A4z,A5z);
   line(A5x,A6x,A5z,A6z);
   line(A6x,A7x,A6z,A7z);
   line(A7x,A8x,A7z,A8z);
   line(A8x,A9x,A8z,A9z);
  
   ellipse(l1x,V1z,V1rad,V1rad);
   ellipse(l2x,V2z,V2rad,V2rad);
   ellipse(l3x,V3z,V3rad,V3rad);
   ellipse(l4x,V4z,V4rad,V4rad);
   ellipse(l5x,V5z,V5rad,V5rad);
  
 }
 
 void MouseListen()
 {
   if (mousePressed=false)
   {
     noLoop();
   }
   else
   {
     mousePressed();
   }
 }
 
 void KeyListen()
 {
   if (keyPressed=false)
   {
     noLoop();
   }
   else
   {
     keyPressed();
   }
 }

NEW CODE - but it still doesn't seem to stop or pause - any ideas on why? 

Lee Brunjes

unread,
May 4, 2018, 11:34:46 AM5/4/18
to proces...@googlegroups.com
So, you need to change how you are thinking about the  input functions(keyPressed, MousePressed.

During normal program flow processing calls setup and then calls draw over and over in a loop. If you want to stop the draw loop you use loop and noLoop.

The key and mouse functions are event handlers this means that they will be called automatically when the event occurs. This happens outside the draw loop and without your input. You do not need to ever call these functions.

Check out this example:


--
You received this message because you are subscribed to the Google Groups "Processing.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to processingjs...@googlegroups.com.

emf...@gmail.com

unread,
May 5, 2018, 3:10:49 AM5/5/18
to Processing.js
Think I understand what you're saying, but I don't want it to entirely stop the draw loop, just pause on the screen it's reached (as this needs to be seen for the user to input information). Plus I do need it to stop until the mouse is Pressed, which is difficult, even with using my MouseListen function!
 

Do you think it might be easier just to use an if statement within the main draw function to check whether the mouse has been pressed, as this will then cause it to pause automatically until this is true? 

Any suggestions for how to solve this would be greatly appreciated!

emf...@gmail.com

unread,
May 5, 2018, 3:11:38 AM5/5/18
to Processing.js


also see this thread for alternative ideas - https://groups.google.com/forum/#!topic/processingjs/-_YDWVTBKKc

Lee Brunjes

unread,
May 5, 2018, 8:03:58 AM5/5/18
to proces...@googlegroups.com
That approach is called polling.

It's a completely valid way to handle input, but it means you have to maintain state yourself.

This is to say, You have to know what keys were down last time in order to tell what keys were released or newly pressed.

If you are doing something like clicking a button there are advanatges to the handler event. If you are trying to do something like move right while the right arrow is pressed polling makes it easy.

Hope that helps,
Lee



On Sat, May 5, 2018, 03:11 <emf...@gmail.com> wrote:


also see this thread for alternative ideas - https://groups.google.com/forum/#!topic/processingjs/-_YDWVTBKKc

--

emf...@gmail.com

unread,
May 5, 2018, 9:12:43 AM5/5/18
to Processing.js
void setup()
{
  Bwidth=1000;
  Blength=400;
  size(1000,400);
  background(0,0,0);
  fill(255,255,255);
 
  dClicked="NoClick";
  VehicleType="Lorry";
  VSelect="Start";
  dCalc="Inter";

  RotateVehicle="NoRotate";
  gravity=9.81;
  AllowableStress=(1.3*(10^(-6)));
 
  v1x=100;
  v2x=230;
  v3x=400;
  v4x=770;
  v5x=900;
 
  mousePressed=false;
  keyPressed=false;
}
void draw()
{
  BridgeInput();
}

void BridgeInput()
{
  if (dClicked=="NoClick")
  {  
    background(116,108,250);
   
    text("Select bridge dimensions from the corner ..... length horizontally (looking at the plan)",100,180,900,300);
    ellipse(27,373,25,25);
   
    if (mousePressed==true)
    {
      Blength=mouseX-27;
      dClicked="Click1";
      mousePressed=false;
    }
  }
 
  if (dClicked=="Click1")
  {
    background(116,108,250);

    text("Select bridge dimensions from the corner ..... width vertically (looking at the plan)",100,180,900,300);
    ellipse(27,373,25,25);
   
    if (mousePressed==true)

    {
      Bwidth=373-mouseY;
      background(139,139,139);
      fill(255,255,255);
      dClicked="Clicked";
      mousePressed=false;
    }
  }
 
  if (dClicked=="Clicked")
  {  
    while (keyPressed==false)
    {
      background(139,139,139);
      VSelect="Start";

      text("Now input on the keyboard your vehicle type - a for lorry, b for car or c for pedestrian",150,150);
      text("You want the most vehicles on the bridge without causing excessive bending stresses - be careful!",200,150);
    }
   
    if (keyPressed==true)

    {
      if (key=='a'||key=='A')
      {
        VehicleType="Lorry";
        VSelect="Complete";
        keyPressed=false;

      }
      if (key=='b'||key=='B')
      {
        VehicleType="Car";
        VSelect="Complete";
        keyPressed=false;

      }
      if (key=='c'||key=='C')
      {
        VehicleType="Ped";
        VSelect="Complete";
        keyPressed=false;

      }
      else
      {
        text("Invalid selection.",150,150);
        VSelect="Incomplete";
        keyPressed=false;
      }
    }
   
    if (VSelect=="Incomplete")
    {
      while (keyPressed==false)

      {
        background(139,139,139);
        text("Please input a proper vehicle type - a for lorry, b for car or c for pedestrian",150,150);
      }
 
      if (keyPressed==true)

      {
        if (key=='a'||key=='A')
        {
          VehicleType="Lorry";
          keyPressed=false;
          VSelect="Complete";

        }
        if (key=='b'||key=='B')
        {
          VehicleType="Car";
          VSelect="Complete";
          keyPressed=false;

        }
        if (key=='c'||key=='C')
        {
          VehicleType="Ped";
          VSelect="Complete";
          keyPressed=false;

        }
        else
        {
          text("Invalid selection.",150,150);
          VSelect="Incomplete";
          keyPressed=false;
        }
      }
    }
    VSelect="Round1";
   
    while (keyPressed==false)
    {
      background(139,139,139);

      text("Select where on the bridge you want your vehicle",150,150);
      text("Or press 2 to rotate your chosen vehicle - remember you're aiming for low stress!",200,150);
    }
 
    if (keyPressed==true)
   {
     if (key=='2')
    {
      RotateVehicle="YesRotate";
      keyPressed=false;
    } 
   }
  
    if (VehicleType=="Lorry")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,330,50);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,50,330);
      }
     
      if (mousePressed==true)
      {
        V1x= mouseX;
        V1y=mouseY;

        mousePressed=false;
      }
     
      V1type=1;
      V1m=3500;
      V1rad=150;
    }
   
    if (VehicleType=="Car")
    {
      if (RotateVehicle=="NoRotate")
      {
        rect(mouseX,mouseY,126,48);
      }
      if (RotateVehicle=="YesRotate")
      {
        rect(mouseX,mouseY,48,126);
      }
    
      if (mousePressed==true)
      {
        V1x= mouseX;
        V1y=mouseY;

        mousePressed=false;
      }
     
      V1type=2;
      V1m=1200;
      V1rad=60;
    }
   
    if (VehicleType=="Ped")
    {
      if (RotateVehicle=="NoRotate")
      {
        ellipse(mouseX,mouseY,88,23.2);
      }
      if (RotateVehicle=="YesRotate")
      {
        ellipse(mouseX,mouseY,23.2,88);
      }
     
      if (mousePressed==true)
      {
        V1x= mouseX;
        V1y=mouseY;
        mousePressed=false;
      }
     
      V1type=3;
      V1m=100;
      V1rad=7;
    } 

emf...@gmail.com

unread,
May 5, 2018, 9:14:36 AM5/5/18
to Processing.js
This code should change various screen colours depending on the mouseclicked and keypressed properties (usually resetting them to false after its finished). However, the second mousePressed input is not working presently - any ideas why this might be!

I'm then hoping the while loop will stop the code (or remain on the same screen) until a key is pressed - does this seem like an appropriate way of doing it?  

Lee Brunjes

unread,
May 7, 2018, 7:48:55 PM5/7/18
to proces...@googlegroups.com
after the second press you use a loop of while(Keypressed ==false);

This will tight loop and prevent further program execution, if you swap those to if it should run more normally.


On Sat, May 5, 2018 at 9:14 AM, <emf...@gmail.com> wrote:
This code should change various screen colours depending on the mouseclicked and keypressed properties (usually resetting them to false after its finished). However, the second mousePressed input is not working presently - any ideas why this might be!

I'm then hoping the while loop will stop the code (or remain on the same screen) until a key is pressed - does this seem like an appropriate way of doing it?  

--
You received this message because you are subscribed to the Google Groups "Processing.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to processingjs+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages