How should I get a connected array of circles (like a worm) to move across my canvas in a straight line?
up vote
2
down vote
favorite
void setup()
size(800,800);
background(0);
void draw()
intx= 20,40,60,80,100,120;
fill (255,0,00);
for (int i = 0; i <x.length; i ++)
ellipse (i*6, 100,10,10);
I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!
arrays animation processing
add a comment |
up vote
2
down vote
favorite
void setup()
size(800,800);
background(0);
void draw()
intx= 20,40,60,80,100,120;
fill (255,0,00);
for (int i = 0; i <x.length; i ++)
ellipse (i*6, 100,10,10);
I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!
arrays animation processing
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
void setup()
size(800,800);
background(0);
void draw()
intx= 20,40,60,80,100,120;
fill (255,0,00);
for (int i = 0; i <x.length; i ++)
ellipse (i*6, 100,10,10);
I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!
arrays animation processing
void setup()
size(800,800);
background(0);
void draw()
intx= 20,40,60,80,100,120;
fill (255,0,00);
for (int i = 0; i <x.length; i ++)
ellipse (i*6, 100,10,10);
I am trying to get the "worm" to move horizontally in a straight line across my canvas. Attached is the "worm." I don't know where to start with the movement because I need to move the whole array at once. Thank you!
arrays animation processing
arrays animation processing
edited Nov 11 at 20:36
Kevin Workman
33.2k53968
33.2k53968
asked Nov 11 at 19:56
G. Prescot
111
111
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
Store the current positions of the worm in a global array, where the head is the first position.
int x= 30,24,18,12,6,0;
int step = 6;
Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:
draw()
.....
for (int i = x.length-1; i > 0; --i )
x[i] = x[i-1];
x[0] += step;
.....
Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:
if ( x[0] >= width || x[0] < 0)
step *= -1;
Draw the worm in the reverse order:
for (int i = x.length-1; i >= 0; --i )
ellipse(x[i], 100, 10, 10);
The full code may look like this:
void setup()
size(200,200);
int x= 30,24,18,12,6,0;
int step = 6;
void draw()
See the preview:
add a comment |
up vote
0
down vote
Moving an array of values isn't much different from moving a single value.
Let's look at how we'd move a single value:
float x = 0;
void draw()
background(0);
ellipse(x, height/2, 20, 20);
x++;
To move an array of values, you'd do the same thing, but for each index of the array. Something like this:
float xArray = 20, 40, 60;
void draw()
background(0);
for(int i = 0; i < xArray.length; i++)
ellipse(xArray[i], height/2, 20, 20);
xArray[i]++;
Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Store the current positions of the worm in a global array, where the head is the first position.
int x= 30,24,18,12,6,0;
int step = 6;
Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:
draw()
.....
for (int i = x.length-1; i > 0; --i )
x[i] = x[i-1];
x[0] += step;
.....
Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:
if ( x[0] >= width || x[0] < 0)
step *= -1;
Draw the worm in the reverse order:
for (int i = x.length-1; i >= 0; --i )
ellipse(x[i], 100, 10, 10);
The full code may look like this:
void setup()
size(200,200);
int x= 30,24,18,12,6,0;
int step = 6;
void draw()
See the preview:
add a comment |
up vote
1
down vote
Store the current positions of the worm in a global array, where the head is the first position.
int x= 30,24,18,12,6,0;
int step = 6;
Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:
draw()
.....
for (int i = x.length-1; i > 0; --i )
x[i] = x[i-1];
x[0] += step;
.....
Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:
if ( x[0] >= width || x[0] < 0)
step *= -1;
Draw the worm in the reverse order:
for (int i = x.length-1; i >= 0; --i )
ellipse(x[i], 100, 10, 10);
The full code may look like this:
void setup()
size(200,200);
int x= 30,24,18,12,6,0;
int step = 6;
void draw()
See the preview:
add a comment |
up vote
1
down vote
up vote
1
down vote
Store the current positions of the worm in a global array, where the head is the first position.
int x= 30,24,18,12,6,0;
int step = 6;
Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:
draw()
.....
for (int i = x.length-1; i > 0; --i )
x[i] = x[i-1];
x[0] += step;
.....
Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:
if ( x[0] >= width || x[0] < 0)
step *= -1;
Draw the worm in the reverse order:
for (int i = x.length-1; i >= 0; --i )
ellipse(x[i], 100, 10, 10);
The full code may look like this:
void setup()
size(200,200);
int x= 30,24,18,12,6,0;
int step = 6;
void draw()
See the preview:
Store the current positions of the worm in a global array, where the head is the first position.
int x= 30,24,18,12,6,0;
int step = 6;
Change the position in every frame by the distance between 2 parts of the worm. Shift the current positions in the array in a loop:
draw()
.....
for (int i = x.length-1; i > 0; --i )
x[i] = x[i-1];
x[0] += step;
.....
Chang the direction if the end of the window or the begin of the window is reached, by the head of the worm:
if ( x[0] >= width || x[0] < 0)
step *= -1;
Draw the worm in the reverse order:
for (int i = x.length-1; i >= 0; --i )
ellipse(x[i], 100, 10, 10);
The full code may look like this:
void setup()
size(200,200);
int x= 30,24,18,12,6,0;
int step = 6;
void draw()
See the preview:
edited Nov 11 at 20:33
answered Nov 11 at 20:11
Rabbid76
31.5k112842
31.5k112842
add a comment |
add a comment |
up vote
0
down vote
Moving an array of values isn't much different from moving a single value.
Let's look at how we'd move a single value:
float x = 0;
void draw()
background(0);
ellipse(x, height/2, 20, 20);
x++;
To move an array of values, you'd do the same thing, but for each index of the array. Something like this:
float xArray = 20, 40, 60;
void draw()
background(0);
for(int i = 0; i < xArray.length; i++)
ellipse(xArray[i], height/2, 20, 20);
xArray[i]++;
Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.
add a comment |
up vote
0
down vote
Moving an array of values isn't much different from moving a single value.
Let's look at how we'd move a single value:
float x = 0;
void draw()
background(0);
ellipse(x, height/2, 20, 20);
x++;
To move an array of values, you'd do the same thing, but for each index of the array. Something like this:
float xArray = 20, 40, 60;
void draw()
background(0);
for(int i = 0; i < xArray.length; i++)
ellipse(xArray[i], height/2, 20, 20);
xArray[i]++;
Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.
add a comment |
up vote
0
down vote
up vote
0
down vote
Moving an array of values isn't much different from moving a single value.
Let's look at how we'd move a single value:
float x = 0;
void draw()
background(0);
ellipse(x, height/2, 20, 20);
x++;
To move an array of values, you'd do the same thing, but for each index of the array. Something like this:
float xArray = 20, 40, 60;
void draw()
background(0);
for(int i = 0; i < xArray.length; i++)
ellipse(xArray[i], height/2, 20, 20);
xArray[i]++;
Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.
Moving an array of values isn't much different from moving a single value.
Let's look at how we'd move a single value:
float x = 0;
void draw()
background(0);
ellipse(x, height/2, 20, 20);
x++;
To move an array of values, you'd do the same thing, but for each index of the array. Something like this:
float xArray = 20, 40, 60;
void draw()
background(0);
for(int i = 0; i < xArray.length; i++)
ellipse(xArray[i], height/2, 20, 20);
xArray[i]++;
Shameless self-promotion: here is a tutorial on animation in Processing, and here is a tutorial on arrays in Processing.
answered Nov 11 at 20:35
Kevin Workman
33.2k53968
33.2k53968
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53252638%2fhow-should-i-get-a-connected-array-of-circles-like-a-worm-to-move-across-my-ca%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown