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 › Array....achtung!!!!!
Page Index Toggle Pages: 1
Array....achtung!!!!! (Read 439 times)
Array....achtung!!!!!
Apr 16th, 2009, 9:18am
 
I have a problem with this sketch...I have not idea how make to display each element of a String in each particle...I have almost architect, no informatic, becaus i newby in P5...Somebody have an idea???

//----------------      LIBRERIAS      ---------------------------------------//

import point2line.*;
//-----------------  VARIABLES GLOBALES   ------------------------------------//

PFont fuente;
Particula [] particula = new Particula [6];
Vect2 [] posicion = new Vect2 [6];
//----------------------------------------------------------//

void setup(){
 size(640, 400);
 smooth();
 noStroke();
 fuente = loadFont("Arial-Black-9.vlw");
 textFont(fuente);
 textAlign(CENTER);
 textSize(6);

for(int i = 0; i < posicion.length;i++){
 posicion = new Vect2 (random(width),random(height));
}
 for(int i = 0; i < particula.length; i++){
   particula[i] = new Particula(posicion,new Vect2(2,2));
 }
}
//----------------------------------------------------------//

void draw(){
 fill(0);
 rect(0,0,640,400);
 for(int i = 0; i < particula.length; i++){
   particula[i].render();
   particula[i].actualizar();
   particula[i].reboteBordes();
   particula[i].updateTexto();
 }
}


class Particula extends Texto{
 //--------  VARIABLES PRINCIPALES       -----------------------------------------//

 Vect2 vel;
 float diam;
 //------------    CONSTRUCTOR       -----------------------------------//

 Particula(Vect2 [] pos, Vect2 vel){
   super(pos);
   this.vel = vel;
   diam = 40;
 }  
 //------------    MÉTODOS      -----------------------------------//

 void render(){
   fill(255,100);
   for(int i = 0; i<pos.length; i++){
   ellipse(pos[i].x,pos[i].y,diam,diam);
   }
 }
 //---------------------------------------------------------//

 void actualizar(){
   pos.add(vel);
 }
 void updateTexto(){
   super.updateTexto();
 }
 //---------------------------------------------------------//
 //Método para que las pelotas reboten por la pantalla sin salirse fuera
 void reboteBordes(){
   for(int i = 0; i<pos.length; i++){
   if(pos[i].x < diam/2){                      // Borde vertical izquierdo
     pos[i].x = diam/2;
     vel.x *= -0.8;
   }
   if(pos[i].x > width-diam/2){                  // Borde vertical derecho  
     pos[i].x = width-diam/2;
     vel.x *= -1.2;
   }
   if(pos[i].y < diam/2){                      // Borde horizontal superior
     pos[i].y = diam/2;
     vel.y *= -0.8;
   }
   if(pos[i].y > height-diam/2){                 // Borde horizontal inferior
     pos[i].y = height-diam/2;
     vel.y *= -1.2;
   }
 }
 }
 //------------    FIN DE LA CLASE   -------------------------//
}



class Texto{
 //-------------        VARIABLES PRINCIPALES         ------------------------------------//

 Vect2 [] pos;          // Coordenadas de inserción del Texto
 String[] words = {
   "INCINERACION", "CREMACIÓN", "CIELO", "INFIERNO", "LIMBO", "ALMA  "      };
 //-------------        CONSTRUCTOR        ------------------------------------//

 // Tenemos que meterle al constructor todas estas variables
 Texto(Vect2 [] pos){
   this.pos = pos;
 }
 //-------------        MÉTODOS        ------------------------------------//

 // Método para actualizar el texto
 void updateTexto(){
   fill(255,0,0);
   textFont(fuente);
   textAlign(CENTER,CENTER);
   for(int i =0; i < pos.length; i++){
     for(int j =0; j < words.length; j++){
       text(words[j], pos[i].x, pos[i].y);
     }
   }
 }
 //-------------        FIN DE LA CLASE    ------------------------------------//
}







Re: Array....achtung!!!!!
Reply #1 - Apr 18th, 2009, 2:58am
 
I'm having a hard time understanding what you're trying to accomplish.. could you please try to explain again?
Re: Array....achtung!!!!!
Reply #2 - Apr 18th, 2009, 4:33pm
 
hi. i couldn't understand what is the problem you are having as well; but once you have mentioned some problem with retrieving words from a String[], the problem is probably on this part of your code

for(int i =0; i < pos.length; i++){
    for(int j =0; j < words.length; j++){
      text(words[j], pos[i].x, pos[i].y);
    }
  }

the way it is written it probably retrieves every word of the words[] for each time it checks the pos[], superimposing the entire words[] at each pos[i].

is text being output at all?
if you could describe what you want to accomplish and what is not happening or what is happening wrong it would be great.
Page Index Toggle Pages: 1