Skip to main content
gamedev.net
🔒 Locked

Java being stupid

Started by cyberninjaru 6 replies 1k views
Original Post
cyberninjaru
cyberninjaru
I recently posted about the NullPointerExceptions. I got that fixed. Now, I have a new problem. when I create all of my objects in: Particle particles[] = new Particle[numParticles]; for i=0;i = new Particle(); they aren't calling the constructor. I wrote a JOptionPane.showDialog() to show some of the value that should be in the particles when they're initialized, but the values were wrong. For example, public Particle() { xPos = 300; } when I check what value is in xPos right after the for loop, it says xPos = 0. Anyone have any ideas what might be wrong? Cyber
Mattman
Mattman
What does your Particle class look like?
cyberninjaru
cyberninjaru
Here is my particle class:


[source lang ="java"]import java.util.Random; import java.lang.Math;public class Particle{  public float lifetime;                      // total lifetime of the particle  public float decay;                          // decay speed of the particle  public int r,g,b;                            // color values of the particle  int x1,y1,x2,y2;                             // position of the particle  int width, height;                   int CenterX, CenterY;     public Vector2D Velocity = new Vector2D();   // speed of the particle  public boolean active;                       // is particle active or not?  	 public Particle()	 {		 		 		 CreateParticle(600, 600, 0);	 }  	 public void CreateParticle(int windowWidth, int windowHeight, int color)	 {		 Random rnd = new Random();         		 int life = rnd.nextInt(50) + 50;		 float posX = (float)rnd.nextGaussian() * 2;		 float posY = (float)(rnd.nextFloat() *2);		 int posEnd = rnd.nextInt(5) +1;		 lifetime= 200.0f;		 decay=0.001f;     		 switch(color)		 {			 case 0:    //Case red particles			 r = 255;			 g = 0;			 b = 0;			 break;				 case 1:   //Case blue particles			 r = 0;			 g = 0;			 b = 255;			 break;			 case 2:			 r = 0;			 g = 255;			 b = 0;			 break;		 }     		 x1 = windowWidth/2;		 y1 = windowHeight/2;		 x2 = x1 + 4;		 y2 = y1 + 4; 		 width  = (int)(x2 - x1);		 height = (int)(y2 - y1); 		 this.Velocity.SetX(posX);		 this.Velocity.SetY(posY);		 active = true;	 }	 	 public void UpdateParticle(int numParticles)	 {		 for(int i=0;i < numParticles;i++)		 {      			 lifetime -= decay;			 x1 += Velocity.GetX()+1;                         y1 += Velocity.GetY();			 x2 = x1 + 4;			 y2 = y1 + 4;			 width  = x2-x1;			 height = y2-y1;                          Velocity.SetY((float)Velocity.GetY() - 0.07f);		 }	 }}
smitty1276
smitty1276
I don't see a member called xPos.

Can you give an ACTUAL example of something you saw that was wrong?
cyberninjaru
cyberninjaru
Ok, sorry about that. I have 3 programming classes and I got my variables mixed up. It's not xPos, it's x1 that was showing 0 when it should have been showing 300;

When I call init() for my applet I run this code:

[source ="java"]public void init(){			windowWidth  = 600;	windowHeight = 600;	        for(i=0; i<numParticles;i++)		particles = new Particle();			s = "particle 0 initial x1: " + particles[0].x1;		JOptionPane.showMessageDialog(null, s, "piece of **** won't work",                 JOptionPane.INFORMATION_MESSAGE);		}

s is just a test string that I declared above init()
Blew
Blew
Where is numParticles declared?
blew
cyberninjaru
cyberninjaru
numParticles is declared above init() in the global area.

int numParticles = 300;

Any variables that aren't decalred in my posted code are above init.
smitty1276
smitty1276
Have you traced through the constuctor and helpers and watched it?
I'm looking a little bit more, but with a debugger in front of you it should be easy to find.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.