Bug when resetting new chain where it doesn't register

So I was trying to simulate tiny wings and got pretty far…but I needed to make the chain loop is when the track runs out it just resets…however the second physics chain doesn’t register as a global value…immediately after I initialiseit the print debug reports ‘nil’ as if it doesn’t exist. And the table method for writing the chain is working fine: that’s printed out and all so t he chain has something’s wrong with it…anyways can you guys kindly look over my coding and help me out? You’ll see the problem after you run it…

For those of you who haven’t heard of tiny wings: randomised track with different elevations of up hills and downhills. Touching the screen increases the gravity of the object you play as, not touching decreases it. There’s a minimum velocity that the ball must travel at…


function setup()
    iteration = 0
    scrx, scry = 0,0

    tab1, tab2 = {},{}
    tab1 = makeNewChain()
    makeChain()
    backup = slopes
    
    ball = physics.body(CIRCLE,10)
    ball.x = 110
    ball.y = 500
    ball.restitution = .01

end

function makeChain()
    slopes = physics.body(CHAIN, false, unpack(tab1))
end

function draw()
    background(253, 3, 106, 255)
    moveScreen()
--    c1 = physics.body()
    if math.floor(ball.x) > (3600 * iteration )-120 - WIDTH/2 then -- -WIDTH/2 then
        tab2 = tab1
        tab1 = makeNewChain()
         makeChain()
         backup = slopes
    end
    
    if(toucheds == true) then ball.gravityScale = 2 else ball.gravityScale = .3 end
 --   local prevX, prevY = 0,math.sin(math.rad(0)) * 50+ 200
    
    for a,b in pairs(tab1) do
     --   stroke(255, 255, 255, 255)
       -- strokeWidth(2)
        noStroke()
    ellipse(b.x,b.y,25)

    end
        for a,b in pairs(tab2) do
       -- stroke(255, 255, 255, 255)
        --strokeWidth(2)
        ellipse(b.x,b.y,25)
    end
    strokeWidth(3)
    ellipse(ball.x,ball.y,ball.radius*2)
   -- print(tab1[360].y.."=yval")
    
    
    if(ball.linearVelocity.x <100) then
        ball:applyForce(vec2(50,0))
    end
end

function moveScreen()
    if ball.x > WIDTH/2 - scrx then
        scrx = -ball.x + WIDTH/2
    end
    if ball.x < WIDTH/2-scrx then
        scrx = -ball.x + WIDTH/2
    end
    if ball.y < WIDTH/8 - scry then
        scry = -ball.y + WIDTH/2
    end
    if ball.y > WIDTH/8 - scry then
        scry = -ball.y + WIDTH/2
    end
    translate(scrx, scry)
    
    --if(ball.y >)
end

function touched(t)
    if t.state == BEGAN or t.state == MOVING then
        toucheds  = true
    else if t.state == ENDED then
        toucheds = false
    end end
end

function makeNewChain()
    local tab = {}
    
    local yInt = 200
    local amp = {}
    count = 1
    
    for a =0,20 do
        amp[a] = math.random(20,100)
    --    print(amp[a].x)
    end

    for a = (3600*iteration), 3600 * (iteration+1),10 do
        
        if iteration > 0 and a == 3600 * iteration then
            yInt = tab1[360].y - math.cos(math.rad(a)) * amp[(a - 3600*iteration)/180]
        end
        
        if a == 0 + (3600*iteration) then count = 1 end
        if(a %180 == 0 and a >  (3600*iteration)) then
                    count = count + 1
        yInt = tab[a/10 - (3600*iteration) -1].y - math.cos(math.rad(a)) * amp[(a - 3600*iteration)/180]
      --      print(yInt)
       end
        
        tab[a/10 - 3600*iteration] = vec2(a - (12*count) -240*iteration, math.cos(math.rad(a)) * amp[math.floor((a-3600*iteration)/180)] + yInt)
    end
    
    iteration = iteration + 1
     
    print("new CHAIN")
    return tab 
end

Thanks guys :slight_smile:

@Invad3rZIM In the makeNewChain function, the second time you make a chain, the size of tab is 0. It doesn’t look like you’re creating it correctly.

@dave1707 how would you fix that?

@david figured it out :slight_smile: thank you!!

@Invad3rZIM Please try and keep all of this in one thread, I believe a little while ago you were asking for tiny wings help in a different discussion. If you could just keep all your questions about making Tiny Wings in that discussion, it would make the forum less cluttered. Thanks.