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 & HelpPrograms › Create New Object Every Second
Page Index Toggle Pages: 1
Create New Object Every Second (Read 566 times)
Create New Object Every Second
Oct 2nd, 2008, 10:42pm
 
Hi all,

I've been playing with an example script within the proMIDI library which sends MIDI data in accordance to some bouncing balls in a square.

I'd like to modify this code so that the number of balls increases by 1 every second.

Here is the code:

import processing.opengl.*;
import promidi.*;

MidiIO midiIO;
MidiOut midiOut;
Bowl[] bowl;

void setup(){
 size(128*5,128*5);
 background(0);
 smooth();

 //get an instance of MidiIO
 midiIO = MidiIO.getInstance(this);
 
 //print a list with all available devices
 midiIO.printDevices();
 
 //open an midiout using the third device and the first channel
 midiOut = midiIO.getMidiOut(0,2);
 
 bowl = new Bowl[1];
 for(int i = 0;i < bowl.length;i++){
   bowl[i] = new Bowl(i);
 }
 noStroke();
}

void draw(){
 background(0);
 for(int i = 0;i < bowl.length;i++){
   bowl[i].move();
   bowl[i].paint();
 }

}

class Bowl{
 float xSpeed;
 float ySpeed;
 float xPos;
 float yPos;
 Note note;
 color col;
 int myNumber;

 
 
 Bowl(int number){
   xSpeed = random(2,20);
   ySpeed = random(2,20);
   note = new Note(0,0,0);
   col = color(
     random(0,255),
     random(0,255),
     random(0,255)
   );
   myNumber = number;
 }
 

 

 void move(){
   xPos += xSpeed;
   yPos += ySpeed;
   midiOut.sendController(
     new Controller(myNumber, int(xPos/6)+2)
   );
   
   if(xPos > width || xPos < 0){
     xSpeed = -xSpeed;
     xPos += xSpeed;

     playNote();
   }
   if(yPos > width || yPos < 0){
     ySpeed = -ySpeed;
     yPos += ySpeed;
     playNote();
     midiOut.sendProgramChange(
       new ProgramChange(myNumber)
     );
   }
 }

 void playNote(){
   note = new Note(int(xPos/5f),int(yPos/10f)+60,int(random(1000)));
   midiOut.sendNote(note);
 }

 void paint(){
   fill(col);
   ellipse(xPos,yPos,20,20);
 }
}

I don't have a great deal of experience, and while I appreciate that this may well be a beginners question I did spend a long time trying to find a way myself before bothering you guys!

Thanks very much,

All the best >> Samana Tleilax
Re: Create New Object Every Second
Reply #1 - Oct 3rd, 2008, 10:35am
 
heres a sample how to "activate" something every second.
just adapt it to your code, you should do fine.


float startTime, currTime;
float hitTime;

void setup()
{
 hitTime = 1000; // 1000 millis = 1 second
 startTime = millis();
}

void draw()
{
 currTime = millis() - startTime;
 if( currTime >= hitTime )
 {
   startTime = millis();
   createNewObject(); // do your call here
 }
}
Re: Create New Object Every Second
Reply #2 - Oct 4th, 2008, 5:24pm
 
Hey!

Thanks for the reply,
I think a lot of the problem was my trying to do too much before I had gotten the basics. Will try and implement that code you posted when I have learnt a bit more XD
Page Index Toggle Pages: 1