Results 1 to 6 of 6
Thread: Moving elements in a vector
- 12-06-2011, 12:51 AM #1
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Moving elements in a vector
Hey guys,
I'm creating a snake game. My snake is made from a vector of shapes (the shape I am using is a custom shape class). The snake is implementing a keylistener.
When I call a move method on my snake I want the head, which is a separate element from the body, to move and each subsequent element in the vector to take the place of the one in front of it.
So I would like the first element in the vector to be at the heads previous location and the second element in the vector to be at the first, etc etc.
At the moment I'm using this method.
Java Code:if(ke.getKeyCode() == KeyEvent.VK_UP) { xLoc = head.getXLocation(); yLoc = head.getYLocation(); head.setLocation(xLoc, yLoc-12); for(int i = 0;i<5;i++) { body.get(i).setLocation(xLoc,yLoc); xLoc = body.get(i).getXLocation(); yLoc = body.get(i).getYLocation(); } }
- 12-06-2011, 01:04 AM #2
Re: Moving elements in a vector
Have you read the API doc for the Vector class?each subsequent element in the vector to take the place of the one in front of it.
Do any of its methods look like they will do what you want?
- 12-06-2011, 01:34 AM #3
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Moving elements in a vector
Well I think the get method will be fine, I'm just writing my method wrong. I am assigning each element in the vector to a single location, but I can't figure out the semantics to fix it.
I have a point A that is being adjusted. Each time point A is adjusted to point B, I want a vector of points to be adjusted like this
[A] Vector[1,2,3,4,5]
After
[B] Vector [A,1,2,3,4]
But right now it looks like this
[A] Vector[1,2,3,4,5]
after
[B] Vector[A,A,A,A,A]
- 12-06-2011, 02:12 AM #4
Re: Moving elements in a vector
Can you post a small program that compiles and executes and shows your problem?
- 12-06-2011, 02:40 AM #5
Member
- Join Date
- Nov 2011
- Location
- New Hampshire
- Posts
- 29
- Rep Power
- 0
Re: Moving elements in a vector
Here is the basic problem I'm having. It's not my program but it shows the area that I need assistance in.
You can see here that I'm assigning the values of all the components in the vector to the head value instead of assigning them the next position in the vector.
Also, here is the list it prints out on the first key stroke UP.Java Code:import java.awt.event.*; import java.util.*; import java.awt.Point; import java.awt.Color; public class Snakes implements KeyListener { private Point last, next; private int head, tailPiece; private Vector<Integer> body; private int xLoc, yLoc, headX, headY; private int direction = 2; public Snakes () { head = 100; body = new Vector<Integer>(); for(int i= 0; i<5; i++) { tailPiece = i*12; body.add(tailPiece); } } public void keyTyped(KeyEvent e) { } public void keyPressed(KeyEvent ke) { if(ke.getKeyCode() == KeyEvent.VK_UP) { head += 10; for(int i = 0;i<5;i++) { int x = body.get(i); x = head; System.out.println("Vector value at i: " +x); System.out.println("Snakes Head: " + head); System.out.println("Integer i: " + i); } } } public void keyReleased(KeyEvent e) { } public static void main(String[]args) { Frame f = new Frame(); Snakes s = new Snakes(); f.addKeyListener(s); } }
Vector value at i: 110
Snakes Head: 110
Integer i: 0
Vector value at i: 110
Snakes Head: 110
Integer i: 1
Vector value at i: 110
Snakes Head: 110
Integer i: 2
Vector value at i: 110
Snakes Head: 110
Integer i: 3
Vector value at i: 110
Snakes Head: 110
Integer i: 4
- 12-06-2011, 03:01 AM #6
Re: Moving elements in a vector
Where is the head value and where is the next position?I'm assigning the values of all the components in the vector to the head value instead of assigning them the next position in the vector.
The only methods I see you are using are the add and get methods.
In this example you gave the difference between A and B is that 5 was removed from the end of the Vector and A was inserted at the beginning of the Vector.[A] Vector[1,2,3,4,5]
After
[B] Vector [A,1,2,3,4]
Go read the API doc and see what methods will do that.
Then write a 10 line program to test out the methods. Those 10 lines include two printlns to show the before and after contents of the Vector.Last edited by Norm; 12-06-2011 at 03:06 AM.
Similar Threads
-
Print the sum of elements, determined by zero elements
By Dimitri in forum New To JavaReplies: 3Last Post: 10-19-2011, 11:42 PM -
swapping elements of vector
By sara12345 in forum New To JavaReplies: 1Last Post: 01-07-2010, 09:45 PM -
Vector<vector> loop thru
By ocean in forum New To JavaReplies: 11Last Post: 11-21-2009, 02:17 PM -
compare newly added Vector Element with previous elements
By nidhirastogi in forum Advanced JavaReplies: 10Last Post: 09-10-2008, 01:32 AM -
Finding elements in a vector
By Java Tip in forum java.langReplies: 0Last Post: 04-14-2008, 08:37 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks