![]() |
Keypressing
Hello,
I'd like to make simple game, when an Object (starship, car, rocket etc.) is controlled by LEFT and RIGHT buttons. When you hold LEFT it strafe left, when you hold RIGHT, it strafe right, when you release buttons - it stops moving. Like any common shooter. <------ /[]\ ------> I tried something like this: repeat; drawimage(ship,x,0); repaint; delay(10); key:=getkeypressed; if (keyToAction(key)=GA_LEFT) then x:=x-10; if (keyToAction(key)=GA_RIGHT) then x:=x+10; until false; It WORKS when I'm pressing ONE button only. But doesn't when I press both at same time. I'll try to explain this: 1. When I press (and hold) LEFT: CPU thinks i'm holding LEFT, and is doing his job. [left] <----/[]\ 2. Then, when I press (and hold) RIGHT, while still holding LEFT pressed: CPU now thinks i'm holding RIGHT. [left][right] /[]\----> 3a. When I release RIGHT button, while still holding LEFT pressed: CPU thinks RIGHT has been released, so in his opinion all buttons has been released and so he does nothing. [left] /[]\ 3b. Also when I release LEFT button, while still holding RIGHT pressed: CPU thinks that all buttons has been released and so he does nothing. [right] /[]\ I hope i made myself clear :P My questions: 1. How to fix it? 2. Does anybody could give me working "spacecraft movement" procedure, please? Thanks |
Ответ: Keypressing
This action can't be handled by standard MP functions, because they can't determine current button state, they only handle press/release events.
You should try to use get_key_states function from Lib_game, this possible can handle multiple key pressing. |
Ответ: Keypressing
Hmm.. i was also thinking about it. However I don't know how to use lib_game, and other 'non-standard things'.. any clues with that?
edit: I downloaded and installed 'game' library into my "..\MidletPascal 3.1\Libs\" folder. Then i added "uses game;" line to my game. Finally i changed the code: repeat; drawimage(ship,x,0); repaint; delay(10); key := game.get_key_states; if (game.get_bit(key, 4)<>0) then x:=x-10; if (game.get_bit(key, 32)<>0) then x:=x+10; until false; Compliation and buliding worked fine. The '*.jar' works. However nothing happens. I think the games freezes from some reason.. But dont know why. edit2: I realised than i'm missing "game.init", and "game.show" thingies. However when i put them, i get white blank screen. What are they for anyway? Please help.. |
Ответ: Keypressing
I searched the forum, and i found this:
Program test; uses game; const UP_PRESSED = 2; DOWN_PRESSED = 64; LEFT_PRESSED = 4; RIGHT_PRESSED = 32; FIRE_PRESSED = 256; GAME_A_PRESSED = 512; GAME_B_PRESSED = 1024; GAME_C_PRESSED = 2048; GAME_D_PRESSED = 4096; var img: image; key: integer; x,y: integer; procedure cls; begin setcolor(255,255,255); fillrect(0,0,getwidth,getheight); end; procedure init; begin img:=loadimage('/icon.png'); x:=20; y:=20; game.init; game.show; end; Begin init; repeat key:=game.get_key_states; if game.get_bit(key, LEFT_PRESSED)<>0 then x:=x-5; if game.get_bit(key, RIGHT_PRESSED)<>0 then x:=x+5; if game.get_bit(key, UP_PRESSED)<>0 then y:=y-5; if game.get_bit(key, DOWN_PRESSED)<>0 then y:=y+5; cls; drawimage(img,x,y); game.refresh; delay(50); until game.get_bit(key, FIRE_PRESSED)<>0; End. That's exactly what i need. I compiled it, but i when i run *.jar, i get blank white screen again! Please help.. |
Ответ: Keypressing
u can try to change game.init; -> game.init(1); :)
|
Ответ: Keypressing
Try to compile this in MP 2.0.2 version.
|
Ответ: Keypressing
Using MP 2.0 and changing game.init; to game.init(1); didn't work.
In fact, when i keep game.init; it doesn't compile at all. Still need help : ( edit: Maybe i have the library wrongly installed. All i have to do is to copy it into "..\MidletPascal 3.1\Libs\" folder? Also what [lib_name].init and [lib_name].show commands do? |
Ответ: Keypressing
Use MP 2.0.2 version, not MP 2.0.
I compile example from post #4 and it perfectly works. |
Ответ: Keypressing
Вложений: 2
Use Midlet Pascal 2.02
Copy Lib_game.class into "..\MIDletPascal\Libs\" It works!. |
Ответ: Keypressing
Thanks :) It works now.
|
Часовой пояс GMT +4, время: 11:36. |
vBulletin® Version 3.6.5.
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Перевод: zCarot