Results 1 to 9 of 9
  1. #1
    andyman99008 is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default Problem with Space Invaders Game

    Hi, I'm having trouble making the aliens move properly in my space invaders game. They can move left and right, but when it gets to either side, only the aliens that hit the edge move back/down, then after a while they just move on top of the other images.

    I was wondering how I could make all the aliens move the same, even though they are all separate objects(needed for collision).

    I even tried creating a separate rectangle that moves as the aliens should move, then I added each alien to an array list at the x and y position of the rectangle + the size of the alien. This didn't work either.

    I'm tearing my hair out here! I will post whatever code you think would let you help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    It's all in the game logic. You likely have an Alien class that encapsulates the alien concept including its position and such. Do you need to create an AlienPack object that holds a collection of Aliens and has the logic that allows them to move in concert, that allows removal of an Alien when it has been hit, etc...?

  3. #3
    andyman99008 is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Yes thats what I want to do. But I can't think what I need to do for the Aliens to move in unison.

    Java Code:
    aliensX+=aliensXSpeed
    if aliensX < 0 or > BOARD_WIDTH
    {
    aliensXSpeed = -aliensXSpeed
    alienY+=ALIEN_JUMP;
    }
    I'm stuck on that basic logic...can you give me any pointers?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Again, if you have an AlienPack class, with a method such as move(), then this class could handle all the logic by iterating through each Alien held in the collection and moving them as it does so. Then it can test if an Alien has reached a boundary, and if so, reverse the direction for all the Aliens held in it.

  5. #5
    andyman99008 is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Ok so if my images are 30 wide and 30 tall.

    Java Code:
    iterate through arraylist
    for each alien
    alien.move()
    
    move(){
    alienX+=image width
    if(alienX+image width > BOARD_WIDTH or alienX<0)
    reverse direction
    
    
    }
    Does this make any sense?

    EDIT: god dammit, I just can't seem to even think the logic through, I have no idea how to make them move as one...If I iterate through and move each one, then that is moving them separately
    Last edited by andyman99008; 12-19-2010 at 07:33 PM.

  6. #6
    andyman99008 is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Ok, well, I just tried making this class, and it does exactly the same thing as before....

    Please help me with the logic.

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Don't worry too much, and in fact sometimes it's a good idea to walk away from the computer for a while. But you could even give your Alien#move method a parameter, perhaps an int to tell the Alien how far to move and which direction (if negative to the left, if positive, to the right), and have the AlienPack hold an int called direction, that it passes to each Alien when it calls move. Then AlienPack can change this variable when the pack reaches an edge.

    This is only one possible suggestion, but certainly not the only or even best way to do this. Again, take a walk, clear your mind before coming back to this. Then experiment, try different options, have fun with the code.

    Luck!

  8. #8
    andyman99008 is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Ye, I'm going to take a break...I just tried to think it through from the start again and couldn't even remember what I was trying to do. Ha.

    Thanks for your help, really appreciated.

  9. #9
    andyman99008 is offline Member
    Join Date
    Dec 2010
    Posts
    17
    Rep Power
    0

    Default

    Wow, I can't thank you enough, that little bit of advice about leaving the pc actually helped!
    Heres what I came up with, what do you think? Is the logic sound?



    Java Code:
    while array contains an alien;
    get the x coord
    if x > (board_width-alien width) && direction != -1
    direction = -1;    
    then iterate again
    set the y value +15
    //do the same for moving left but replace board_width-alien_width with x<alien_width && direction!=1
    
    then for moving along my x I have
    iterate through
    check if alien is visible
    then call a different move method in my Alien class
    alien.move(direction);
    
    //the move method in alien
    alienx+=direction;
    Again, thanks for all your help

Similar Threads

  1. Problem with PermGen Space
    By ulix83 in forum Eclipse
    Replies: 8
    Last Post: 11-24-2010, 03:29 PM
  2. Problem with PermGen Space in Jboss
    By ulix83 in forum NetBeans
    Replies: 1
    Last Post: 11-24-2010, 09:01 AM
  3. Space Invaders help!
    By Midge in forum New To Java
    Replies: 5
    Last Post: 03-04-2010, 04:38 PM
  4. Heap Space Problem
    By segolas in forum Advanced Java
    Replies: 6
    Last Post: 01-14-2010, 11:29 AM
  5. regex problem - allowing optional space
    By Norm in forum Advanced Java
    Replies: 26
    Last Post: 10-06-2008, 04:38 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
  •