Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

CurrentTouch initial state

edited April 2013 in Bugs Posts: 29

CurrentTouch initiates with a random setting, very easy to see if you create a simple project that, in the setup simply prints(CurrentTouch)

Every time you reset the program you'll get different values for the various bits of currentTouch, and it'll eventually crash.

Running the latest version of codea (1.5.1)

Comments

  • Posts: 1,595

    Best thing to use is function touched(touch) which gets called whenever any sort of touch state occurs

  • Posts: 29

    I realise that, but I'm pretty sure it's still a bug :)

  • Posts: 1,595

    Oh well in that case I'm not sure what to tell you, is it only crashing when you're resetting the game/viewer

  • edited April 2013 Posts: 29

    Tbh the crashing isn't the problem, it's the initial state being random. I'm mostly doing super quick programming examples for my kids (aged 8 and 4) and using CurrentTouch in the draw loop to draw an ellipse around the touch area - as the currentTouch starts of randomly the ellipse gets drawn initially despite there not being a touch (I am using touched(touch) to do things in reaction to the touch, but it was the visual feedback I'm after)

    So, here's a super quick example :

    -- touch
    
    -- Use this function to perform your initial setup
    function setup()
        taps = 0
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color 
        background(40, 40, 50)
    text(taps,WIDTH/2,HEIGHT/2)
        -- This sets the line thickness
        if CurrentTouch.state~=ENDED then
            ellipse(CurrentTouch.x,CurrentTouch.y,100)
        end
    
    end
    
    function touched(touch)
        taps=taps+touch.tapCount
    ends
    
    

    (I mention the crashing only because it happened -it may be totally irrelevant)

    Happy to work an alternative (and grateful for any simple suggestions!) but I'm sure I'm right in thinking CurrentTouch should either initiate with nil or other sensible value.

  • edited April 2013 Posts: 1,595
    -- ellipse
    
    -- Use this function to perform your initial setup
    function setup()
        touch = nil
    end
    
    function touched(t)
        --Handling one single touch, stops ellipse jumping between fingers if two or more are on screen
        if t.state == BEGAN and touch == nil then
            touch = t
        elseif touch and t.id == touch.id then
            if t.state == MOVING then
                touch = t
            elseif t.state == ENDED then  
                touch = nil
            end
        end
    end
    
    -- This function gets called once every frame
    function draw()
        -- This sets a dark background color 
        background(40, 40, 50)
    
        -- This sets the line thickness
        strokeWidth(5)
    
        -- Do your drawing here
        if touch then
            strokeWidth(10)
            stroke(135, 62, 29, 255)
            fill(194, 166, 122, 255)
            ellipse(touch.x,touch.y,75)
        end
    end
    

    I think this would be a better way of handling it, if this is what you're looking for, but what you do is fine, you could just do:

    If CurrentTouch.state == MOVING then
     ellipse(CurrentTouch.x,CurrentTouch.y,100)
    end
    
  • Posts: 29

    Cheers! I came up with a similar solution and will probably just avoid CurrentTouch (or, at least, avoid using it raw!)

    -pj

Sign In or Register to comment.