• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How do I save a variable value -Unsolved-

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic