Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Making many instances of a class

edited August 2017 in Questions Posts: 35

Hello,

I have made a class named showerBall that has properties such as velocity and position and should rebound off the corners of the screen. I want to call it from another function that makes lots of instances of this ball with different velocities and positions what's the best way to do this in a sort of automated fashion. I guess what I'm stuck on is that I that to come up with a bunch of names for the different instances of the class and put them in a table which seems laborious and I can't find a better solution for this.

thanks! Code below

ShowerBall = class()



function showerBall:init(x,y,speed)
    self.pos = vec2(x, y)
    self.radius = WIDTH*0.014
    self.speed = speed
    showerBallUse = "active"

end

function Ball:draw()
    local alpha = 255
    fill(0, 255, 63, 242)
    noStroke()
    ellipse(self.pos.x, self.pos.y, 2 * self.radius)


end

function Ball:update()
    self.pos = self.pos + self.vel
    if (self.pos.x + self.radius) >= WIDTH then
        self.pos.x = WIDTH - self.radius
        self.vel.x = -self.vel.x

    elseif (self.pos.x - self.radius) <= 0 then
        self.pos.x = self.radius
        self.vel.x = -self.vel.x

    elseif (self.pos.y + self.radius) >= HEIGHT then

    self:vanish()




    elseif (self.pos.y - self.radius) <= 0
    then
     self:vanish()
    end




end


function showerBall:vanish()
    showerBallUse = "stop Using"

end

Comments

  • Posts: 35

    Ps. Can someone explain how to post code on here so that it's in that blue rectangle? Thanks

  • Posts: 716

    Put three tildas ~~~ at the start and end of your code block.


    Code goes here
  • dave1707dave1707 Mod
    Posts: 7,468

    @rydergaz I added the ~~~ to the start and end of your code.

  • dave1707dave1707 Mod
    Posts: 7,468

    @rydergaz To use multiple instances of a class, just use a table. Here's your code creating 200 instances. You had a lot of errors in your code. Compare what I have to what you had to see what I corrected.

    function setup()
        sb={}
        for z=1,200 do
            table.insert(sb,ShowerBall(math.random(WIDTH),math.random(HEIGHT),math.random(10)))        
        end
    end
    
    function draw()
        background(0)
        for a,b in pairs(sb) do
            b:draw()
            b:update()
        end
    end
    
    ShowerBall = class()
    
    function ShowerBall:init(x,y,speed)
        self.pos = vec2(x, y)
        self.radius = WIDTH*0.014
        self.vel = vec2(speed,speed)
        self.showerBallUse = "active"
    end
    
    function ShowerBall:draw()
        local alpha = 255
        fill(0, 255, 63, 242)
        noStroke()
        if self.showerBallUse=="active" then
            ellipse(self.pos.x, self.pos.y, 2 * self.radius)
        end
    end
    
    function ShowerBall:update()
        self.pos = self.pos + self.vel
        if (self.pos.x + self.radius) >= WIDTH then
            self.pos.x = WIDTH - self.radius
            self.vel.x = -self.vel.x
    
        elseif (self.pos.x - self.radius) <= 0 then
            self.pos.x = self.radius
            self.vel = -self.vel
    
        elseif (self.pos.y + self.radius) >= HEIGHT then
            self:vanish()
        elseif (self.pos.y - self.radius) <= 0 then
            self:vanish()
        end
    end
    
    function ShowerBall:vanish()   
        self.showerBallUse = "stop Using"
    end
    
  • Posts: 35
    Ah I see thanks! Yeah excuse the errors I was making this class from a class called ball which I still hadn't finished converting
  • Posts: 35
    Is there a way to delete an instance of the class to free up memory
  • dave1707dave1707 Mod
    Posts: 7,468

    @rydergaz To delete an instance, just set it to nil. When garbage collection happens, it will free memory.

Sign In or Register to comment.