Falling Problems
SkaxCo

I have a little game called Zombie Onslaught with zombies coming in (can you guess?) an onslaught. I went back to my original killing method of clicking on them. To make a long story short, I wanted to add in a new device (or 3) so I reanimated it. In the code for animating a zombie, I had: (well, this is the whole zombie class)

1class zombie{
2public:
3 float x;
4 float y;
5 float fall_speed;
6 int moving; /*0: moving, 1: exploding*/
7 int alive; /*1 = alive, 0 = dead*/
8 int type;
9 int frame;
10 int frame_delay;
11 void animate(){
12 x = (rand() % (SCREEN_W - 30)) + 1;
13 y = -30;
14 //LOOK AT THIS:!!!
15 fall_speed = ((rand() % 10) / 10) + 1;
16 alive = 1;
17 moving = 0;
18 type = 0;
19 frame = (rand() % 8) + 1;
20 frame_delay = 3;
21 }
22 void kill(){
23 x = y = 500;
24 alive = 0;
25 moving = 1;
26 }
27};

Despite the fact that everything is floats, everything falls at an integer speed, so everything at the same speed. If I bump it too ((rand() % 20) / 20) +1;, then everything falls at two speeds. Here is the movement code:

  for(int i = 0; i < ZOMBIE_MAX; i++){
    if(horde<i>.alive){
      horde<i>.y += horde<i>.fall_speed;
    }
  }

Anyone see anything blatantly wrong?

Rampage

The module operation returns an integer.

[edit]

// ((rand() % 20) / 20) + 1;

temp1 = rand() % 20; // integer between 0 and 19

temp2 = (temp1) / 20; // integer division, integer result, here's the problem

temp2 + 1; // integer result

// solution:

((rand() % 20) / 20.0) + 1; // float division

SkaxCo

So? I divide it by 10. Should I have the divide by ten in another line?
EDIT: Score! On different lines, it works out nice. Thanks bunches.

Rampage

You can do it in the same line. See my edit.

Tobias Dammers

Just a hint: Make it a habit to put all float constants (read: all numbers within a float context) as actual floats, and cast explicitly. I'd have put your statement like this:

fall_speed = ((float)(rand() % 10) / 10.0f) + 1.0f;

Thread #591261. Printed from Allegro.cc