We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The move function works, but I am trying to get the character to walk vs slide across the screen. Upon adding the if statements for individual images it broke, and I am out of ideas to fix it. I am relatively new to coding, so it's possible that I am over complicating this. Is there an easier method that I am not aware of?
Thanks :-)
//8 bit test walk for Fred the stick figure
// 0 = nothing
// 1 = stand/look
// 2,3,4 = steps
int[] walkArray =
{
0, 0, 0, 2, 3, 4, 2, 3, 4, 2, 3, 4, 2, 3, 4, 1, 0, 0, 0, 0
};
Fred[] freds = new Fred[20];
void setup()
{
size(1000, 700);
rectMode(CENTER);
frameRate(30);
for (int i = 0; i < freds.length; i++)
{
freds[i] = new Fred (10, 20, 1, walkArray[i]);
}
}
void draw()
{
background(0);
for (int i = 0; i < freds.length; i++)
{
freds[i].walk();
}
}
class Fred
{
int n = 8; //number = pixel size
int count; //count from array
int x, y; //position
float speed;
Fred (int xpos, int ypos, float sp, int c)
{
x = xpos;
y = ypos;
speed = sp;
count = c;
}
void walk()
{
if (frameCount % 5 == 0) //time delay
{
x += speed;
if (x > height)
{
x = 0;
}
}
if (count == 1) //look right
{
background(0);
noStroke();
fill(130);
rect((x-1)*n, (y)*n, 1*n, 6*n);
rect((x+1)*n, (y)*n, 1*n, 6*n);
rect((x+.5)*n, (y+2.5)*n, 4*n, 1*n);
}
else if (count == 2) //step 1
{
background(0);
noStroke();
fill(130);
rect((x)*n, (y)*n, 1*n, 6*n);
rect((x+.5)*n, (y+2.5)*n, 2*n, 1*n);
rect((x-1)*n, (y-2)*n, 1*n, 2*n);
rect((x-2)*n, (y)*n, 1*n, 2*n);
rect((x-3)*n, (y+2)*n, 1*n, 2*n);
}
else if (count == 3) //step 2
{
background(0);
noStroke();
fill(130);
rect((x)*n, (y)*n, 1*n, 6*n);
rect((x+1)*n, (y-2)*n, 1*n, 2*n);
rect((x+2)*n, (y)*n, 1*n, 2*n);
rect((x+1)*n, (y+2)*n, 1*n, 2*n);
}
else if (count == 4) //step 3
{
background(0);
noStroke();
fill(130);
rect((x)*n, (y)*n, 1*n, 6*n);
rect((x+.5)*n, (y+2.5)*n, 2*n, 1*n);
rect((x+1)*n, (y-2)*n, 1*n, 2*n);
rect((x+2)*n, (y)*n, 1*n, 2*n);
rect((x+3)*n, (y+2)*n, 1*n, 2*n);
}
else //no nothing
{
background(0);
}
}
}
Answers
I haven't dug too deep in your code yet; but at least got it showing some "legs"! :-j
awesome! now i'm going to look up the new terms you introduced. :)
I am a little confused with this bit:
If i make the array larger will this cause problems? :)
no.
I've made more cleanups and discoveries as well:
No need to instantiate more than 1 Fred, since its walk() method knows how to perform all the "steps"! :P
I totally forgot about switch statements! that makes sense! THANK YOU!!!!!! ^:)^
Well,
if..else if..else
chained blocks are perfect fine. Very rarely I touchswitch...case
at all! :-$It's just that since your blocks were thick w/ even 5 statements that
switch...case
is aesthetically better!Regular
if..else if..else
are better for few statements in each 1!