
-----------------------------------
Junaid2pac
Sun Jan 20, 2008 1:39 pm

Accessing variables
-----------------------------------
i am making a game in which 2 thread classes (ball and enemy) are used and the collision math is inside the enemy class since i want the enemy to basically stop after the collision of the ball happens. I want to get the position values from the ball class. Would this be how it would look

This is inside the ball class....after the ball has changed position

Enemy e1 = new Enemy (Ball.this);


and inside enemy class:

Ball bx;
 
public Enemy(Ball bx)
{
    this.bx = bx;
    x = bx.getXBall();
    y = bx.getYBall();
}


but when using x and y inside the enemy class, i don't think the variables are getting the value properly
Please help

-----------------------------------
Junaid2pac
Sun Jan 20, 2008 2:06 pm

Re: Accessing variables
-----------------------------------
it just uses the original x and y value of the ball, and not the updated real time x and y value position

-----------------------------------
Junaid2pac
Sun Jan 20, 2008 2:07 pm

Re: Accessing variables
-----------------------------------
i can post the ball class and/or enemy class if that would help

-----------------------------------
HellblazerX
Sun Jan 20, 2008 2:35 pm

Re: Accessing variables
-----------------------------------
The Enemy object e1 you created only exists inside the Ball class.  You need to create a pointer in the Ball and Enemy class for the opposite class, and methods in each one to set those pointers.  But I have a question for you: why do you need each class to be a separate thread?  You could simply have a main thread that would do the calculations, and your Ball and Enemy classes would simply for storing values.

-----------------------------------
Junaid2pac
Sun Jan 20, 2008 3:00 pm

Re: Accessing variables
-----------------------------------
i would like to do that but i want the enemy class going if i click another button first and then the ball class for the other button....or is there some way to make 2 buttons for accessing one part of the class? Originally i wanted the enemy to be created when the game starts and have a button so that the ball can be created
pls help if this is possible

-----------------------------------
Junaid2pac
Sun Jan 20, 2008 3:10 pm

Re: Accessing variables
-----------------------------------
currently in my main class:

if(e.getActionCommand().equals("New Game"))
  {
                Enemy enemy = new Enemy(canvas, GameFrame.this);
                enemy.start();
  }
else if (e.getActionCommand().equals("Start"))
  {
  	Ball b = new Ball(canvas, GameFrame.this); 
        	b.start();
  }

is there a way to just have one class for ball and enemy yet i want the enemy to be created by a one button and ball created by a second button

-----------------------------------
Junaid2pac
Sun Jan 20, 2008 4:13 pm

Re: Accessing variables
-----------------------------------
I figured out how to put both ball and enemy in the same class, and when i click new game, only the enemy is created and it runs fine because i mad a runEnemy and runBall boolean. when the new game button is clicked, the runEnemy = true, while runBall = false. When the shoot button is pressed, runBall = true, and i set runEnemy = false. But when i click start both the enemy and new ball are created. This is all i need to fix now

Main class:

	public void actionPerformed (ActionEvent e)
  	{
  		if(e.getActionCommand().equals("New Game"))
  		{
  			runEnemy = true;
  			enemy = new Ball(canvas, GameFrame.this);
    		                enemy.start();
  		}
  		else if (e.getActionCommand().equals("Start"))
  		{
  			runBall = true;
  			ball = new Ball(canvas, GameFrame.this); 
        	                                ball.start();
  		}
               }

Ball and enemy class combined:

public void move()
  	{
  		if (runBall == true)
  		{
  			elapsed = System.currentTimeMillis() - start;			//Current time
    		if (!box.isVisible())
      			return;
			/*Set Ball*/
    		Graphics g = box.getGraphics();
			g.setXORMode(box.getBackground());				//Removes trail of the ball
    		g.fillOval(x, y, xLength, yLength);
    	
    		//Enemy e1 = new Enemy (Ball.this);
    	
    		///////////////////////////
			/*Speed of ball*/
    		x += dx;
    		y += dy;
    		Dimension d = box.getSize();
    
    		///////////////////////////
    		/*Change velocity of ball*/
    		///////////////////////////
    		if (x>= 615 || y>= 390)
    		{
    			dx = 0;
    			dy = 0;
    		}
    		else
    		{
    			if (x>=0)
    			{
    				dx = (Math.cos((Math.toRadians(angle)))) * velocity + windX;					//X Speed
    			}
    			dy = (Math.sin((Math.toRadians(angle)))) * velocity + ((windY + (accelY * (elapsed))) * 0.0005); 	//Y Speed
    		}
    	
    		g.fillOval(x, y, xLength, yLength);
    		g.dispose();
  		}
  	}
  	
  	public void moveEnemy()
  	{
  		if (runEnemy == true);
  		{
  		if (!box.isVisible())
      		return;
      		
      	/*Set enemy*/
    	/////////////
    	Graphics e = box.getGraphics();
		e.setXORMode(box.getBackground());
    	e.drawOval(xE,yE,xELength,yELength);
    	
                //Temp collision detection
    	if (xE - x = 600)
    	{
    		dxE *= -1;
    	}
    	else if(xE = 390)
    	{
    		dyE *= -1;
    	}
    	else if (yE 