I'm quite new to programming in LÖVE 2D and Lua and I started coding my first game on it. Everything was running smoothly until I added collision to my game. It now gives me this error: Player.lua:13: attempt to compare number with nil.
I know I somehow made a stupid mistake but I'm just not sure. Here is the code:
Player = Class{}
function Player:init(y,dy)
self.x = 640
self.y = wHeight/2
self.dy = dy
self.image = love.graphics.newImage('SpaceShip.png')
self.width = 40
self.height = 10
end
function Player:collide(paddle)
if self.x > paddle.x + 100 or paddle.x > self.x + 40 then
return false
end
if self.y > paddle.y + 100 or paddle.y > self.y + 10 then
return false
end
return true
end
function Player:update()
self.y = self.y + self.dy
if (player.y >= wHeight) then
player.y = wHeight - 15
end
if (player.y <= 0) then
player.y = 0
end
end
function Player:render()
love.graphics.draw(self.image,wWidth/2,self.y,0,2.5,2.5)
end
I have used the collision in some other code and has work perfectly. But I have been stuck for many hours.
Thanks!
Attachments
Screen Shot 2019-08-07 at 9.19.50 PM.png (59.7 KiB) Viewed 9054 times
How are you calling Player:collide? Seems like the issue is there; maybe you're calling it with a dot (.) and neglected to pass itself as the first parameter, or you are calling it with a colon (:) and neglected to pass the paddle table into it.
Me and my stuff True Neutral Aspirant. Why, yes, i do indeed enjoy sarcastically correcting others when they make the most blatant of spelling mistakes. No bullying or trolling the innocent tho.
Class = require "Class"
require "Player"
require "Asteroid"
FORCE = 0.2
BACKGROUND = love.graphics.newImage("background.png")
local SCROLL_SPEED = 5
local SCROLL = 1940
function love.load()
love.window.setTitle("Mid-G")
love.graphics.setDefaultFilter("nearest", "nearest")
wWidth = 1280
wHeight = 720
love.window.setMode(wWidth, wHeight)
player = Player(wHeight/2,0.1)
asteroid1 = Asteroid()
end
function love.update()
player:update()
asteroid1:update()
if Player:collide(asteroid1) then
love.event.quit()
end
asteroid1:reset()
SCROLL = (SCROLL + SCROLL_SPEED)%640
if love.keyboard.isDown("escape") then
love.event.quit()
end
if love.keyboard.isDown("space") then
if player.y >= wHeight/2 + 5 then
player.dy = player.dy + FORCE
else
player.dy = player.dy - FORCE
end
else
if player.y >= wHeight/2 then
player.dy = player.dy - FORCE
else
player.dy = player.dy + FORCE
end
end
end
function love.draw()
love.graphics.draw(BACKGROUND,-SCROLL,0)
asteroid1:render()
player:render()
end
function debug(log)
love.graphics.print(log)
end
Also, the reason why it's called paddle in Player.lua and asteroid in main.lua is because I used the collision code from another project, and when I changed the paddle to asteroid, I thought that was the problem, so I left it as it is.
I set the function to love.event.quit() just so I know that the collision works.
The error is not "attempting to index a nil value". The error is "attempting to compare number with nil".
One of the objects doesn't have an x or an y field.
The problem is most likely in Asteroid.lua, but rather than guessing. it would be nice if you post the whole project instead of the snippets where you believe the problem is.
Asteroid = Class{}
function Asteroid:init()
self.x = 1280
self.y = love.math.random(0,wHeight)
self.dx = 0
self.image = love.graphics.newImage("Asteroid.png")
self.dx = love.math.random(5,100)
self.width = self.image:getWidth()
self.height = self.image:getHeight()
end
function Asteroid:reset()
if self.x <= 0 then
self.x = 1280
self.y = love.math.random(0,wHeight)
self.dx = love.math.random(10,20)
end
end
function Asteroid:update()
self.x = self.x - self.dx
end
function Asteroid:render()
love.graphics.draw(self.image,self.x,self.y)
end
I don't have any more code and I haven't left anything else out.
But that's not the right way to call it. The object is called player, the class is called Player. You're passing the class to the function instead of the object. The class doesn't contain x or y, hence the error.