2019-08-01 00:01:27

Hello,

So at the end of my post is the code for the spikes, but the way they work is that when they're not active, they start a timer. When this timer elapses, they become non active again.

While active, if you walk within 2 tiles of them, you get skewered. But here's the problem, if I have multiple spikes, they all hit me, even if I'm only on one of them. Here's the code.

 spike@[] spikes(0); bool shitby=false;
class spike
{
int numb=random(1,4);
int x,y;
bool active=false;
timer activetimer; int activetime=random(300,500);
spike(int sx, int sy)
{
x=sx;
y=sy;
}
}
void spikeloop()
{
for (uint i=0; i<spikes.length(); i++)
{
if(spikes[i].active==false and spikes[i].activetimer.elapsed>=spikes[i].activetime)
{
spikes[i].active=true;
spikes[i].activetimer.restart();
p.play_3d("sounds/spike"+spikes[i].numb+".ogg",me.x,me.y,me.z,spikes[i].x,spikes[i].y,0,false);
}
else if(spikes[i].active==true and spikes[i].activetimer.elapsed>=spikes[i].activetime)
{
spikes[i].activetimer.restart();
spikes[i].active=false;
shitby=false;
}
if(me.x>spikes[i].x-2 and me.x<spikes[i].x+2 and me.y>spikes[i].y-2 and me.y<spikes[i].y+2 and spikes[i].active==true and shitby==false)
{
p.play_3d("sounds/spikehit"+random(1,2)+".ogg",me.x,me.y,me.z,spikes[i].x,spikes[i].y,0,false);
pain();
lhit="An iron spike";
health-=1;
shitby=true;}}
}
void spawn_spike(int x, int y)
{
spike s1(x,y);
spikes.insert_last(s1);
}
void destroy_all_spikes()
{
spikes.resize(0);
}
----------
“Yes, sir. I am attempting to fill a silent moment with non-relevant conversation.”
“You don’t tell me how to behave; you’re not my mother!”
“Could you please continue the petty bickering? I find it most intriguing.” – Data (Star Trek: The Next Generation)

2019-08-01 05:47:46

I'd maybe help you if forum would let me post the code.
It gives me the i can't be opened within it self or something like that error.

2019-08-01 10:41:27

You'll want to break out of the loop after the spike spikes you. Also, you may probably want to give the spikes a Z variable.

2019-08-01 11:32:15

Your code is very hard to read. If you'd do all the work in the spikeloop, I think the problem would be solved. I don't see the need for the spike hit me variable, either. Even better would be if you used classes way more. example: if enemies and player were subclassed from the same superclass, you could have an array of all living beings and in the spike loop you'd loop over that array and hit anyone in range. The way this would work is that you handle all keyboard input like moving, in the player class, maybe in a function like update. In the enemy class the update function could be much simpler and simply make the enemy move towards the player.

Roel
golfing in the kitchen