no error but code not working

W

whiteman

Guest
One of my students is developing his own game. Its works fine but the code highlighted in red does not work. He doesn't get an error message but the code does not do what it is supposed to do. I can't find an issue with it either. It should work but doesn't

move_wrap(true, true, sprite_width/2)
if hp <= 0 {
instance_destroy()
}
if type == 1 {
sprite_index = s_archer
}
if type == 2 {
sprite_index = s_solder
}
if type == 3 {
sprite_index = s_ice_mage
}
if type == 4 {
sprite_index = s_fire_mage
}
if (attack = 0){
hsp = 0;
vsp = 0;
if o_king.y < y {
if (not place_meeting(x,y-3,o_team)) {vsp = -3}
}
if o_king.y > y {
if (not place_meeting(x,y+3,o_team)) {vsp = 3}
}
if o_king.x < x {
if (not place_meeting(x-3,y,o_team)) {hsp = -3}
}
if o_king.x > x {
if (not place_meeting(x+3,y,o_team)) {hsp = 3}
}
if (distance_to_object(o_king)) <= 10
{
vsp = 0;
hsp = 0;
}
x += hsp;
y += vsp;
}
// attack
if (attack = 1) {
if instance_e
xists(o_enemy_team){
hsp = 0;
vsp = 0;
direction = o_enemy_team;
if sprite_index = s_archer {
if distance_to_object(o_enemy_team) <= 300 {
hsp = 0;
vsp = 0;
if trigger1 {
alarm[4] = 60
trigger1 = false
}
}

}
if sprite_index = s_solder {
if distance_to_object(o_enemy_team) <= 5 {
hsp = 0;
vsp = 0;
if trigger2 {
alarm[3] = 60
trigger2 = false
}
}
}
if sprite_index = s_ice_mage {
if distance_to_object(o_enemy_team) <= 300 {
hsp = 0;
vsp = 0;
if trigger3 {
alarm[2] = 100
trigger3 = false
}
}
}

if sprite_index = s_fire_mage {
if distance_to_object(o_enemy_team <= 100) {
hsp = 0;
vsp = 0;
if trigger {
alarm[1] = 60
trigger = false
}
}
}
if o_enemy_team.y < y {
if (not place_meeting(x,y-3,o_team)) {vsp = -3}
}
if o_enemy_team.y > y {
if (not place_meeting(x,y+3,o_team)) {vsp = 3}
}
if o_enemy_team.x < x {
if (not place_meeting(x-3,y,o_team)) {hsp = -3}
}
if o_enemy_team.x > x {
if (not place_meeting(x+3,y,o_team)) {hsp = 3}
}

x += hsp;
y += vsp;
}
}
 

NightFrost

Member
What is supposed to happen with the code? I see that it is a number of conditionals checked to set an alarm, so either one more conditions is never met, or the code in the alarm has some problem.
 
No idea if this will help (as a pro-tip, read the forum guidelines first and you'll see that you should post code within the [ code][/code] tags to make it more readable and maintain it's formatting), but what do you think:
Code:
direction = o_enemy_team;
is doing? It sure as hell ain't setting a proper direction...

It should be something along the lines of:
Code:
direction = point_direction(x,y,o_enemy_team.x,o_enemy_team.y);
And that's ONLY if there's never going to be more than one instance of o_enemy_team. If there is multiple instances of it, you'll have to get the specific instance ID of the o_enemy_team object you are referring too.

As to the problem overall, do you know how to use the debugger? Set a breakpoint at the beginning of the not running code segment and step through and see what is happening/not happening. That will show you exactly what is going on. If you don't know how to use the debugger, then you should prioritise learning how that works before continuing on with teaching people. Also, some of the code is just sloppy in general, there's a few different code formatting styles going on, the student is using = for checking values, rather than the proper == (single = should only be used for assignment, though changing it will not fix this bug, it is just good coding practice), there a million missing ; (which, there is an off-chance that that is contributing to the bug, though it's unlikely). Also, switches are generally more readable than a bunch of if statements in a row.[/code]
 
Top