I have been trying to get the keyboard to work for the arrows, but it has not worked for me. This is what I tried:
Asynckeydown in 100 period/ms timer
'If Keyboard.AsyncKeyDown(&h7B) then
'//do something with the left arrow key
'end if
'If Keyboard.AsyncKeyDown(&h7C) then
'//do something with the right arrow key…
'end if
'If Keyboard.AsyncKeyDown(&h7D) then
'//do something with the down arrow key…
'end if
If Keyboard.AsyncKeyDown(&h7E) then
Paddle.top = Paddle.top - 5
//do something with the Up arrow key…
end if
(I think it’s because I don’t know what the asynckey is…)
I have started a pong game, for a school assignment, and have been wondering how to get the ‘pongball’ to hit the rectangle. This is what I tried:
Same string as ‘pongball’ in the timers; MoveRight, MoveLeft.
*Also, I would like the rectangle to dissapear after being hit, I want an array (since I’ll be using doing the same exact thing for more than one rectangle) but don’t know how to do one.
Can you please help me figure out how to do what I need to do?
I don’t have any animation or game developing experience but I might can advise a little about the arrow keys. For whatever control that you are doing the animation with (canvas I would assume) you need to create a new event (key down). That event is passed a variable key that contains the key code pressed. You then can get the key value by using the Asc() function. The codes for left, right, up and down are 28 - 31.
if Asc(key) = 28 then 'left arrow do something
' code here
else if Asc(key) = 29 then ' right arrow do something
' code here
else if Asc(key) = 30 then ' up arrow do something
'code here
else if Asc(key) = 31 then ' down arrow do something
' code here
end if
[quote=167024:@William James]I don’t have any animation or game developing experience but I might can advise a little about the arrow keys. For whatever control that you are doing the animation with (canvas I would assume) you need to create a new event (key down). That event is passed a variable key that contains the key code pressed. You then can get the key value by using the Asc() function. The codes for left, right, up and down are 28 - 31.
if Asc(key) = 28 then 'left arrow do something
' code here
else if Asc(key) = 29 then ' right arrow do something
' code here
else if Asc(key) = 30 then ' up arrow do something
'code here
else if Asc(key) = 31 then ' down arrow do something
' code here
end if
[/quote]
How would I create a new event? Would it be in the key down section of canvas; is that what you meant by (key down)?
To create an event you right click on a control and select the “add to” menu item and then select “event handler …”. From there you can select which event and in this case you would select key down. Alternatively you could select the control in the navigator and then from the Xojo insert menu select event handler.
I then write the code you provided me with, correct?
if Asc(key) = 28 then 'left arrow do something
’ code here
else if Asc(key) = 29 then ’ right arrow do something
’ code here
else if Asc(key) = 30 then ’ up arrow do something
'code here
else if Asc(key) = 31 then ’ down arrow do something
’ code here
end if
if Keyboard.AsyncKeyDown(124) then shipVel.x = shipVel.x + s 'right
if Keyboard.AsyncKeyDown(126) then shipVel.y = shipVel.y - s 'up
if Keyboard.AsyncKeyDown(123) then shipVel.x = shipVel.x - s 'left
if Keyboard.AsyncKeyDown(125) then shipVel.y = shipVel.y + s 'down
I don’t use KeyDown events because they don’t the way you necessarily want, delayed repeats, etc. Also I’d run the Timer faster than 10 times a second, at least 30fps so a Period of 33 or Period 16 for 60fps.
Thank you for all the helpful comments, fortunately I was able to get the game to work with an email to the xojo company; A very helpful and nice person told me that the code I was looking for was;
if Asc(key) = 28 then 'left arrow do something
MsgBox “left”
elseif Asc(key) = 29 then ’ right arrow do something
’ code here
elseif Asc(key) = 30 then ’ up arrow do something
'code here
elseif Asc(key) = 31 then ’ down arrow do something
’ code here
end if
in my case I used this exact code, to work the keyboard, but I took out the MsgBox and left it with a code.
Thank you for all the help provided, and a special thanks to #William James, #Tim Hare and #Will Shank for being helpful; thank you.
(I still need the ‘pong’ help, by the way…oh and if you don’t mind telling me how a scoreboard works then that’d be great as well. Thanks)
The webinars didn’t really give me anything about a score board, and the forum is not what I was looking for since it has no score board, by scoreboard, I mean that:
Since I am doing the pong game, I wanted to have the pongball hit the two paddles and I want the scoreboard so that the game resets automatically when one of the paddles miss the pongball more than once;
I know that I need to use ‘Val’ and ‘Stl’ but don’t know what the code is.