Help with spawning multiple objects

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
leyohla
Prole
Posts: 4
Joined: Mon May 18, 2020 3:45 pm

Help with spawning multiple objects

Post 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 ^^
User avatar
4vZEROv
Party member
Posts: 126
Joined: Wed Jan 02, 2019 8:44 pm

Re: Help with spawning multiple objects

Post 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
leyohla
Prole
Posts: 4
Joined: Mon May 18, 2020 3:45 pm

Re: Help with spawning multiple objects

Post 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
sphyrth
Party member
Posts: 260
Joined: Mon Jul 07, 2014 11:04 am
Contact:

Re: Help with spawning multiple objects

Post 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.
User avatar
zorg
Party member
Posts: 3465
Joined: Thu Dec 13, 2012 2:55 pm
Location: Absurdistan, Hungary
Contact:

Re: Help with spawning multiple objects

Post 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...
Me and my stuff :3True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
cristoferfb
Prole
Posts: 16
Joined: Wed May 15, 2019 12:41 am
Location: Valparaiso, Chile.

Re: Help with spawning multiple objects

Post 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/
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 0 guests