Page 1 of 1

Help with spawning multiple objects

Posted: Mon May 18, 2020 4:13 pm
by leyohla
Hello, I was wondering if someone could help me out with spawning objects

I am trying to make another ball spawn once two objects collide with one another, through my 'addBalls' function (file found here):
Ball.lua
file containing 'addBalls' function
(2.85 KiB) Downloaded 168 times

Code: Select all

balls = {}

function addBalls(x, y)

    local ball = {}
    ball.width = 8
    ball.height = 8

    ball.dy = 0
    ball.dx = 0

    ball.skin = skin

    table.insert(balls, ball)

    for ball in pairs(balls) do
        love.graphics.draw(gTextures['main'], gFrames['balls'][ball.skin], ball.x, ball.y)
    end

end
And I call the function in the update function of my PlayState like this:

Code: Select all

if self.powerup:collides(self.paddle) then
        powerup = false
        Ball:addBalls(x,y)
end
But then I get the error message: PlayState.lua:92: attempt to call method 'addBalls' (a nil value)

Any help appreciated ^^

Re: Help with spawning multiple objects

Posted: Mon May 18, 2020 4:51 pm
by 4vZEROv
The cs50 course really is that bad ...

Your 'addBall()' function should :
- create a ball with 'Ball:init()'
- insert it inside the 'balls' table

Code: Select all

balls = {}

function Ball:init(x, y, skin)
    self.skin = skin
    self.x = x
    self.y = y

    self.width = 8
    self.height = 8
    self.dy = 0
    self.dx = 0
end

function addBalls(x, y)
    local ball = Ball:init(x, y)
    table.insert(balls, ball)
end

Code: Select all

if self.powerup:collides(self.paddle) then
        powerup = false
        addBalls(x,y)
end

Re: Help with spawning multiple objects

Posted: Tue May 19, 2020 7:47 am
by leyohla
Actually I didn't take cs50, I am taking gd50 their course in games development so I'm totally new to Lua.
Thanks for the help

Re: Help with spawning multiple objects

Posted: Tue May 19, 2020 8:06 am
by sphyrth
GD50 falls under CS50 (maybe it's the other way around, but you get the point).

Their tutorials were "great" back in the day, but they were posted at a time when Love2D was about to get a backwards-incompatible update. So, the spoon-fed nature of the tutorials collapsed as time goes on.

Even looking at the recent comments for their Pong Tutorial, beginners struggle because of a conflict with today's version Love2d, and the version of push.lua they were using at that time.

Re: Help with spawning multiple objects

Posted: Tue May 19, 2020 5:20 pm
by zorg
Add to that the people who don't even realize that push is an external library someone else made, and is not part of löve either...

Re: Help with spawning multiple objects

Posted: Wed May 20, 2020 5:09 am
by cristoferfb
For a more "familliar" way of use objects in Lua I recommend u to use a library like https://github.com/rxi/classic/