# Allegro and C++ problem

This topic is 3386 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

OK, so I am buiding a small game in Allegro and for the past week or so have been figuring out the collision detection among other things and now have that but I am working on something to make it so that if you walk into a door it will show you the inside of the buildings in a little town I've built. Here's some of the code:

int state = 0;
if(hero_top > door1_bottom){
state = 0;
}
else if(hero_top < door1_bottom){
state = 1;
}
switch(state){
case 0:
draw_sprite(buffer, hero,
hero_x_position, hero_y_position);
draw_sprite(buffer, map,
0,0);
break;
case 1:
draw_sprite(buffer, hero,
hero_x_position,hero_y_position);
draw_sprite(buffer, building,
0,0);
break;


I know it is messy but its only part and sort of condensed. Anyway what would make state not change to 1? I've used almost the same thing about three times already just in this game so I think it should do what I want. Is there anything noticably wrong from the code? That is the only part of the program that has "int state" in it. I've tried changing the ifs to whiles which made things worse. I've tried moving the code around inside the program and making state start at 1 and move to zero but it just stays wherever I set it, among other things. Please, any help would be appriciated.

##### Share on other sites
Quote:
 Original post by In_Yack_ModeAnyway what would make state not change to 1?
The variable will not change to 1 when:

hero_top > door1_bottom, or
hero_top == door1_bottom

which can be condensed to simply:

hero_top >= door1_bottom

##### Share on other sites
That's what it's supposed to do but it isn't. What could be the reason? I've tried all I can think of.

##### Share on other sites
Well it could be a number of things, more information is needed to diagnose this though:

What makes you think that state isn't being assigned to one?

What actually are hero_top and door1_bottom in terms of data-type and/or implementation?

##### Share on other sites
Okay, so I just skimmed through a little bit, and I'm not sure if my code will even work, but try this and see if it works.

int state = 0;        if(hero_top > door1_bottom){            state = 0;        }        if(hero_top < door1_bottom){            state = 1;        }        if(state == 0)        {           draw_sprite(buffer, hero, hero_x_position, hero_y_position);           draw_sprite(buffer, map, 0,0);        }        else if(state == 1)        {           draw_sprite(buffer, hero, hero_x_position,hero_y_position);           draw_sprite(buffer, building, 0, 0);        }

##### Share on other sites
Quote:
 Original post by dmatterWell it could be a number of things, more information is needed to diagnose this though:What makes you think that state isn't being assigned to one?What actually are hero_top and door1_bottom in terms of data-type and/or implementation?

Ok first I know its not because I added this
if(state = 1)			{                draw_sprite(buffer, tree, 0, 0);            }

and it didn't draw the tree. hero_top is an int that represents the hero images top on the y-axis and changes when you press up or down. door_bottom is also an int but is set 10 below the bottom of the door to the building on the outside.
Quote:
 Okay, so I just skimmed through a little bit, and I'm not sure if my code will even work, but try this and see if it works.
Yes I had something like that but I decided to change to a switch statement.

##### Share on other sites
That's because

if(state = 1)

should be

if(state == 1)

##### Share on other sites
Quote:
 Original post by In_Yack_ModeOk first I know its not because I added this *** Source Snippet Removed ***and it didn't draw the tree.
Well that condition will always pass as true because you're making an assignment rather than a comparison with 1. So, if that's your actual code and it's not drawing the tree then either:

a) Something goes wrong in draw_sprite, or
b) That code is never reached at all.

Quote:
 hero_top is an int that represents the hero images top on the y-axis and changes when you press up or down. door_bottom is also an int but is set 10 below the bottom of the door to the building on the outside.
Get your character into the position in which you expect the state variable to equal 1, then check the values of hero_top and door1_bottom to be sure that they are what you think they should be. You could do this either by printing the values to the console, putting them in a popup message box or by using a debugger to watch the variables. You might find that your values are wrong, or that your assumptions about what the values should be are wrong.

##### Share on other sites
Also, make sure that your door bottom is adding the height of the door, I didn't think of that. The bottom of the door should be something like:

int door_bottom = door_position_y + door_height;

EDIT:
Yeah sorry, was just assuming :) . I did know that, just wasn't thinking :).

[Edited by - nooblet on October 12, 2008 12:49:59 AM]

##### Share on other sites
nooblet, just to point out, that plus could be a minus - it depends where the origin of the coordinate system lies and whether the entrance way is above or below the door itself (i.e. the direction of the door is factor).