Results 1 to 4 of 4

Thread: Button Repitition Problem

  
  1. #1
    Neodudeman is offline -Hacks Newbie
    Join Date
    Dec 2005
    Posts
    11
    Rep Power
    0

    Default

    I have a problem when reading the buttons n stuff.

    Well, first, i did this draw program thing in one of the tuts found at Important Lua Websites. Here it is:

    Code:
    red = Color.new(255, 0, 0);
    black = Color.new(0, 0, 0);
    white = Color.new(255, 255, 255);
     
    canvas = Image.createEmpty(480, 272)
    canvas:clear(white)
     
    brush = {}
    eraser = {}
     
    x0 = 0
     
    y0 = 0
    x1 = 0
    y1 = 0
    while true do
    	pad = Controls.read()
    	dx = pad:analogX()
    	if math.abs(dx) > 32 then
    		x0 = x0 + dx / 64
    	end
    	dy = pad:analogY()
    	if math.abs(dy) > 32 then
    		y0 = y0 + dy / 64
    	end
    	if pad:cross() then
    		canvas:drawLine(x0, y0, x1, y1, black)
    	end
    	x1 = x0
    	y1 = y0
    	screen:blit(0, 0, canvas, 0, 0, canvas:width(), canvas:height(), false)
    	screen:drawLine(x1 - 5, y1, x1 + 5, y1, red)
    	screen:drawLine(x1, y1 - 5, x1, y1 + 5, red)
    	screen.waitVblankStart()
    	screen.flip()
    	if pad:start() then break end
    	if pad:select() then screen:save("screenshot.tga") end
    end
    so then i decided that I wanted to switch between the colors Red and Black by pressing R. I figured how to change it between Red and Black by using this:

    Code:
    black = Color.new(a, 0, 0);
    if pad:r() and a==0 then
    a=255
    elseif pad:r() and a ==255 then
    a=0
    end
    and so the problem is that I have to press the R REALLY REALLY fast for it to change, and it usually changes between the colors 3 or more times before I can release the key

    what can i do to correct this problem?


  2. #2
    modsyn is offline -Hacks Guru
    Join Date
    Aug 2005
    Location
    Shinigami Kurosaki Ichigo!
    Posts
    2,475
    Rep Power
    17

    Default

    i'm pretty sure there is a better worded version of what i'm about to say
    somewhere in one of the stickies, but if you don't want to search too much:

    first, read the controls outside of your main control loop, then compare each
    read with the value of the last one, like this:

    Code:
    oldPad = Controls.read()
    running = true
    while running do
      pad = Controls.read()
      if pad:R() then
        if pad ~= oldPad then
          --do some stuff 
        end
      end
      if pad:up() then
        if pad ~= oldPad then
          --do other stuff
        end
      end
      if pad:start() then
        if pad ~= oldPad then
          running = false
        end
      end
      oldPad = pad
    end
    jMEnc Guide, jMEnc2 page - by the way, you smell nice

  3. #3
    Neodudeman is offline -Hacks Newbie
    Join Date
    Dec 2005
    Posts
    11
    Rep Power
    0

    Default

    oh wow, cool, ty mucho! :E

  4. #4
    ST@TIC is offline -Hacks Newbie
    Join Date
    Nov 2006
    Location
    South Africa
    Posts
    39
    Rep Power
    14

    Default

    Dude the easier way, which i use is

    while running == true do
    pad = Controls.read()
    if pad~= oldpad then
    if (pad:r()) and (a == 255) then a = 0 end
    if (pad:r()) and (a == 0) then a = 255 end
    oldpad = pad
    end
    end

    \"Death be not proud, some have called thee mighty and great, thou art not so\"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •