Results 1 to 9 of 9
Thread: Problem with Space Invaders Game
- 12-19-2010, 06:59 PM #1
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
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!
-
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...?
- 12-19-2010, 07:09 PM #3
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
Yes thats what I want to do. But I can't think what I need to do for the Aliens to move in unison.
I'm stuck on that basic logic...can you give me any pointers?Java Code:aliensX+=aliensXSpeed if aliensX < 0 or > BOARD_WIDTH { aliensXSpeed = -aliensXSpeed alienY+=ALIEN_JUMP; }
-
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.
- 12-19-2010, 07:25 PM #5
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
Ok so if my images are 30 wide and 30 tall.
Does this make any sense?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 }
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 separatelyLast edited by andyman99008; 12-19-2010 at 07:33 PM.
- 12-19-2010, 08:05 PM #6
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
Ok, well, I just tried making this class, and it does exactly the same thing as before....
Please help me with the logic.
-
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!
- 12-19-2010, 08:22 PM #8
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
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.
- 12-19-2010, 09:20 PM #9
Member
- Join Date
- Dec 2010
- Posts
- 17
- Rep Power
- 0
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?
Again, thanks for all your helpJava 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;
Similar Threads
-
Problem with PermGen Space
By ulix83 in forum EclipseReplies: 8Last Post: 11-24-2010, 03:29 PM -
Problem with PermGen Space in Jboss
By ulix83 in forum NetBeansReplies: 1Last Post: 11-24-2010, 09:01 AM -
Space Invaders help!
By Midge in forum New To JavaReplies: 5Last Post: 03-04-2010, 04:38 PM -
Heap Space Problem
By segolas in forum Advanced JavaReplies: 6Last Post: 01-14-2010, 11:29 AM -
regex problem - allowing optional space
By Norm in forum Advanced JavaReplies: 26Last Post: 10-06-2008, 04:38 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks