Results 1 to 4 of 4
  1. #1
    robbie is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Default Getting an Error, not too sure why - Arrays?

    My code here isn't compiling
    Getting th error "Cannot find symbol - method getX"
    i'm assuming it will give the same for getY once this error is fixed. Could it be an array thing to do with compCars?
    roughly 30 lines from the bottom of the code, i have marked it
    I'm pretty new to java and find it very confusing. any help would be much appreciated! :-)


    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    import javax.swing.event.*;


    public class GamePanel extends JPanel
    {
    private Driver driver;
    private Car PlayerCar, c;
    private Car [] compCars;
    private Timer timer;
    final private int DELAY = 100;
    private int score, cars, crashes, maxCars;
    private Random gen;
    int count;

    public GamePanel (Driver driver)
    {
    this.driver = driver;
    setPreferredSize (new Dimension (450,600));
    setBackground(Color.gray);
    setLayout(null);
    addMouseMotionListener (new TouchListener());
    timer = new Timer (DELAY, new AnimationListener());

    PlayerCar = new Car (7,4);
    add(PlayerCar);

    gen = new Random();

    maxCars = 13;

    startGame();

    }

    public void startGame() {

    score = 0; crashes = 0; cars = 0;
    //driver update score Callback pattern
    compCars = new Car [maxCars];
    //loop through the array create the individual cars
    for (int i = 0; i < maxCars; i++)
    //random number 0-12 random number 0-3*/)
    add(compCars[i],0);
    }
    {
    timer.start();
    }

    public void pauseGame (boolean play) {
    timer.stop();
    timer.start();
    }

    private void recordScore (int type) {
    count++;
    }

    public boolean collision(Car c) {
    //if (compCar x and width campare with the playerCar x and Width)
    if(c.getX() > PlayerCar.getX() &&
    c.getX() < PlayerCar.getX()+PlayerCar.getWidth())
    return true;

    if (PlayerCar.getX() <c.getX() + c.getWidth() &&
    PlayerCar.getX() + 450 < c.getX())
    return true;
    // if (compCar y and height " " " "...............)
    return false;
    }



    public void setDifficulty(int diff) {
    switch(diff) {

    case 1:
    maxCars = 3;
    timer.setDelay(100);
    break;

    case 2:
    maxCars = 5;
    timer.setDelay(100);
    break;

    case 3:
    maxCars = 10;
    timer.setDelay(100);
    break;

    case 4:
    maxCars = 5;
    timer.setDelay(50);
    break;

    case 5:
    maxCars = 10;
    timer.setDelay(50);
    break;

    case 6:
    maxCars = 13;
    timer.setDelay(50);
    break;

    case 7:
    maxCars = 10;
    timer.setDelay(25);
    break;

    case 8:
    maxCars = 13;
    timer.setDelay(25);
    break;

    case 9:
    maxCars = 10;
    timer.setDelay(1);
    break;

    case 10:
    maxCars = 13;
    timer.setDelay(1);
    break;
    }

    }


    private class TouchListener implements MouseMotionListener {
    public void mouseDragged(MouseEvent e) {
    int xPos = e.getX();
    if (xPos > 420)
    xPos = 419;
    if (xPos < 0)
    xPos = 1;
    PlayerCar.setLocation (xPos, PlayerCar.getY());
    }

    public void mouseMoved (MouseEvent e) {}
    }

    private class AnimationListener implements ActionListener
    {

    public void actionPerformed (ActionEvent event)
    {

    compCars.setLocation(compCars.getX(), compCars.getY() + compCars.getSpeed()); //ERROR ON THIS LINE!!!!!!!!!!!!!!!!!!!!! :confused:
    if(compCars.getY() > 480)
    {
    if(collision(compCars))
    {
    pauseGame();
    driver.incrementCrashes();

    int crash = JOptionPane.showConfirmDialog (null, "Would you Care to try again?");

    if (crash == JOptionPane.YES_OPTION){
    JOptionPane.showMessageDialog(null, "Thanks for playing");
    System.exit(0);
    }else{
    newGame();
    }
    }
    }

  2. #2
    mtyoung is offline Senior Member
    Join Date
    Dec 2008
    Location
    Hong Kong
    Posts
    473
    Rep Power
    6

    Default

    i'm assuming it will give the same for getY once this error is fixed.
    have you add back method getX to class Car?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,945
    Rep Power
    19

    Default

    private Car [] compCars;

    This is an array.
    You're calling a method that (presumably) exists on the Car class...but compCars is an array.

    I assume you want to move each car, so you'll have to iterate over the array and call the various Car methods on each Car. (ie compCars[x].getX(), and so on).

Similar Threads

  1. Replies: 1
    Last Post: 10-26-2009, 04:38 PM
  2. Replies: 3
    Last Post: 01-23-2009, 08:53 AM
  3. A little help with arrays..
    By zeppelin in forum New To Java
    Replies: 8
    Last Post: 01-05-2009, 12:33 PM
  4. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 07:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •