How to make a collectible respawn?

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
Krauly
Prole
Posts: 12
Joined: Thu Dec 08, 2022 6:12 pm

How to make a collectible respawn?

Post by Krauly »

Hi, im pretty new to programming in general so don't get mad if the question is stoopid xd.
Everything works perfectly except when I try to make the coins 'respawn', the mess is in the startRespawn function.
I have no idea about how to solve it, I probably f up some basic logic/rule?
I'm calling all the .draw,.update ect in the main and passing dt, but as soon as I collide with a coin I get the error
" attempt to perfrom arithmetic on local 'dt' (a nil value)"

Code: Select all

Coin = {}
Coin.__index = Coin
ActiveCoins = {}

function Coin.new(x,y)
  instance = setmetatable({}, Coin)
  instance.x = x
  instance.y = y 
  instance.img = love.graphics.newImage("IMG/diamond.png")
  instance.width = instance.img:getWidth()
  instance.height = instance.img:getHeight()
  instance.scaleX = 1
  instance.toBeRemoved = false
  instance.respawnTimer = 0
  instance.respawn = false
  instance.physics = {}
  instance.physics.body = love.physics.newBody(world, instance.x, instance.y, 'static')
  instance.physics.shape = love.physics.newRectangleShape(instance.width, instance.height)
  instance.physics.fixture = love.physics.newFixture(instance.physics.body, instance.physics.shape)
  instance.physics.fixture:setSensor(true)
  table.insert(ActiveCoins, instance)
end

function Coin:startRespawn(dt)
  for i,instance in ipairs(ActiveCoins) do
    if instance == self then
      if self.toBeRemoved then
        self.respawnTimer= self.respawnTimer + dt
        if self.respawnTimer > 5 then
          Coin.new(self.x, self.y)
          self.respawnTimer = 0
        end
      end
    end
  end
end

function Coin:remove()
  for i,instance in ipairs(ActiveCoins) do
    if instance == self then
      Player.coins = Player.coins + 1
      self.physics.body:destroy()
      table.remove(ActiveCoins, i)
    end
  end
end

function Coin:spin(dt)
  self.scaleX = math.sin( love.timer.getTime() * 1.5 )
end

function Coin:update(dt)
  self:spin(dt)
  self:startRespawn(dt)
  self:checkRemove(dt)
end

function Coin:checkRemove(st)
  if self.toBeRemoved then
    self:remove(  )
  end
end

function Coin:draw()
  love.graphics.draw(self.img, self.x, self.y, 0,self.scaleX,1, self.width/2, self.height/2)
end

function Coin:updateAll( dt )
  for i,instance in ipairs(ActiveCoins) do
    instance:update(dt)
  end
end

function Coin:drawAll(  )
  for i,instance in ipairs(ActiveCoins) do
    instance:draw()
  end
end

function Coin.beginContact(a, b, collision)
  for i,instance in ipairs(ActiveCoins) do
    if a == instance.physics.fixture or b == instance.physics.fixture then
      if a == Player.physics.fixture or b == Player.physics.fixture then
        instance.toBeRemoved = true
        return true
      end
    end
  end
end
Krauly
Prole
Posts: 12
Joined: Thu Dec 08, 2022 6:12 pm

Re: How to make a collectible respawn?

Post by Krauly »

Maybe instead of re-using the coin.new func I should make a clone with the difference that phyisics and img gets loaded if instance.timer > 5?
User avatar
knorke
Party member
Posts: 237
Joined: Wed Jul 14, 2010 7:06 pm
Contact:

Re: How to make a collectible respawn?

Post by knorke »

Can you post the complete code? It would be easier to debug if we can run it.
function Coin:checkRemove(st)
Is "st" a typo?
Krauly
Prole
Posts: 12
Joined: Thu Dec 08, 2022 6:12 pm

Re: How to make a collectible respawn?

Post by Krauly »

knorke wrote: Fri Dec 09, 2022 7:19 am Can you post the complete code? It would be easier to debug if we can run it.
function Coin:checkRemove(st)
Is "st" a typo?
Yep fixed it xd
Attachments
ProjectPC.rar
(78.26 KiB) Downloaded 34 times
Last edited by Krauly on Fri Dec 09, 2022 2:36 pm, edited 1 time in total.
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to make a collectible respawn?

Post by darkfrei »

Krauly wrote: Fri Dec 09, 2022 2:12 pm
knorke wrote: Fri Dec 09, 2022 7:19 am Can you post the complete code? It would be easier to debug if we can run it.
function Coin:checkRemove(st)
Is "st" a typo?
Yep fixed it xd
<ProjectPC.rar>
Please don't rar it! Not all people have bought the winrar. But you can zip it or zip it and rename to .love
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Krauly
Prole
Posts: 12
Joined: Thu Dec 08, 2022 6:12 pm

Re: How to make a collectible respawn?

Post by Krauly »

Like this?
Attachments
ProjectPC(2).7z
(65.71 KiB) Downloaded 30 times
ProjectPC(2) - Copy.love
(65.71 KiB) Downloaded 34 times
User avatar
darkfrei
Party member
Posts: 1168
Joined: Sat Feb 08, 2020 11:09 pm

Re: How to make a collectible respawn?

Post by darkfrei »

Krauly wrote: Fri Dec 09, 2022 2:40 pmLike this?
Almost, you are need to pack files, not the folder, zip it, but not 7zip it:
2022-12-09T15_46_12-tech. Auftrag KA-2022-30034760 CP-KFM - Techn. Auf... (RDS.netzwerk.local).png
2022-12-09T15_46_12-tech. Auftrag KA-2022-30034760 CP-KFM - Techn. Auf... (RDS.netzwerk.local).png (45.46 KiB) Viewed 1087 times

And rename from .zip to .love:
2022-12-09T15_46_32-Keyboard Smasher.png
2022-12-09T15_46_32-Keyboard Smasher.png (2.64 KiB) Viewed 1087 times
:awesome: in Lua we Löve
:awesome: Platformer Guide
:awesome: freebies
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 16 guests