File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do I save a variable value -Unsolved-

 
Mark Quin
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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);
 
Mark Quin
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the snake class. I commented the line that I am working on with a line of # signs:
 
Mark Quin
Greenhorn
Posts: 15
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic