Two classes One fly

Hi guys, I’m seek for you help again. I have simple problem , how you can see in this video , i need remove from screen direct fly which collide with laser. Here are snippets of my code

Flys class

Flys = class()

function Flys:init()

    self.posx = -50
    self.score = 0
    self.flys = {}
    
    for i = 1,3 do 
        table.insert(self.flys,vec2(math.random(-4000,-50),math.random(100,HEIGHT-50)))
    end
    

    self.fwight = 1024/4
    self.fheight = 768/4
    self.posrndy = math.random(170,HEIGHT-150)
end


function Flys:draw()
    self.vecrand = vec2(math.random(-4000,-50),math.random(100,HEIGHT-50))
    
    
    self.rnd = math.random(0,1)
    self.posx = self.posx + 5
    
    for k,v in pairs(self.flys) do
    v.x = v.x + 3
    v.y = v.y + math.random(-3,3) 

    for a,b in pairs(self.flys) do
            table.insert(ctt,contactvec)
        end
        
    contactvec = vec2(v.x,v.y)

    if v.x > WIDTH+15  then    

        v.x = math.random(-100,-50)
        v.y = math.random(100,HEIGHT-50)
       
        table.remove(self.flys,k)     
        if #self.flys < 8 then
        table.insert(self.flys,self.vecrand)
        end
        
    end 
    if flydead == true and touching == true then
  

     end
    if self.rnd == 1 then
    sprite("Documents:Flyd",v.x,v.y,self.fwight,self.fheight)
    else
   sprite("Documents:Flyu",v.x,v.y,self.fwight,self.fheight)
    end
    end
    
    
   -- print(#self.flys)
end

function Flys:touched(touch)
    -- Codea does not automatically call this method
end

Lasers class

Lasers = class()

function Lasers:init()
    -- you can accept and set parameters here
    self.laserx = WIDTH/2+20
    self.lasery = 130
    self.angle = 0
    self.vec = vec2(WIDTH/2,0)
    
    
    
end
--t.x-lx,t.y-ly
function Lasers:draw()
    -- Codea does not automatically call this method
    self.las = vec2(self.laserx,self.lasery)
    
    self.tap = vec2(CurrentTouch.x-self.laserx,CurrentTouch.y-self.lasery)
    self.tap = self.tap:normalize()
    self.angle = math.deg(self.vec:angleBetween(self.tap))
    if touching == true then
    self.laserx = self.laserx + self.tap.x*30
    self.lasery = self.lasery + self.tap.y*30
    else
  --  self.laserx = WIDTH/2+20
  --  self.lasery = 130 
    end
    
   -- ellipse(WIDTH/2,HEIGHT/2,70)
    for i,z in pairs(ctt) do
       
             if #ctt > 5 then
        table.remove(ctt,i)
    end
    
    if z:dist(self.las) < 65 then
     --   print("sooqa")
        self.laserx = WIDTH/2+20
        self.lasery = 130
        
        flydead = true
        
    end
    end
    
    
    pushMatrix()
    
    translate(self.laserx,self.lasery)
    rotate(self.angle)
    
    sprite("Documents:Laser",0,0,1024/6,768/6)
    popMatrix()
end
--    ex=ex+dir.x*10
--    ey=ey+dir.y*10

function Lasers:touched(touch)
    -- Codea does not automatically call this method
    
end

try this

v=nil

instead of this

table.remove(self.flys,k)     

and please fix your indenting with all the for loops and if statements, your code is difficult to read, the way it is.

@Ignatz thank you very much. I will

@Ignatz , dont work because

    if self.rnd == 1 then
    sprite("Documents:Flyd",v.x,v.y,self.fwight,self.fheight)
    else
   sprite("Documents:Flyu",v.x,v.y,self.fwight,self.fheight)
    end
    end

if i have v = nil codea returns error

.

well, clearly, if you delete a fly, you should not try to draw it afterwards

try this

if self.rnd==1 and v~=nil then
etc

@Ignatz Thank you, works very good

@Ignatz , sorry for asking, but i messed around all day and i don’t have any progress Codea deletes all flys at screen as with table.remove

you need to post the code

@Ignatz

Lasers = class()

function Lasers:init()
    -- you can accept and set parameters here
    self.laserx = WIDTH/2+20
    self.lasery = 130
    self.angle = 0
    self.vec = vec2(WIDTH/2,0)
    
    
    
end
--t.x-lx,t.y-ly
function Lasers:draw()
    -- Codea does not automatically call this method
    self.las = vec2(self.laserx,self.lasery)
    
    self.tap = vec2(CurrentTouch.x-self.laserx,CurrentTouch.y-self.lasery)
    self.tap = self.tap:normalize()
    self.angle = math.deg(self.vec:angleBetween(self.tap))
    if touching == true then
    self.laserx = self.laserx + self.tap.x*30
    self.lasery = self.lasery + self.tap.y*30
    else
  --  self.laserx = WIDTH/2+20
  --  self.lasery = 130 
    end
    
   -- ellipse(WIDTH/2,HEIGHT/2,70)
    for i,z in pairs(ctt) do
       
             if #ctt > 5 then
        
      table.remove(ctt,i)
    end
    
    if z:dist(self.las) < 65 then
     --   print("sooqa")
        self.laserx = WIDTH/2+20
        self.lasery = 130
        
        flydead = true
        
    end
    end
    
    
    pushMatrix()
    
    translate(self.laserx,self.lasery)
    rotate(self.angle)
    
    sprite("Documents:Laser",0,0,1024/6,768/6)
    popMatrix()
end
--    ex=ex+dir.x*10
--    ey=ey+dir.y*10

function Lasers:touched(touch)
    -- Codea does not automatically call this method
    
end

Flys class

Flys = class()

function Flys:init()

    self.posx = -50
    self.score = 0
    self.flys = {}
    
    for i = 1,3 do 
        table.insert(self.flys,vec2(math.random(-1000,-50),math.random(100,HEIGHT-50)))
    end
    

    self.fwight = 1024/4
    self.fheight = 768/4
    self.posrndy = math.random(170,HEIGHT-150)
end


function Flys:draw()

    self.vecrand = vec2(math.random(-4000,-50),math.random(100,HEIGHT-50))
   
    
    self.rnd = math.random(0,1)
    self.posx = self.posx + 5
    
    for k,v in pairs(self.flys) do
    v.x = v.x + 3
    v.y = v.y + math.random(-3,3) 
        


    for a,b in pairs(self.flys) do
            table.insert(ctt,contactvec)
        end
        
    contactvec = vec2(v.x,v.y)

    
    if v.x > WIDTH+15 and v ~= nil then    
   --  table.remove(self.flys,k)
      --  table.insert
    
      v.x = math.random(-100,-50)
      v.y = math.random(100,HEIGHT-50)
       
  
        
    
    end
    if flydead == true then
      -- table.remove(self.flys,k) 
         table.insert(self.flys,k,nil)
        
     end
    if self.rnd == 1 and v ~= nil then
    sprite("Documents:Flyd",v.x,v.y,self.fwight,self.fheight)
    elseif v~= nil and self.rnd~= 1 then
    sprite("Documents:Flyu",v.x,v.y,self.fwight,self.fheight)
    end
    end
    
    
   -- print(#self.flys)
end

function Flys:touched(touch)
    -- Codea does not automatically call this method
end

what is this?

table.insert(self.flys,k,nil)

rather say k=nil

also instead of

 table.remove(ctt,i)

use z=nil

Your code indenting is still messed up. I can’t really be bothered to figure out code that’s in a mess. It’s hard enough to debug when it is neat, why do you want to make it harder?

@Ignatz , its messed up , i know, but can you give me an small example program , which makes what i want (in separate classes, if you can)? I know , sounds selfish, but i would be very grateful to you

I’m not sure what your code is supposed to do

@Ignatz my code supposed to remove fly , then laser and fly collide. Flys and lasers are in separate classes

@Ignatz i searched the forum, but i didnt find problems like mine