@lupino8211 difficult to make a detailed answer to your question. You could look at this code: it draws 300 butterfies, which are touch sensitive, and the drawing is efficient (if not ‘elegant’). Managing many buttefies is done by creatin a ‘butterfly’ class. Maybe this example can help you?
--# Butterflies
Butterflies = class()
function Butterflies:init(n)
self.colorList = {}
self.colorList[1] = color(253, 255, 0, 255)
self.colorList[2] = color(255, 188, 0, 255)
self.colorList[3] = color(255, 75, 0, 255)
self.colorList[4] = color(255, 0, 157, 255)
self.colorList[5] = color(252, 0, 255, 255)
self.colorList[6] = color(138, 0, 255, 255)
self.colorList[7] = color(0, 159, 255, 255)
self.colorList[8] = color(0, 247, 255, 255)
self.colorList[9] = color(0, 255, 158, 255)
self.colorList[10] = color(41, 255, 0, 255)
self.n = n
local id = {}
local ms = mesh()
print(ms)
print(#ms.vertices)
self.id = id
self.ms = ms
for i =1,n do
local c = self.colorList [rnd(10)]
local j = (i-1)*12
id[i] = Butterfly(ms,c)
end
end
function Butterflies:draw()
self.ms:draw()
for i,p in ipairs(self.id) do
p:draw()
end
end
function Butterflies:touched(touch)
for i,p in ipairs(self.id) do
p:touched(touch)
end
end
--# Butterfly
Butterfly = class()
rnd = math.random
cos = math.cos
sin = math.sin
pi = math.pi
function Butterfly:init(ms,c)
local x,y,z,w,h,o,r,j
local xc,yc = WIDTH/2,HEIGHT/2
local shape = {}
x = (rnd()-0.5)*WIDTH*0.9 + xc
y = (rnd()-0.5)*HEIGHT*0.9 + yc
z = 0
w = (rnd()+2)*4
h = w
o = 1 -- this is the opening of the wings
r = rnd()*2*math.pi
j = #ms.vertices
ms:addRect(x,y,w,h,r)
ms:addRect(x,y,w,h,r)
self.ms = ms
local p =self
p.j, p.shape, p.r, p.c, p.w, p.h, p.o, p.x, p.y = j,shape,r,c,w,h,o,x,y
p.beat, p.maxBeat = 0,1
self:redraw()
end
function Butterfly:redraw()
local p= self
local j,shape,r,c0,w,h,o,x,y = p.j, p.shape, p.r, p.c, p.w, p.h, p.o, p.x, p.y
local ms = self.ms
shape[1]=vec2(0,0):rotate(r)
shape[2]=vec2(w*o*1.3,h):rotate(r)
shape[3]=vec2(w*o/2,-h):rotate(r)
shape[4]=vec2(0,0):rotate(r)
shape[5]=vec2(-w*o*1.3,h):rotate(r)
shape[6]=vec2(-w*o/2,-h):rotate(r)
c = color(0,0,0,128)
local d,s
if self.flying then d=10 s=1.1 else d=4 s=1 end
for k=1,6 do
ms:vertex(j+k,shape[k]*s+vec2(x+d,y-d))
ms:color(j+k,c)
end
for k=1,6 do
ms:vertex(j+k+6,shape[k]*s+vec2(x,y))
ms:color(j+k+6,c0)
end
end
function Butterfly:draw()
local rnd = math.random
if rnd(500)==1 then self:turn() end
if rnd(500)==1 then self:wingBeat(rnd(20)+10) end
if rnd(10000)==1 then self:fly("random") end
self:wingBeat()
if self.flying then self:fly() end
end
function Butterfly:wingBeat(n)
local pap = self
if n then -- this is a start
pap.beat= n
pap.maxBeat = n
-- sound(SOUND_HIT, 39771)
else -- continue beating
if pap.beat ~=0 then
local o = (1.5+ cos(pap.beat/pap.maxBeat*2*pi))/2.5
pap.o = o
self:redraw()
pap.beat = pap.beat - 1
end
end
end
function Butterfly:turn()
local pap = self
local r = (rnd()-0.5)*pi/2
pap.r = pap.r + r
self:redraw()
end
function Butterfly:fly(type)
local alpha
if type then
self.flying = true
self.flyType = type
if self.flyType == "random" then
self.flySpeed = 1+rnd(1)
alpha = self.r + pi/2
self.flyDir = vec2(cos(alpha),sin(alpha))
end
else
if self.beat==0 then self:wingBeat(rnd(10)+10) end
if self.flyType == "random" then
if rnd(30)==1 then
self:turn()
alpha = self.r + pi/2
self.flyDir = vec2(cos(alpha),sin(alpha))
end
if rnd(300)==1 then self.flying=false end
end
if self.x <0 then self.x = self.x + WIDTH end
if self.x >WIDTH then self.x = self.x - WIDTH end
if self.y<0 then self.y = self.y + HEIGHT end
if self.y>HEIGHT then self.y = self.y - HEIGHT end
local dir,speed = self.flyDir,self.flySpeed
self.x = self.x + dir.x * speed
self.y = self.y + dir.y * speed
self:redraw()
end
end
function Butterfly:touched(touch)
if vec2(touch.x,touch.y):dist(vec2(self.x,self.y))<40 then self:fly("random") end
end
--# Main
-- 0 papillon
displayMode(FULLSCREEN)
-- Use this function to perform your initial setup
function setup()
print("Hello World!")
papillons = Butterflies(300)
-- fps=FPS()
end
-- This function gets called once every frame
function draw()
-- This sets a dark background color
background(68, 68, 68, 255)
papillons:draw()
-- fps:draw()
end
function touched(touch)
papillons:touched(touch)
end