Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » Problem with keys

Credits go to bamccaig for helping out!
This thread is locked; no one can reply to it. rss feed Print
Problem with keys
Allfreeware
Member #8,435
March 2007

Hi all.4 days ago i've begin to program a fight game.
Now i've finish and i'm testing it.
I've found a bug in the menu for select caracter.
The menu' appear but when i press right or left key the game crash
This's the code:

1void movmenu()
2{
3 if(finetutto=1)
4 {
5
6 if(key[KEY_RIGHT])
7 {
8 if (xsel<xlimdes)
9 xsel=xsel+1;
10 {
11
12
13 if(xsel=1)
14 {
15 draw_sprite(buf,imgsel,x1,yselmen);
16 //sostituirlo manualmente
17 }
18 if(xsel==2)
19 {
20 draw_sprite(buf,imgsel,x2,yselmen);
21 }
22 if(xsel==3)
23 {
24 draw_sprite(buf,imgsel,x3,yselmen);
25
26
27 }
28 if(xsel==4)
29 {
30 draw_sprite(buf,imgsel,x4,yselmen);
31
32
33 }
34 if(xsel==5)
35 {
36 draw_sprite(buf,imgsel,x5,yselmen);
37
38
39 }
40 }
41 }
42 if(key[KEY_LEFT])
43 {
44 if(xsel>xlimsin)
45 xsel=xsel-1;
46 {
47 if(xsel==1)
48 {
49 draw_sprite(buf,imgsel,x1,yselmen);
50
51
52 }
53 if(xsel==2)
54 {
55 draw_sprite(buf,imgsel,x2,yselmen);
56
57
58 }
59 if(xsel==3)
60 {
61 draw_sprite(buf,imgsel,x3,yselmen);
62
63
64 }
65 if(xsel==4)
66 {
67 draw_sprite(buf,imgsel,x4,yselmen);
68
69
70 }
71 if(xsel==5)
72 {
73 draw_sprite(buf,imgsel,x5,yselmen);
74
75
76 }
77 }
78 }
79 }
80}

xsel=this's for cech what caracter is now "selected"
xlimdes=it's the max value xsel can be take
xlimsin=it's the min value xsel can be take
imgsel=the sprite of the "selection image"
yselmen=the y coordinate of the sprite
x1,x2,x3,x4,x5=the x coordinates of the sprite
finetutto=a var tell "movmenu" can't be use ingame.

Where's the problem?
Really thanks.
Ps:sorry for bad english.:-X

Onewing
Member #6,152
August 2005
avatar

Quote:

Where's the problem?

I don't think it's in the code you posted, unless imgsel or buf is messed up somehow. Just because you press left or right causes a crash doesn't mean that the problem is right there. Have you tried figuring out exactly where it crashes using a debugger?

------------
Solo-Games.org | My Tech Blog: The Digital Helm

Allfreeware
Member #8,435
March 2007

You've reason.The problem is the bmp,i've save it at 8 color and so allegro haven't read it.
But now,I can see the selection image,but i can't go over the first player,if i push right the img appear at the first player and if i relese it the img disappear and if i push again the right button the img appear always at the first player.:o
Can help me?
Thanks

Kris Asick
Member #1,424
July 2001

You should show us the code where imgsel is updated. Crashes are usually related to drawing null BITMAP objects, or drawing objects which aren't BITMAPs.

Also, please tell us if you're using datafiles or not.

--- Kris Asick (Gemini)
--- http://www.pixelships.com

--- Kris Asick (Gemini)
--- http://www.pixelships.com

Allfreeware
Member #8,435
March 2007

Now i've resolve the problem with this code,but now the img remain in the screen at the first player and i can't move it...:o
No,i'm not using a .dat file.

1void visualsprite()
2{
3 if(xsel=1)
4 {
5 draw_sprite(buf,imgsel,x1,yselmen);
6 clear;
7 //sostituirlo manualmente
8 }
9 if(xsel==2)
10 {
11 draw_sprite(buf,imgsel,x2,yselmen);
12 }
13 if(xsel==3)
14 {
15 draw_sprite(buf,imgsel,x3,yselmen);
16 }
17 if(xsel==4)
18 {
19 draw_sprite(buf,imgsel,x4,yselmen);
20 }
21 if(xsel==5)
22 {
23 draw_sprite(buf,imgsel,x5,yselmen);
24 }
25}
26void incremento()
27{
28 xsel++;
29 visualsprite();
30}
31void decremento()
32{
33 xsel--;
34 visualsprite();
35}
36void movmenu()
37{
38 if(finetutto=1)
39 {
40 if(key[KEY_RIGHT])
41 {
42 if (xsel<=xlimdes)
43 incremento();
44 }
45 if(key[KEY_LEFT])
46 {
47 if(xsel>=xlimdes)
48 decremento();
49 }
50 }
51}

Thanks

bamccaig
Member #7,536
July 2006
avatar

Allfreeware said:

if(finetutto=1)

I'm assuming the above variable is an integer. The equals sign (=) is an assignment operator (unless you've overloaded it, but it's impossible to tell from the code you've given), which means that you assign 1 to finetutto and evaluate it as a boolean conditional.

Allfreeware said:

if(xsel=1)

Same thing here. I'm pretty sure the above statements will always be true. To check if a value is equal to another one make sure you use the comparison operator (==).

Perhaps you manually entered the code and forgot an equals sign? This is where copy/paste is your friend: it's faster and it makes sure we see the exact code you're having problems with. It might also help to see a more complete picture (i.e. show more or all of the code).

