``````
-- Boltz

-- Use this function to perform your initial setup
function setup()
touches = {}
lives = 3
--physics.gravity(0)
counter = 0
createballs = false
debugDraw = PhysicsDebugDraw()
local sensor = physics.body(EDGE,vec2(-20,0),vec2(WIDTH+20,0))
sensor.sleepingAllowed = false
sensor.sensor = true
sensor.type = STATIC
balls = {}
difficulty = 3
positioncheckx = false
-- positionchecky = false
end

-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(154, 154, 163, 255)
if createballs ==  true then
for k,touch in pairs(touches) do
for h,circle in ipairs(balls) do
if touch.x <= circle.x+50 or touch.x >= circle.x - 50 then
if touch.y <= circle.y+50 or touch.y >= circle.y - 50 then
circle.x = touch.x
circle.y = touch.y
end
end
end
end
else
while counter < difficulty do
if counter == 0 then
table.insert(balls,createCircle(math.random(100,WIDTH-100),math.random(100,HEIGHT-100),50,true))
counter = counter + 1
elseif counter < difficulty-1 then
positionx = math.random(100,WIDTH-100)
for h,circle in ipairs(balls) do
if positionx >= circle.x-200 or positionx <= circle.x+200 then
positioncheckx = true
end
end
if positioncheckx == false then
table.insert(balls,createCircle(positionx,math.random(100,HEIGHT-100),50,true))
counter = counter + 1
end
positioncheckx = false
elseif counter == difficulty-1 then
positionx = math.random(100,WIDTH-100)
for h,circle in ipairs(balls) do
if positionx >= circle.x-200 or positionx <= circle.x+200 then
positioncheckx = true
end
end
if positioncheckx == false then
table.insert(balls,createCircle(positionx,math.random(100,HEIGHT-100),50,false))
counter = counter + 1
end
positioncheckx = false
end
end
createballs = true
end
debugDraw:draw()
end
function createCircle(x,y,r,decider)
local circle = physics.body(CIRCLE, r)
-- enable smooth motion
if decider == true then
circle.categories = {1}
else
circle.categories = {2}
end
circle.interpolate = true
circle.x = x
circle.y = y
circle.restitution = 0.25
circle.sleepingAllowed = false
return circle
end

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

function collide(contact)
if debugDraw then
debugDraw:collide(contact)
end
end

``````

@bocaaust , please, use the pattern

@bocaaust You are getting stuck in the “while” loop. The variable “counter” is getting added to 1 time, therefore it’s always less than “difficulty”. You add 1 to “counter” if it’s 0, then you don’t try to add 1 to it until “counter==difficulty-1” which it will never be. I also added three ~ before and after your code to format it correctly.

Sorry about not formatting the post properly — I know to use the three ~ for the future. The way I intended for the code to work, the counter would only increase if the math.random statement generated a x-position for a new circle body that would not initially overlap with previously generated circle bodies. It seems like the counter is never increasing for a second time because it doesn’t generate a random number that satisfies the if statement in time for whatever tolerance codea may have.

I got it working — no longer need help

@bocaaust you know, it wouldnt harm anyone just to write a little ‘thank you Dave1707 for taking the time to read my code, understand it, and trying to help me’, would it?

@Jmv38 I stopped expecting any type of “thank you” a long time ago.

@bocaaust You’re welcome. Codea isn’t the problem with your random numbers. math.random() does exactly what it’s supposed to do. It’s up to you to understand what it does and write your code accordingly. From what you said above, it’s sounds like you did just that to get your code working. There are very few problems with Codea, and when they’re found, they’re corrected as soon as possible.

I’m sorry but I am brand new to this forum and I didn’t realize there was a proper etiquette. Thank you so much @dave1707 for trying to help me with the problem. P.S. just in case your curious, the problem is with codea not the while loop because it continuously generates random numbers that are very similar. I solved the problem by putting a variable in called randomcheck which only let Codea generate 1200 numbers that did not satisfy the if statement before it defaulted to numbers that relatively do not and will not overlap based off of a constant multiplied by table.maxn(balls).

@dave1707, “I stopped expecting any type of “thank you” a long time ago.” That’s a shame, you always help out, you deserve some more recognition

@JakAttak I don’t expect to be thanked, but I’m sure everyone who responds to a question likes to know if what they posted actually helped or not.

@dave1707 it’s all helpful when it comes down to it.