Spawning Endless Object With Conditions
Posted: Thu Feb 15, 2024 10:21 pm
I am new to both Lua and Löve2D. Trying to learn them both by recreating Flappy Bird but got stuck on spawning pipes. I am creating the lower pipe based of on the upper pipe by adding a gap to its Y coordinate. It works good so far but I couldn't figure out a way to actually spawn them but not draw each one. (Impossible obviously.) I was trying to change the coordinates but soon realized that changing the pipes coordinates results in the change of first pipes coordinates too, so it would just glitch on the screen. (You get the idea of how a pipes spawn condition should be like in the original Flappy Bird) Here is what the base code seems like:
And a really quick question: If off screen objects are using memory like "setting a pipes X coordinate to -100" how could you actually destroy them once they are no longer used.
Code: Select all
function Pipes:load()
self.pipeSprite = love.graphics.newImage("sprites/pipe-green.png")
self.posY = math.random(100, love.graphics.getHeight() / 2)
self.posX = love.graphics.getWidth()
self.pipeGap = 80
self.pipeVelocity = 80
end
function Pipes:update(dt)
self.posX = self.posX - self.pipeVelocity * dt
end
function Pipes:draw()
love.graphics.draw(self.pipeSprite, self.posX, self.posY, 0, 1, -1)
love.graphics.draw(self.pipeSprite, self.posX, self.posY + self.pipeGap)
end