We are about to switch to a new forum software. Until then we have removed the registration on this forum.
OK, I'm making a tower defense/ space invaders game and an enemy moves the same way a bunch of times in a row, but when I tried dropping it into a loop the enemy zoomed off the screen. So now I got a bunch of lines of code making the enemy do like a staircase moving. I think there is a way to make it less code, but idk. Here the movement code:
int x,y,t,sp;
int spd = 1;
if (t == 0){
sp = y;
t = 1;
}
if (t == 1){
y -= spd;
if (y <= sp-100){
t = 2;
sp = x;
}
}
if (t == 2){
x -= spd;
if (x < sp-290){
t = 3;
sp = y;
}
}// t == 1
if (t == 3){
y -= spd;
if (y <= sp-80){
t = 4;
sp = x;
}
}
if (t == 4){
x += spd;
if (x > sp+50){
t = 5;
sp = y;
}
}// t == 1
if (t == 5){
y -= spd;
if (y <= sp-80){
t = 6;
sp = x;
}
}
if (t == 6){
x += spd;
if (x > sp+50){
t = 7;
sp = y;
}
}// t == 1
if (t == 7){
y -= spd;
if (y <= sp-80){
t = 8;
sp = x;
}
}
if (t == 8){
x += spd;
if (x > sp+50){
t = 9;
sp = y;
}
}// t == 1
if (t == 9){
y -= spd;
if (y <= sp-80){
t = 10;
sp = x;
}
}
if (t == 10){
x += spd;
if (x > sp+50){
t = 11;
sp = y;
}
}// t == 1
if (t == 11){
y -= spd;
if (y <= sp-50){
t = 12;
sp = x;
}
}
if (t == 12){
x += spd;
if (x > sp+70){
t = 13;
sp = y;
}
}// t == 1
if (t == 13){
y += spd;
if (y >= sp+350){
t = 14;
sp = x;
}
}
if (t == 14){
x += spd;
if (x > sp+85){
t = 15;
sp = y;
}
}// t == 1
can anyone help me make this code shorter.
Answers
Probably. But I wouldn't touch that code with a ten-foot pole.
Instead, let's back up a bit and think about things a little. How are you enemies actually moving? Like the classic space invaders? Like this?
it's following a path that looks like this. that why I said tower defense space invaders they dont follow simple paths. I tried using a loop on the sides going up. After done copying the track I'l remove the picture then have the enemy do the track without the player seeing where the enemy will go.
Tower defense with pathfinding, where you can change or block the paths and the attackers (your bloons, a.k.a. 'creeps') will respond? Like A* path finding?
Or, as an alternate approach, do you just want each attacker to slide along a series of waypoints? If so, you can use a list-based interpolation:
See specifically the LerpVectorsExample.
PVector vecs[]
will contain a list of points that draws your path through the snow. Each attacker stores a distance traveled amount, 0-1.0 (100%), which indicates how far along the path it is. Increment the amount to move it along the path, e.g. +0.0002 for a slow attacker and +0.0005 for a fast one.This is just a starting point to one approach. Notice that:
For the attackers to maintain a constant speed on a path you need the list of path points to be placed equidistantly -- if you simply trace the corner points you will get fast edges and slow edges.
If your paths are different lengths then, in order for the attachers to maintain constant speed between multiple paths, you need attacker speed to be scaled to the length of the path they are on (since attacker speed is expressed as a percentage, not a distance). So an attacker on a 1000-long path would go +0.0002, and an attacker on a 500-long path would go +0.0004, and these would be the same visual speed on the screen.