Page 1 of 1

Classes and spawning and things of the sort

Posted: Sun Dec 06, 2015 2:45 pm
by TheOdyssey
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.

Re: Classes and spawning and things of the sort

Posted: Sun Dec 06, 2015 3:35 pm
by MadByte

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

Posted: Sun Dec 06, 2015 3:54 pm
by s-ol
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
I fixed all of these and attached a version that does what I think you want it to:

Re: Classes and spawning and things of the sort

Posted: Mon Dec 07, 2015 2:14 am
by TheOdyssey
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
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.
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:
  • 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
I fixed all of these and attached a version that does what I think you want it to:
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!
EDIT: nevermind, I got it literally seconds after posting this :3