GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes How do I save a variable value -Unsolved- Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do I save a variable value -Unsolved-" Watch "How do I save a variable value -Unsolved-" New topic
Author

How do I save a variable value -Unsolved-

Mark Quin
Greenhorn

Joined: Oct 21, 2007
Posts: 15
I am trying to make a snake animation with 10 circles. Each time the head of the snake moves in some direction, it will make the circle behind it move to where the head was previously. Likewise each circle moves to where the circle in front of it was previously.

Right now my snake is just a head:

How can I save a variable value, so I can move the other parts of the snake up? For instance taking the x,y and then setting the old x,y to the other circle behind it when ever it moves.

Also, any tips or examples on how I can get this to work in general would be of great help.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

The answer is simpler than you were expecting, I think: you just need some more member variables named (for example) oldxpos and oldypos:

oldxpos = xpos;
oldypos = ypos;
xpos = (new x position);
ypos = (new y position);


[Jess in Action][AskingGoodQuestions]
Mark Quin
Greenhorn

Joined: Oct 21, 2007
Posts: 15
When I tried that the old one was the same as the new value. It did not save the value. It just made another variable with the same value. So, when the new value change the old valued changed to the same exact thing.
[ October 26, 2007: Message edited by: Mark Quin ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You did something wrong, then. If you'd like to show us more code we could be more specific about how to implement this.
Mark Quin
Greenhorn

Joined: Oct 21, 2007
Posts: 15
Here is the snake class. I commented the line that I am working on with a line of # signs:
Mark Quin
Greenhorn

Joined: Oct 21, 2007
Posts: 15
Someone else I had talked to said it had something to do with the way I was setting things up. I did not quite understand what he was trying to tell me. It had something to do with adding this:

s1= new snake (p)
s2= new snake (p)
s3= new snake (p)
and so on... untill:
s8= new snake (p)
s9= new snake (p)
s10= new snake (p)

privateSnakehead;
public void setup
(Snake head, Snake next,true for head,true for taill)

s1.setup(s1,s2,true,false)
s2.setup(s1,s3,false,false)
and so on... untill:
s10.setup(s1,null,false,true)

if(leader)this
prop()
else
head.prop()

prop()
if(tail){
next.prop()
next.setxy(x,y)}

--------------------------------------
I would have asked him more but he had to leave. Anyone understand what he was trying to tell me. I understand part of it. Like how it checks for head or tail and how the head sends the value to all of the other parts of the snake. But, I still don't fully understand how to implement this.

Anyone understand what to do? I really want to understand how this would work.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I'd be more inclined to put my snakes in an array

It's a lot less code to type or read, and a lot easier to change sizes later. Then we can move by putting each circle on top of the one before it for an instant - like an inch worm - except for the head which has to go someplace new.

Does that make sense?

I did something like this years ago and didn't actually move all the body parts. I just added a new one on the head end of the drawing and erased the tail one. It still looked like everything moved. Would that do the job? Only works if all the body parts are identical in appearance.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How do I save a variable value -Unsolved-