Accelerometer

Ok so I found this code on the internet and it’s pretty cool. I was trying to add an accelerometer to it, but when ever I try to spawn a ball I get an error. Anyone know why this happens?


function setup()
    displayMode(FULLSCREEN)
    --myFPSReporter = FPSReporter(4)

    noSmooth()

    balls = {}
    touches = {}

    base = physics.body(EDGE, vec2(100,0), vec2(WIDTH-100,0))

    nextball = 0
end

function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        --if touches[touch.id] == nil then
        touches[touch.id] = touch
        --end
    end    
end    

function touchActions()
    for k,v in pairs(touches) do 
        if CurrentTouch.state == ENDED then
            --if there are no current touches then we kill all current touches to avoid bugged ball producers
            touches[k] = nil
        else

            --add a new ball at the touch location
            size = math.random(1,20)
            tspot = physics.body(CIRCLE, size)
            tspot.position = vec2(v.x+math.random(-1,1), v.y+math.random(-1,1))
            tspot.restitution = 1

            balls[nextball] = { tspot = tspot, size = size * 2, r = math.random(30,255), g = math.random(30,255), b = math.random(30,255) }

            nextball = nextball + 1
        end    
    end
end

function draw()
    background(0, 0, 0, 255)
    --myFPSReporter:draw(3)
    strokeWidth(0)
    
      
    

    for k,v in pairs(balls) do
        if v.tspot.x < -20 or v.tspot.x > WIDTH + 20 or v.tspot.y < -20 then
            balls[k].tspot:destroy()
            balls[k] = nil  
        else
            fill(v.r, v.g, v.b, 255)
            ellipse(v.tspot.x, v.tspot.y, v.size)
            x=x+Gravity.x*10
            y=y+Gravity.y*10
        end
        
        

    
    
    
    end

    touchActions()
end

the problem is that you never define a global var ‘x’

this

x = x + Gravity.x
y = y + Gravity.x

should be

v.tspot.x = v.tspot.x + Gravity.x
v.tspot.y = v.tspot.y + Gravity.y

Sir you are a magician (; @stevon8ter

@Willkassens i just looked at the error, and then looked at the line :wink:

Also, this is a very nice way to learn codea, nice job

Yea man all the wikis don’t work for me so I study other peoples codes and just learn from that. @stevon8ter

@Willkassens the problem is you’re trying to change the value of x which has been defined.

function setup()
    displayMode(FULLSCREEN)
    --myFPSReporter = FPSReporter(4)

    noSmooth()

    balls = {}
    touches = {}

    base = physics.body(EDGE, vec2(100,0), vec2(WIDTH-100,0))

    nextball = 0
end

function touched(touch)
    if touch.state == ENDED then
        touches[touch.id] = nil
    else
        --if touches[touch.id] == nil then
        touches[touch.id] = touch
        --end
    end    
end    

function touchActions()
    for k,v in pairs(touches) do 
        if CurrentTouch.state == ENDED then
            --if there are no current touches then we kill all current touches to avoid bugged ball producers
            touches[k] = nil
        else

            --add a new ball at the touch location
            size = math.random(1,20)
            tspot = physics.body(CIRCLE, size)
            tspot.position = vec2(v.x+math.random(-1,1), v.y+math.random(-1,1))
            tspot.restitution = 1

            balls[nextball] = { tspot = tspot, size = size * 2, r = math.random(30,255), g = math.random(30,255), b = math.random(30,255) }

            nextball = nextball + 1
        end    
    end
end

function draw()
    background(0, 0, 0, 255)
    --myFPSReporter:draw(3)
    strokeWidth(0)


    physics.gravity(Gravity)

    for k,v in pairs(balls) do
        if v.tspot.x < -20 or v.tspot.x > WIDTH + 20 or v.tspot.y < -20 then
            balls[k].tspot:destroy()
            balls[k] = nil  
        else
            fill(v.r, v.g, v.b, 255)
            ellipse(v.tspot.x, v.tspot.y, v.size)
        end






    end

    touchActions()
end

I fixed it by removing the x=x+Gravity lines to physics.gravity(Gravity)