How to see if an object is part of a specific vector?

Discussion in 'Java' started by caleb, May 30, 2005.

  1. caleb

    caleb Guest

    I'm making a space invader-like game, and for one method
    getLocationOfNearestAlien, which lasers use in part to see if they are
    touching an alien, I need some help.

    public NearestAlienInfo getLocationOfNearestAlien(double x, double y) {
    if (aliens.isEmpty()) {
    return new NearestAlienInfo((int) x, (int) y, 100000000);
    }
    double closestDistanceSoFar = 100000000;
    Alien closestAlien = null;
    double d =0;
    double e =0;
    for(int i=0; i<aliens.size(); i++) {
    Alien a = (Alien) aliens.elementAt(i);
    if(a.contains(ufos)) {
    for(int z=0; z<ufos.size(); z++) {
    Ufo u = (Ufo) ufos.elementAt(i);
    d = u.getSizeX() /2;
    e = u.getSizeY() /2;
    }
    }
    else if(a.contains(smartAliens)) {
    for(int z=0; z<smartAliens.size(); z++) {
    SmartAlien s = (SmartAlien)
    smartAliens.elementAt(i);
    d = s.getSizeX() /2;
    e = s.getSizeY() /2;
    }
    }
    // doubles b and c should point to a pixel in the center of the alien
    double b = a.getX() + d;
    double c = a.getY() + e;
    //this is the actual ammt for Ufo before I decided I would need to make
    it more flexible for more alien types.
    //double b = a.getX() + 32; //that's half of the Ufo x
    pixels
    //double c = a.getY() + 18.5; // and half of the Ufo y
    pixels
    double dx = x - b;
    double dy = y - c;
    double distance = Math.sqrt(dx*dx + dy*dy);
    if (distance < closestDistanceSoFar) {
    closestDistanceSoFar = distance;
    closestAlien = a;
    }
    }
    return new NearestAlienInfo(closestAlien.getX(),
    closestAlien.getY(),
    closestDistanceSoFar);
    }

    There are different aliens. And each has a different size image that
    I'm using. The Alien class is the basic class that the class Ufo and
    SmartAlien extend. The Vector aliens includes all aliens but I want the
    if statements to work so that if the a object is also part of ufos (not
    just part of aliens), then it will add it's pixel size divided by two
    and then add that number to where it is so you end up with a pixel
    right in the middle of the Ufo. If it is not a Ufo I want it to check
    if it is a SmartAlien and so on with all the aliens I have made. I
    experimented with contains and equals but I couldn't get either to work
    if they are even the right way to do it. So if anyone could help me and
    perhaps show me some source code for how I should do it that would be
    fantastic. I'm new to java(and programming in general) though so I
    don't know all of the lingo yet. Also if anyone had any good links to
    starting java game programmers that would be great.
     
    caleb, May 30, 2005
    #1
    1. Advertising

  2. caleb

    Bjorn Abelli Guest

    "caleb" wrote...

    > I'm making a space invader-like game, and for one method
    > getLocationOfNearestAlien, which lasers use in part to see
    > if they are touching an alien, I need some help.


    A quick glance reveals your problem:


    > Alien a = (Alien) aliens.elementAt(i);
    > if(a.contains(ufos)) {


    ....

    > else if(a.contains(smartAliens)) {



    > I experimented with contains and equals but I couldn't get
    > either to work if they are even the right way to do it.


    Yes, it should work, but the other way around.

    An alien doesn't contain an ArrayList, but rather the other way around...


    Alien a = (Alien) aliens.elementAt(i);
    if(ufos.contains(a)) {

    ....

    else if(smartAliens.contains(a)) {

    .....


    I haven't checked for any other possible errors in the code, as this was
    what you asked for...

    // Bjorn A
     
    Bjorn Abelli, May 30, 2005
    #2
    1. Advertising

  3. caleb

    Johan Poppe Guest

    caleb wrote:

    1.
    > for(int i=0; i<aliens.size(); i++) {
    > Alien a = (Alien) aliens.elementAt(i);

    2.
    > if(a.contains(ufos)) {
    > for(int z=0; z<ufos.size(); z++) {
    > Ufo u = (Ufo) ufos.elementAt(i);
    > d = u.getSizeX() /2;
    > e = u.getSizeY() /2;
    > }
    > }
    > else if(a.contains(smartAliens)) {
    > for(int z=0; z<smartAliens.size(); z++) {
    > SmartAlien s = (SmartAlien)
    >smartAliens.elementAt(i);
    > d = s.getSizeX() /2;
    > e = s.getSizeY() /2;
    > }
    > }


    Quite aside from the question you actually ask, this code ha a lot of
    unnecessary things and I doubt it is doing what you intend it to do.

    At the end of each of the inner loops, (and after you have fixed the
    'contains' bug) d and e will contain the size of _the last_ element in
    either the smartAliens vector or the ufos vector. Is that what you
    want?

    I am not quite sure I understand your thinking, but I _think_ you
    should replace the whole if (ufos.contain(a)) .... else if ...
    structure with these two lines:

    d = a.getSizeX() /2;
    e = a.getSizeY() /2;

    Then you should make sure that the getSizeX and ...Y-methods returns
    the correct values for the various types of aliens.

    >I'm new to java(and programming in general) though so I
    >don't know all of the lingo yet. Also if anyone had any good links to
    >starting java game programmers that would be great.


    If you are new to programming, you should start by just learning
    programming and not focus too much on the "game" part just yet.

    --
    Johan Utne Poppe
     
    Johan Poppe, May 31, 2005
    #3
  4. caleb

    caleb Guest

    Thanks for the replies. When I say I'm new I mean I've taken an
    introductory course already but haven't really had any experience
    making games which is what I want to do. Anyway I fixed it and it works
    great. I've been basically using the reference material at java.sun.com
    to make my programs so it's takes a great deal of trying stuff to
    figure out how to use different things.

    Anyway have the vector contain the object makes perfect sense and I
    can't believe I couldn't think of that.

    Johan I'm not sure what you're saying but I'm sure what I'm doing
    probably isn't the best way. See the thing is, a is part of Vector
    aliens but aliens also contains ufos and smartAliens which both have
    different sizes. If you can show me a better way to do it I would
    appreciate it.
     
    caleb, May 31, 2005
    #4
  5. caleb

    caleb Guest

    Actually if I just put the source code for the whole thing on a website
    could you guys maybe look through it?
     
    caleb, May 31, 2005
    #5
  6. caleb

    Johan Poppe Guest

    caleb wrote:

    >Johan I'm not sure what you're saying but I'm sure what I'm doing
    >probably isn't the best way. See the thing is, a is part of Vector
    >aliens but aliens also contains ufos and smartAliens which both have
    >different sizes.


    Yes, I understand that they have different size. But the things you
    are doing now to get the size is unnecessary, and not even very
    precise. Try to really _look_ at your code. Follow the flow of
    execution, line by line. Think about which methods it is you are
    calling, on which objects.


    --
    Johan Utne Poppe
     
    Johan Poppe, May 31, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Flip
    Replies:
    3
    Views:
    992
    Tony Morris
    Feb 9, 2004
  2. pmatos
    Replies:
    6
    Views:
    24,028
  3. Replies:
    8
    Views:
    1,985
    Csaba
    Feb 18, 2006
  4. Javier
    Replies:
    2
    Views:
    602
    James Kanze
    Sep 4, 2007
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    386
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page