Regresar

Archivo de la clase:

---------------------------------------------------
------- class Cosa


-- constructor
Cosa = class(
function(this,px,py,id)
this.px = px
this.py = py
this.radio = 40
this.radio2 =10
this.id = id
this.vx = of.random(-3.98183,3.984326)
this.vy = of.random(-2.98183,2.984326)
this.parpadea = 0
end
)

function Cosa:update()
  self.px = self.px + self.vx                          -- Comportamientos: velocidad
  self.py = self.py + self.vy
  self.parpadea = math.floor(of.random(5, 8))          -- parpadeo

  if self.px <= 40 or self.px+120 > OUTPUT_WIDTH then  --- rebotes
    self.vx = self.vx * -1
  end

  if self.py-80 <= 0 or self.py+80 > OUTPUT_HEIGHT then
    self.vy = self.vy * -1
  end

  if of.getElapsedTimeMillis()%self.parpadea == 0 then
    self.radio2 = 4
    self.radio = 35
  else
    self.radio2 = 10
    self.radio = 40
  end

end

function Cosa:draw()                                        -- composicion objeto clase
  of.fill()
  of.setColor(255)
  of.drawCircle(self.px, self.py, self.radio)
  of.drawCircle(self.px +80, self.py, self.radio)

  of.setColor(0)
  of.setLineWidth(2)
  of.noFill()
  of.drawCircle(self.px, self.py, self.radio)
  of.drawCircle(self.px +80, self.py, self.radio)

  of.fill()
  of.setColor(0)
  of.drawCircle(self.px, self.py, self.radio2)
  of.drawCircle(self.px +80, self.py, self.radio2)
  of.drawCircle(self.px +40, self.py+40, self.radio/2)
end

Archivo principal

--[[

  ----------------------------------------------------------
  Mosaic | OF Visual Patching Developer Platform

  Copyright (c) 2018 Emanuele Mazza aka n3m3da

    See https://github.com/d3cod3/Mosaic for documentation
  ----------------------------------------------------------

  Class and table example: A Lua script for Mosaic,
  created by mj

]]


require("Cosa")

maxCosas = 100
actualCosa = 0
cosas = {}
mouseX = 0
mouseY = 0

----------------------------------------------------
function setup()

end

----------------------------------------------------
function update()
  for i = 0, actualCosa-1 do  -- actualiza datos para las cosas actualmente creadas
    if actualCosa > 0 then
      cosas[i]:update()
    end
  end
end

----------------------------------------------------
function draw()
  of.setColor(255)
  of.drawRectangle(0, 0, OUTPUT_WIDTH, OUTPUT_HEIGHT)
  of.setCircleResolution(120)
        for i = 0, actualCosa-1 do
    cosas[i]:draw()
  end

        of.setColor(0)
  of.drawBitmapString(tostring(actualCosa),20,20)
end

----------------------------------------------------
function exit()

end

-- input callbacks

----------------------------------------------------
function keyPressed(key)

end

function keyReleased(key)

end

function mouseMoved(x,y)
  mouseX = x
  mouseY = y
end

function mouseDragged(x,y)

end

function mouseReleased(x,y)
  if actualCosa < maxCosas then
    cosas[actualCosa] = Cosa(mouseX, mouseY, actualCosa) -- iguala la tabla a la clase
   	actualCosa = actualCosa + 1   -- incrementa actualCosa +1 con cada evento del ratón
   end
end