This is a little something I threw together to amuse my toddlers. It generates balls at touch point(s) and they bounce
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)
end
end
touchActions()
end
Very nice and amusing for adults, too! I was planning on doing something like this based on an old Java program I wrote. I will be studying this. Thanks for sharing!
@spacemonkey tap on the screen with 3 fingers in a row, one after the other: tap-tap-tap! You’ll see something you probably didn’t expect! Also works if i slide 3 fingers, but not 2 or 4. Maybe it is because i have enabled zoom with 3 fingers double tap?
@spacemonkey you’re too humble and this is a lot of fun for such a small amount of code. I’m new to Codea so this sort of project is exactly what I’m interested in for learning the capabilities of the framework. My 13 year old son wants to help, as well, so I’ll be sharing this with him as a fantastic example. Thanks again!
.@SpaceMonkey i very much this program. This is another of CODEA gems that are or will be progressively deeply burried into the forum… I’d like to collect some of those gems in my web repo, so others (and I) can find them easily. Do you allow me to put the code in my web repo, crediting you as the author and owner of the code, of course?
Feel free to take any code I’ve put on the forum and stick it in your repo. I have a new physics one that I am hoping to publish soon as well if I can just knock the bugs out of it