Classes and spawning and things of the sort

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
User avatar
TheOdyssey
Citizen
Posts: 52
Joined: Mon Jul 13, 2015 4:29 pm
Location: Turn Around...

Classes and spawning and things of the sort

Post 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.
Attachments
ethos.love
(2.84 KiB) Downloaded 115 times
User avatar
MadByte
Party member
Posts: 533
Joined: Fri May 03, 2013 6:42 pm
Location: Braunschweig, Germany

Re: Classes and spawning and things of the sort

Post 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
User avatar
s-ol
Party member
Posts: 1077
Joined: Mon Sep 15, 2014 7:41 pm
Location: Cologne, Germany
Contact:

Re: Classes and spawning and things of the sort

Post 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:
Attachments
fixed.love
(2.98 KiB) Downloaded 113 times

s-ol.nu /blog  -  p.s-ol.be /st8.lua  -  g.s-ol.be /gtglg /curcur

Code: Select all

print( type(love) )
if false then
  baby:hurt(me)
end
User avatar
TheOdyssey
Citizen
Posts: 52
Joined: Mon Jul 13, 2015 4:29 pm
Location: Turn Around...

Re: Classes and spawning and things of the sort

Post 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
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot], Semrush [Bot] and 4 guests