Classes and spawning and things of the sort
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
- TheOdyssey
- Citizen
- Posts: 52
- Joined: Mon Jul 13, 2015 4:29 pm
- Location: Turn Around...
Classes and spawning and things of the sort
Ok, I really don't think I'm quite getting the concept of spawning enemies or pickups or drops. Because all of the ways I have tried it so far, it either can't spawn more than one at a time or can't spawn anything at all. Can someone help me with this please? I added a .love file to show what I'm talking about.
- Attachments
-
- ethos.love
- (2.84 KiB) Downloaded 116 times
Re: Classes and spawning and things of the sort
Code: Select all
-- HEART ---------------------------------------------------------------------------------
-- Create an Object --
Class = require("middleclass")
Heart = Class("Heart")
function Heart:initialize(x, y)
self.x = x or 0
self.y = y or 0
self.width = 16
self.height = 16
return self
end
function Heart:draw()
love.graphics.rectangle("fill", self.x , self.y, self.width, self.height)
end
-- MAIN ---------------------------------------------------------------------------------
-- Create a table to hold instances of your object --
local container = {}
-- Draw all instances --
function love.draw()
for k, heart in pairs(container) do
heart:draw()
end
end
-- Create an instance and put it into the table --
function love.mousepressed(x, y, button)
if button == "l" then
container[#container+1] = Heart(x, y)
end
-- Remove the last instance from the table --
if button == "r" then
table.remove(container, #container)
end
end
-- Close the window --
function love.keypressed(key)
if key == "escape" then love.event.quit() end
end
Re: Classes and spawning and things of the sort
There's a bunch of things you seem to have misunderstood. You should probably read the OOP chapter of PIL (again?). Some things you missed:
- the usage and relevance of the "self" parameter
- you need to call your init function "initialize" and use heart:new to create a new instance
- you don't need to randomly return "heart"
- you need to keep a list of instances if you want multiple
- you shouldn't re-instantiate the object every frame
- Attachments
-
- fixed.love
- (2.98 KiB) Downloaded 113 times
- TheOdyssey
- Citizen
- Posts: 52
- Joined: Mon Jul 13, 2015 4:29 pm
- Location: Turn Around...
Re: Classes and spawning and things of the sort
Ok, I got this part working, now how would I go about adding physics? I tried function Heart:physics() but when I do any variable starting with self it throws an error.MadByte wrote:Code: Select all
-- HEART --------------------------------------------------------------------------------- -- Create an Object -- Class = require("middleclass") Heart = Class("Heart") function Heart:initialize(x, y) self.x = x or 0 self.y = y or 0 self.width = 16 self.height = 16 return self end function Heart:draw() love.graphics.rectangle("fill", self.x , self.y, self.width, self.height) end -- MAIN --------------------------------------------------------------------------------- -- Create a table to hold instances of your object -- local container = {} -- Draw all instances -- function love.draw() for k, heart in pairs(container) do heart:draw() end end -- Create an instance and put it into the table -- function love.mousepressed(x, y, button) if button == "l" then container[#container+1] = Heart(x, y) end -- Remove the last instance from the table -- if button == "r" then table.remove(container, #container) end end -- Close the window -- function love.keypressed(key) if key == "escape" then love.event.quit() end end
I did read that, multiple times(I do that before posting), and what I got from it was the code you saw. But thanks for the fix!S0lll0s wrote:There's a bunch of things you seem to have misunderstood. You should probably read the OOP chapter of PIL (again?). Some things you missed:
I fixed all of these and attached a version that does what I think you want it to:
- the usage and relevance of the "self" parameter
- you need to call your init function "initialize" and use heart:new to create a new instance
- you don't need to randomly return "heart"
- you need to keep a list of instances if you want multiple
- you shouldn't re-instantiate the object every frame
EDIT: nevermind, I got it literally seconds after posting this
Who is online
Users browsing this forum: Amazon [Bot], Google [Bot] and 9 guests