Simple Educative Class Library: Help creating instance

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
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Simple Educative Class Library: Help creating instance

Post by Shadowblitz16 »

can someone tell me why I keep getting a error of: "main.lua:6: attempt to call method 'new' (a nil value)"

Code: Select all

require "bullet"

love.window.setMode(480, 576)
love.graphics.setDefaultFilter('nearest', 'nearest')

bullet = Bullet:new()

function love.load()


	--player:load()
	--player.x = 86
	----player.y = 182
end

function love.update()
	--player:update(dt)
end

function love.draw()
	love.graphics.scale(3, 3)
	
	bullet:draw()

	--player:draw()
end

Code: Select all

require "class"

	  Bullet          = class:new()
	  Bullet          = {}                                                        -- Doesn't really do anything just there incase
	  Bullet.x        = 0                                                         -- Set bullet x
	  Bullet.y        = 0                                                         -- Set bullet y
	  Bullet.gfx      = {}                                                        -- Set bullet gfx
      Bullet.sfx      = {}                                                        -- Set bullet sfx
	  Bullet.speed    = 2                                                         -- Set bullet speed
	  Bullet.width    = 10                                                        -- Set bullet width
	  Bullet.height   = 10                                                        -- Set bullet height

-- Helper functions
function toNumber(x)
	if     (x == false) then return 0
	elseif (x == true ) then return 1
	else   				 	 return nil
	end
end

-- Resource functions
function Bullet:loadGfx(file, xOffset, yOffset)
	local sprite         = {}
		  sprite.image   = love.graphics.newImage(file)
		  sprite.xOffset = xOffset
		  sprite.yOffset = yOffset
	table.insert(self.gfx, sprite)
end
function Bullet:loadSfx(file)
	local sound   = love.audio.newSource(file)
	table.insert(self.sfx, sound)
end 
 
	  
function Bullet:update(dt)
	if (self.y <= -self.width) then bullet = nil end                              -- Check if bullet is otuside screen and if so then delete it
	self.y =  self.y - speed                                                               -- Incerment y upwards
end
function Bullet:draw()
	love.graphics.setColor(255, 255, 255)                                         -- Set draw color
	for _, g in pairs(self.gfx) do                                                -- Loop though bullet graphics
		love.graphics.draw(g.image, self.x + g.xOffset, self.y + g.yOffset)       -- Draw them
	end
end
pedrosgali
Party member
Posts: 107
Joined: Wed Oct 15, 2014 5:00 pm
Location: Yorkshire, England

Re: Simple Educative Class Library: Help creating instance

Post by pedrosgali »

Try

Code: Select all

Bullet = require 'bullet'

Code: Select all

if not wearTheseGlasses() then
  chewing_on_trashcan = true
end
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: Simple Educative Class Library: Help creating instance

Post by Shadowblitz16 »

it still doesn't work

Code: Select all

Bullet = require "bullet"

love.window.setMode(480, 576)
love.graphics.setDefaultFilter('nearest', 'nearest')

bullet = Bullet:new()

function love.load()


	--player:load()
	--player.x = 86
	----player.y = 182
end

function love.update()
	--player:update(dt)
end

function love.draw()
	love.graphics.scale(3, 3)
	
	bullet:draw()

	--player:draw()
end

Code: Select all

require "class"

	  Bullet          = class:new()
	  Bullet          = {}                                                        -- Doesn't really do anything just there incase
	  Bullet.x        = 0                                                         -- Set bullet x
	  Bullet.y        = 0                                                         -- Set bullet y
	  Bullet.gfx      = {}                                                        -- Set bullet gfx
	  Bullet.sfx      = {}                                                        -- Set bullet sfx
	  Bullet.speed    = 2                                                         -- Set bullet speed
	  Bullet.width    = 10                                                        -- Set bullet width
	  Bullet.height   = 10                                                        -- Set bullet height

-- Helper functions
function toNumber(x)
	if     (x == false) then return 0
	elseif (x == true ) then return 1
	else   				 	 return nil
	end
end

-- Resource functions
function Bullet:loadGfx(file, xOffset, yOffset)
	local sprite         = {}
		  sprite.image   = love.graphics.newImage(file)
		  sprite.xOffset = xOffset
		  sprite.yOffset = yOffset
	table.insert(self.gfx, sprite)
end
function Bullet:loadSfx(file)
	local sound   = love.audio.newSource(file)
	table.insert(self.sfx, sound)
end 
 
	  
function Bullet:update(dt)
	if (self.y <= -self.width) then bullet = nil end                              -- Check if bullet is otuside screen and if so then delete it
	self.y =  self.y - speed                                                               -- Incerment y upwards
end
function Bullet:draw()
	love.graphics.setColor(255, 255, 255)                                         -- Set draw color
	for _, g in pairs(self.gfx) do                                                -- Loop though bullet graphics
		love.graphics.draw(g.image, self.x + g.xOffset, self.y + g.yOffset)       -- Draw them
	end
end

return Bullet
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Simple Educative Class Library: Help creating instance

Post by bartbes »

It's because you set Bullet to {} after setting it to the result of class:new(). So instead of Bullet being a class, it is instead just a plain table, without a new function.
Shadowblitz16
Citizen
Posts: 73
Joined: Wed Oct 28, 2015 11:18 pm

Re: Simple Educative Class Library: Help creating instance

Post by Shadowblitz16 »

I just made my own class.lua by following a video tutorial

I'm pretty sure it doesn't have as much features but I like it and it works
thankyou for your help hopefully someone using that library that needs help with see this
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 52 guests