In the case of xsel, you then go on to check if it equals 2, 3, 4, and 5. Unless it is being changed in a separate thread you should know as soon as the first is true that the others have to be false. For this reason, it appears that you should be using an else...if ladder:

1if(xsel==1)
2{
3 draw_sprite(buf,imgsel,x1,yselmen);
4 clear;
5 //sostituirlo manualmente
6}
7else if(xsel==2)
8{
9 draw_sprite(buf,imgsel,x2,yselmen);
10}
11else if(xsel==3)
12{
13 draw_sprite(buf,imgsel,x3,yselmen);
14}
15else if(xsel==4)
16{
17 draw_sprite(buf,imgsel,x4,yselmen);
18}
19else if(xsel==5)
20{
21 draw_sprite(buf,imgsel,x5,yselmen);
22}

Or even cleaner is a switch statement:

1switch(xsel)
2{
3case 1:
4 draw_sprite(buf,imgsel,x1,yselmen);
5 clear;
6 //sostituirlo manualmente
7 break;
8case 2:
9 draw_sprite(buf,imgsel,x2,yselmen);
10 break;
11case 3:
12 draw_sprite(buf,imgsel,x3,yselmen);
13 break;
14case 4:
15 draw_sprite(buf,imgsel,x4,yselmen);
16 break;
17case 5:
18 draw_sprite(buf,imgsel,x5,yselmen);
19}

Note that an else...if ladder or switch statement are only applicable if xsel doesn't change during these checks.

Sorry, I assume English isn't your first language? I'm having trouble understanding... It would help if you posted all the code (assuming it's not too much) and any error messages would also help. Perhaps if you run from the command-line you will get text output from Allegro?

Try to explain what each block of code does with comments. Especially try to explain what each variable represents and what type it is. More descriptive variable names would also help.

Also, there doesn't seem to be anything that prevents xsel from going below 1 or above 5. If it's out of range should it be moved back into range or do you just want to ignore it??

Allfreeware
Member #8,435
March 2007

Wow!Really Really thanks for the help.
I've change my code with it:

1void visualsprite()
2{
3 if(xsel==1)
4 {
5 draw_sprite(buf,imgsel,x1,yselmen);
6 //sostituirlo manualmente
7 }
8 else if(xsel==2)
9 {
10 draw_sprite(buf,imgsel,x2,yselmen);
11 }
12 else if(xsel==3)
13 {
14 draw_sprite(buf,imgsel,x3,yselmen);
15 }
16 else if(xsel==4)
17 {
18 draw_sprite(buf,imgsel,x4,yselmen);
19 }
20 else if(xsel==5)
21 {
22 draw_sprite(buf,imgsel,x5,yselmen);
23 }
24}
25 
26void movmenu()
27{
28 if(finetutto=1)
29 {
30
31 if(key[KEY_RIGHT])
32 {
33 if (xsel<xlimdes)
34 xsel=xsel+1;
35 {
36 visualsprite();
37 }
38 }
39 if(key[KEY_LEFT])
40 {
41 if(xsel>xlimsin)
42 xsel=xsel-1;
43 {
44 if(xsel==1)
45 {
46 visualsprite();
47 }
48 }
49 }
50 }
51}

And now the menĂ¹ work fine;)
Thanks for the tip (i think thet's the correct work:-X)
I think i will have other problem with this game...;D
Ps:yes,i'm not english,i'm italian.
Thanks for the help

Neil Black
Member #7,867
October 2006
avatar

bamccaig said:

Also, there doesn't seem to be anything that prevents xsel from going below 1 or above 5. If it's out of range should it be moved back into range or do you just want to ignore it??

Allfreeware said:

if (xsel<=xlimdes)
incremento();

and...

Allfreeware said:

if(xsel>=xlimdes)
decremento();

Those prevent it from going out of range.

bamccaig
Member #7,536
July 2006
avatar

Possumdude0 said:

Those prevent it from going out of range.

Guess I missed that...

If you ask me the check should be applied inside the function that accesses the variable. Those functions could be called elsewhere and if you forget to implement the check you could again go out of range. On top of that, if you implement the check in the increment/decrement functions it would eliminate a redundant check before calling the function (assuming the function could at some point be called elsewhere).

1void incremento()
2{
3 /*
4 * Optionally include code that will move it back in range. Otherwise, you'll
5 * have to check for > in the return condition.
6 */
7 if(xsel>xlimdes) // Why doesn't the > operator highlight?
8 xsel = xlimdes; // Why do semi-colons look like commas in Firefox!?
9 
10 // If at the limit return without change.
11 if(xsel == xlimdes)
12 return;
13 
14 // Increment.
15 xsel++;
16}
17 
18void decremento()
19{
20 /*
21 * Optionally include code that will move it back in range. Otherwise, you'll
22 * have to check for < in the return condition.
23 */
24 if(xsel<xlimdes)
25 xsel = xlimdes;
26 
27 // If at the limit return without change.
28 if(xsel == xlimdes)
29 return;
30 
31 // Decrement.
32 xsel--; // Why doesn't the -- operator highlight?
33}

It would also clean up the game logic a bit.

void movmenu()
{
    if(finetutto==1)
    {
        if(key[KEY_RIGHT])
            incremento();

        if(key[KEY_LEFT])
            decremento();

        // Redraw.
        visualsprite();
    }
}

Go to: