2015-05-27 07:46:17

Hi everyone!
please help find a bug in the code!
that's a mistake:
File: D:\MyGame\play.bgt
On line: 73 (1)
Information: Compiling void shoot()

File: D:\MyGame\play.bgt
On line: 75 (29)
Line: if(absolute(player_position-enemy.position)<=5)
Error: 'enemy' is not declared

File: D:\MyGame\play.bgt
On line: 77 (6)
Line: enemy.hp -= 10;
Error: 'hp' is not a member of 'int'

File: D:\MyGame\play.bgt
On line: 78 (58)
Line: enemy_d.play_1d("sounds/kill.wav", player_position, enemy.position, false);
Error: 'position' is not a member of 'int'


Here's the code:
#include "sound_pool.bgt"

int hp;
sound step;
sound vin;
int player_position;
int stena=20;
timer steper;
sound_pool enemy_d;
void main()
{
screen_reader_set_library_path(NVDA, "NVDA.dll");
show_game_window("Enemy");
screen_reader_speak(NVDA, "Добро пожаловать в enemy!");
wait(1000);
vin.load("sounds/vin.wav");
game_play();
}

void game_play()
{
enemy enemy;
while(true)
{
enemy.moove();
if(absolute(player_position-enemy.position)<=5)
{
enemy.shoot();
}
if(key_down(KEY_LEFT))
{
if(steper.elapsed>300)
{
player_position--;
int step_rand = random(1, 3);
step.load("sounds/step"+step_rand+".wav");
step.play();
steper.restart();
}
}
if(key_down(KEY_RIGHT))
{
if(steper.elapsed>300)
{
player_position++;
int step_rand = random(1, 3);
step.load("sounds/step"+step_rand+".wav");
step.play();
steper.restart();
}
}
if(player_position<1)
{
player_position++;
step.stop();
}
if(player_position>stena)
{
player_position--;
step.stop();
}
if(key_pressed(KEY_F4))
{
exit();
}
if(key_pressed(KEY_SPACE))
{
shoot();
}
}
}

void shoot()
{
if(absolute(player_position-enemy.position)<=5)
{
enemy.hp -= 10;
enemy_d.play_1d("sounds/kill.wav", player_position, enemy.position, false);
}
vin.play();
}

class enemy
{
int hp;
int position;
int speed;
sound_pool step;
timer steper;
sound_pool vin;
timer attack;
enemy()
{
hp = 20;
position = 20;
speed = 300;
}
void moove()
{
if(player_position<this.position)
{
if(steper.elapsed>speed)
{
this.position--;
step.play_1d("sounds/step3.wav", player_position, this.position, false);
steper.restart();
}
}
if(player_position>this.position)
{
if(steper.elapsed>speed)
{
this.position++;
step.play_1d("sounds/step3.wav", player_position, this.position, false);
steper.restart();
}
}
}
void shoot()
{
if(attack.elapsed>500)
{
vin.play_1d("sounds/vin.wav", player_position, this.position, false);
hp -= 5;
}
}
}

2015-05-27 14:53:35

The enemy variable is declared in the game_play function, so the shoot function does not know about it.
If shoot is a method of the enemy class, instead of enemy, you could use this.

看過來!
"If you want utopia but reality gives you Lovecraft, you don't give up, you carve your utopia out of the corpses of dead gods."
MaxAngor wrote:
    George... Don't do that.

2015-05-27 15:53:53

that is, how to fix the error?

2015-05-27 20:09:18

CAE_Jones just gave you the solution!

2015-05-28 16:43:20

@jonikster, fine, fine. WE'll explain it in simple terms: Cut the enemy variable into the top level code - do not initialize it in a function.

"On two occasions I have been asked [by members of Parliament!]: 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out ?' I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question."    — Charles Babbage.
My Github