387 posts
Location
in a chair in my room, probably
Posted 09 June 2015 - 06:15 PM
This problem's a bit more complex than my last entry. The code is supposed to let the user press one key and make the orange pixel run to either the left or the right depending on the key. However, if the user presses one of those two keys more than once, the pixel's speed in that direction increases to a point that it shouldn't. I really need help for this one. My code can be found
here. PLZ HELP!
-LM
EDIT: It might be easier to just use:
pastebin get kVdJBS3c helios
Edited on 09 June 2015 - 04:16 PM
3057 posts
Location
United States of America
Posted 09 June 2015 - 07:55 PM
The problem is here:
while true do
os.startTimer(.5) --#new timer
ev,a0,a1,a2=os.pullEvent()
if ev=="key" then
if a0==203 then
hs=-1
elseif a0==205 then
hs=1
elseif a0==15 then
break
end
elseif ev=="timer" then
if (x>1 and hs<0) or (x<w and hs>0) then
xp=x
x=x+hs
end
paintutils.drawPixel(xp,y,colors.black)
paintutils.drawPixel(x,y,colors.orange)
end
end
Your code starts a new timer each time
any event occurs. I'd recommend starting a new timer only after the previous timer event was caught. It'd also be a good idea to check if the timer event is, in fact, the same timer you started. Otherwise, if the program was run in parallel with something else (probably by an OS), you might catch timer events from other programs.
387 posts
Location
in a chair in my room, probably
Posted 09 June 2015 - 09:21 PM
THANK YOU! Now I can fix that issue! It didn't even occur to me that I should start the first timer BEFORE the loop. X'D