I’m pretty lost when it comes to how the touch function works when it comes to classes and was wondering if anyone could help me solve this problem. My code (the touch function code isn’t mine) is below and whenever more than one ball is on the screen, they both move. Also, when one is removed from the table, they are all then removed. I would greatly appreciate any help.
--# Main
displayMode(FULLSCREEN)
function setup()
c1 = color(255, 255, 255, 0)
c2 = color(0,0,255)
c3 = color(255, 0, 0, 255)
balls={}
counter = 250
limit = 50
thrust = 0
gravity = 0
end
function draw()
background(255, 255, 255, 255)
rectMode(CENTER)
fill(0)
rect(WIDTH-100,HEIGHT-100,200,200)
counter = counter + 1
if counter > limit then
if limit <= 240 and limit > 150 then
limit = limit-50
end
counter = 0
create()
end
for i,b in pairs(balls) do
b:draw()
if b.y < -100 then
table.remove(balls,i)
end
end
end
function create()
table.insert(balls,Ball(WIDTH/2,-100,50,20,0,color(0,0,255),0,0))
end
function touched(touch)
for i,b in pairs(balls) do
b:touched(touch)
end
end
--# Ball
Ball = class()
function Ball:init(x,y,d,t,g,c,vx,vy)
-- you can accept and set parameters here
self.x = x
self.y = y
self.diameter = d
self.thrust = t
self.gravity = g
self.color = c
self.velx = vx
self.vely = vy
self.velocity = {}
self.newVelocity = vec2(0,0)
self.touchcircle = false
self.n = 0
self.touches = {}
end
function Ball:draw()
-- Codea does not automatically call this method
fill(self.color)
ellipseMode(RADIUS)
ellipse(self.x,self.y,self.diameter)
self.y = self.y + self.thrust - self.gravity
self.thrust = self.thrust * 0.98
self.gravity = self.gravity + 0.05
self.y = self.y + self.vely
self.x = self.x + self.velx
self.velx = self.velx * 0.98
self.vely = self.vely * 0.98
if self.x > WIDTH-self.diameter then
self.velx = 0
self.x = WIDTH-self.diameter
elseif self.x < self.diameter then
self.velx = 0
self.x = self.diameter
end
if self.y > HEIGHT-self.diameter/2 then
self.vely = 0
self.y = HEIGHT-self.diameter/2
end
if self.x < WIDTH and self.x > WIDTH-200 and self.y < HEIGHT and self.y > HEIGHT-200 then
table.remove(balls,b)
end
end
function Ball:touched(touch)
-- Codea does not automatically call this
if touch.state == MOVING then
self.newVelocity = vec2(touch.deltaX, touch.deltaY)
table.insert(self.velocity, 1, self.newVelocity)
end
if touch.state == ENDED then
for i = 1, 10 do
if self.velocity[i] then
self.n = self.n + 1.5
self.vely = self.vely + self.velocity[i].y
self.velx = self.velx + self.velocity[i].x
end
end
if self.n > 0 then
self.velx = self.velx/self.n
self.vely = self.vely/self.n
end
end
end