Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Accessing variables
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Junaid2pac




PostPosted: Sun Jan 20, 2008 1:39 pm   Post subject: 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
code:

Enemy e1 = new Enemy (Ball.this);


and inside enemy class:
code:

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
Sponsor
Sponsor
Sponsor
sponsor
Junaid2pac




PostPosted: Sun Jan 20, 2008 2:06 pm   Post subject: 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




PostPosted: Sun Jan 20, 2008 2:07 pm   Post subject: Re: Accessing variables

i can post the ball class and/or enemy class if that would help
HellblazerX




PostPosted: Sun Jan 20, 2008 2:35 pm   Post subject: 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




PostPosted: Sun Jan 20, 2008 3:00 pm   Post subject: 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




PostPosted: Sun Jan 20, 2008 3:10 pm   Post subject: Re: Accessing variables

currently in my main class:
code:

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




PostPosted: Sun Jan 20, 2008 4:13 pm   Post subject: 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:
code:

        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:
code:

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 <= sumR)
        {
            return;
        }
       
        xE += dxE;
        //yE += dyE;
        Dimension d = box.getSize();
       
        ////////////////////////////
        /*Change velocity of enemy*/
        ////////////////////////////
        if(xE >= 600)
        {
            dxE *= -1;
        }
        else if(xE <= 50)
        {
            dxE *= -1;
        }
        /*if(yE >= 390)
        {
            dyE *= -1;
        }
        else if (yE <= 50)
        {
            dyE *= -1;
        }*/
       
                e.drawOval(xE, yE, xELength, yELength);
                runEnemy = false;
              }
        }
       
        public int getXBall()
        {
              return x;
        }
       
        public int getYBall()
        {
              return y;
        }
       
        public void run()
        {
        try
        {
        draw();
          do
          {
            if (runBall == true)
            move();
            //if (runEnemy == true)
            moveEnemy();
                        sleep(25);                                                //Speed of ball(25)
          }
          while(x<= 615 || y <= 390);                                   //Ball moves until it hits the edge of screen
        } catch (InterruptedException e)
        {
        }
        }
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: