We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Help with a grid using a for cycle
Page Index Toggle Pages: 1
Help with a grid using a for cycle (Read 928 times)
Help with a grid using a for cycle
Jan 15th, 2010, 3:11am
 
Hi everyone!
I could really use a hand here cause i cant think of anything.
I am making a touchscreen aplication that is an instrument. The keys of the instrument are divided on a grid created by a for cycle.
Now as this is a multitouch screen... i have a problem.. i need it to only play the sound once when pressed. which is done easily.. but when there are more then 1 finger at the time.. then there is a problem. It plays the sound of both fingers constantly cause it keeps checking the positions of the 2 squares.

heres part of the code:

void draw() {

 background(bg);
  stroke(0,0,255);
       for (int y= 1; y<height; y += 80){
         for (int x= 1; x<width; x += 60){
           strokeWeight(1);
           noFill();
           rect(x, y,rectWidth, rectHeight);
           if (mouseX >= x && mouseX <= x+rectWidth &&  
             mouseY >= y && mouseY <= y+rectHeight) {
             if(lastPos != x*y )
             {
               fill(0);
               rect(0,0,600,800);
               fill(255);
               lastPos = x*y;
               rect(x, y,rectWidth, rectHeight);
               if(tuioCursorList.size() == 1)
               {
                 sc.playNote(x / 4, y - val, 0.5);
               }
               if(tuioCursorList.size() == 2)
               {
                 float[] pitches = {x / 4, x / 4 +4, x / 4 + 7, x / 4+11       };
                 sc.playChord(pitches, 80, 1);
                 
               }
             
   }
 }
}

If i had to guess i think i would have to store all the cursor positions in a array and then make some conditionals but i am not sure how to do it.

Can somebody Please help me? I would really apreciate it.

Thank you,
Michael
Re: Help with a grid using a for cycle
Reply #1 - Jan 15th, 2010, 3:38am
 
Quote:
if(lastPos != x*y )


The more I look at this line the more boggled I get.  Shocked
What are you trying to do?
Re: Help with a grid using a for cycle
Reply #2 - Jan 15th, 2010, 3:43am
 
TfGuy44 wrote on Jan 15th, 2010, 3:38am:
Quote:
if(lastPos != x*y )


The more I look at this line the more boggled I get.  Shocked
What are you trying to do


Thx for the reply TfGuy44!
Thatt line is so that the sound will not repeat itself more then once. So if lastPos is the same number.. it means my cursor is in the same place cause the value will be different when its in a different cell.
Its a noobish way of doing it right srry.. i am just a begginer.
Re: Help with a grid using a for cycle
Reply #3 - Jan 15th, 2010, 5:00am
 
Plz help... i have to hand in this assignmant in 2 hours.
Re: Help with a grid using a for cycle
Reply #4 - Jan 15th, 2010, 5:16am
 
Perhaps with
lastPost != x + y * width
? At least, you won't have a conflict eg. if x=6 and y=10 vs. x=2 and y=30 vs. x=3 and y=20 etc. if you get my idea.

You can also store the coordinates of the first finger, and while you have a positive hit on this one, you don't check other positions.
Re: Help with a grid using a for cycle
Reply #5 - Jan 15th, 2010, 5:29am
 
well changing it to x + y * width makes it not reproduce any sound when pressed with 2 fingers at a time.

I tried doing something similar to what you sugested but i would get stuck and not really know how to handle it.. cause he is constantly checking the position and i cant stop the verification.

Here is the entire code if it helps.:


import TUIO.*;
TuioProcessing tuioClient;

import arb.soundcipher.*;

Ripple[] rip; // Declare the object array
int numRipples = 50; // Max number of ripples
int currentRipple = 0; // To keep track
color bg = 0;
int c1 = 255; // Color array
int c2 = 100;
color[] c = {color(c1, c2, 0), color(c1, 0, c2), color(c2, c1, 0)};
int col;
int which;
int rectWidth = 60;
int rectHeight = 80;
int lastPos = 2;
int val = 0;
float[] cursors;

SoundCipher sc = new SoundCipher(this);

void setup() {
 //background(0);
 size(600,800);  
 tuioClient  = new TuioProcessing(this);
 smooth();
 stroke(255);
 strokeWeight(3);
 noFill();
 rip = new Ripple[numRipples]; // Create the object
 for (int i = 0; i < numRipples; i++) {
   rip[i] = new Ripple();
 }
}

void draw() {

 background(bg);
 for (int p = 0; p < numRipples; p++) {
   rip[p].resize(); // Change the parameters
   rip[p].display(); // Actually draw 'em
 }
 Vector tuioCursorList = tuioClient.getTuioCursors();
 for (int i=0;i<tuioCursorList.size();i++) {
   //println(tuioCursorList.size());
   TuioCursor tcur = (TuioCursor)tuioCursorList.elementAt(i);
   Vector pointList = tcur.getPath();
   if (pointList.size()>0) {
     stroke(0,0,255);
     TuioPoint start_point = (TuioPoint)pointList.firstElement();
     for (int j=0;j<pointList.size();j++) {
       TuioPoint end_point = (TuioPoint)pointList.elementAt(j);
       //println(tcur.getCursorID());
       
       if(start_point.getScreenY(height) <= height / 3)
       {
         sc.instrument(25);
         val = 0;
         which = 0;
       }
       else if (start_point.getScreenY(height) >= height / 3 + height /3)
       {
         sc.instrument(23);
         val = 400;
         which = 1;
       }
       else
       {
         sc.instrument(12);
         val = 200;
         which = 2;
       }
       for (int y= 1; y<height; y += 80){
         for (int x= 1; x<width; x += 60){
           strokeWeight(1);
           noFill();
           //rect(x, y,rectWidth, rectHeight);
            if(tuioCursorList.size() == 0)
                {
                  println(tuioCursorList.size());
                  lastPos = 2;
                }
           if (start_point.getScreenX(width) >= x && start_point.getScreenX(width) <= x+rectWidth &&  
             start_point.getScreenY(height) >= y && start_point.getScreenY(height) <= y+rectHeight) {
             if(lastPos != x*y )
             {

               fill(0);
               //rect(0,0,600,800);
               fill(255);
               lastPos = x*y;
               col = x/20;
               //rect(x, y,rectWidth, rectHeight);
               start_point = end_point;
               //if(tuioCursorList
               if(tuioCursorList.size() == 1)
               {
                 sc.playNote(x / 4, y - val, 0.5);
                 //println(y);
               }
               if(tuioCursorList.size() == 2)
               {
                 float[] pitches = {
                   x / 4, x / 4 +4, x / 4 + 7, x / 4+11                  };
                 sc.playChord(pitches, 80, 1);
                 
               }
                // pick a color, any color (or the number associateing to that color in the array)
               rip[currentRipple].xy(x + 30, y + 40, which); // tell the upcoming ripple where it should be made and what color it should be
               currentRipple++; // advance to the next ripple int the array
               if (currentRipple >= numRipples) { // Reset if we max out numRipples
                 currentRipple = 0;
                 noFill();
               }
             }
           }
         }
       }
     }
   }
 }
}

void addTuioObject(TuioObject tobj) {

}

// called when an object is removed from the scene
void removeTuioObject(TuioObject tobj) {

}

// called when an object is moved
void updateTuioObject (TuioObject tobj) {

}

// called when a cursor is added to the scene
void addTuioCursor(TuioCursor tcur) {

}

// called when a cursor is moved
void updateTuioCursor (TuioCursor tcur) {

}

// called when a cursor is removed from the scene
void removeTuioCursor(TuioCursor tcur) {

}

// called after each message bundle
// representing the end of an image frame
void refresh(TuioTime bundleTime) {
 redraw();
}


its missing a class but its not important for what i need.
Page Index Toggle Pages: 1