function Coin:update(dt)
for i,v in ipairs(Coin) do
v.x = v.x -- v is coin
v.y = v.y + .5
v.angle = v.angle + math.pi * dt
-- Starting here is where I need help...
if v.y + Coin.model:getHeight() > 500 then
table.remove( v, id )
end
if v.x + Coin.model:getHeight() >= player.x and v.x <= player.x + player.model:getWidth() and v.y + Coin.model:getHeight() >= player.y and v.y <= player.y + player.model:getHeight() then
table.remove(v,id)
cc = cc + 1
end
end
end
Okay so this function pretty much updates the coin as it falls down toward the player. My Issue is that I can't seem to remove it when either the player touches it or if it gets out of bounds. My other issue is cc = cc + 1. Which is the total amount of coins collected. However, whenever the player comes in contact with the falling coin instead of adding 1 coin it seems to add the amount of time the two were in contact. Help would be greatly appreciated , Thank you.
function Coin:update(dt)
self.x = self.x -- v is coin
self.y = self.y + .5
self.angle = self.angle + math.pi * dt
if self.y + Coin.model:getHeight() > 500 then
self.remove = true
end
if self.x + Coin.model:getHeight() >= player.x and
self.x <= player.self + player.model:getWidth() and
self.y + Coin.model:getHeight() >= player.y and
self.y <= player.y + player.model:getHeight() then
self.remove = true
cc = cc + 1
end
end
-- somewhere inside of your update loop
for i=#coins, 1, -1 do
local v = coins[i]
if v.remove then
table.remove(coins, i)
end
-- other update code...
end
"coins" should be your coins list. Also, on the loop, we are looping backwards so the loop doesn't do any funky stuff when we remove the coins.
xFade wrote:What if I wasn't using a colon operator?
Use a table for each coin object, and store those coins in a coins table. Then loop over (preferably backwards, if you plan to remove some coins) and update each coin.
entitySample = {}
function entitySample:create(v)
local _i = {}
setmetatable(_i, {__index = self})
_i:setup(v)
return _i
end
function entitySample:setup(v)
self.x = v.x or 0
self.y = v.y or 0
self.spinFrame = 0
self.whatever = v.whatever or defaultValue
...
end
function entitySample:update(dt)
self.spinFrame = self.spinFrame + dt * self.spinSpeed
if self.spinFrame > self.frameCount then self.spinFrame = 0 end
...
end
function entitySample:draw()
love.graphics.draw(self.image, self.quad[math.floor(self.spinFrame)], self.x, self.y)
...
end
Of course this is just a sample, all you ned to know is the function creation stuff. You'd then create a new "coin" by calling coinEntity[#coinEntity+1] = entitySample:create {x = 100, y = 100} whenever you want to create one. And loop through all your entities and call their update functions in update and their draw functions in draw.
Someone else can elaborate but this is the gist of creating an "object" in Lua/Löve.
Taco = {}
ti = 0
tc = 0
tacopic = love.graphics.newImage("Index/taco.png")
function Taco:create(v)
local _i = {}
setmetatable(_i, {__index = self})
_i:setup(v)
return _i
end
function Taco:setup(v)
self.x = v.x or 0
self.y = v.y or 0
self.angle = 0
--self.whatever = v.whatever or defaultValue
end
function Taco:update(dt)
self.angle = self.angle + math.pi * dt
end
function Taco:draw()
love.graphics.draw(tacopic,self.angle, self.x, self.y)
end
timer1 = 0
function Taco_rain(dt)
timer1 = timer1 + dt
if timer1 >= .5 then
x = math.random(50,690)
sTaco[#sTaco+1] = Taco:create {x = x, y = 0}
timer1 = 0
ti = ti + 1
end
end
So um I keep getting an error "attempt to perform arithmetic on field angle" a nil value... :/