************************
Error
RCar.lua:29: attempt to perform arithmetic on field 'y' (a nil value)
Traceback
RCar.lua:29: in function 'collides'
RCar.lua:99: in function 'update'
states/PlayState.lua:13: in function 'update'
StateMachine.lua:59: in function 'update'
main.lua:90: in function 'update'
[C]: in function 'xpcall'
*************************
This is really slowing down my progression on this project so I would love any help I could get.
Here's my code, the issue is in the collides function:
Code: Select all
RCar = Class{}
local count = 0
local groundHeight = 0
function RCar:init()
self.image = love.graphics.newImage('red_car.png')
groundHeight = VIRTUAL_HEIGHT - 37
self.x = 120
self.y = groundHeight
self.width = self.image:getWidth()
self.height = self.image:getHeight()
self.dx = 0
self.dy = 0
self.timer = 0
MOVEMENT_SPEED = 100
GRAVITY = 10
end
function RCar:collides()
if BallY >= self.y + 3 and self.x <= BallX and BallX <= self.x + 3 then --Back trunk hit
BallDy = -5
elseif BallX >= self.x + 3 and self.y + 3 <= BallY <= self.y then --Back window hit
BallDx = -BallDx - 2
elseif BallY <= self.y and self.x + 3 <= BallX <= self.x + 11 then --top of the car
BallDy = -5
elseif BallX <= self.x + 11 and self.y <= BallY <= self.y + 3 then --front window
BallDx = -BallDx + 2
elseif BallY >= self.y + 3 and self.x + 11 <= BallX <= self.x + 16 then --hood
BallDy = -5
elseif BallX <= self.x + 16 and self.y + 3 <= BallY <= self.y + 7 then --bumper
BallDx = -BallDx + 2
elseif BallY <= self.y + 7 and self.x <= BallX <= self.x + 16 then --bottom
BallDy = -BallDy
if BallDx < 0 then
BallDx = -BallDx + 2
elseif BallDx > 0 then
BallDx = BallDx + 2
end
elseif BallX > self.x and self.y + 3 <= BallY <= self.y + 7 then --back bumper
BallDx = -BallDx - 2
end
end
function RCar:update(dt)
if self.y < groundHeight then
self.dy = self.dy + GRAVITY * dt
else
self.dy = 0
end
if love.keyboard.isDown('d') then
self.dx = MOVEMENT_SPEED
self.x = self.x + self.dx * dt
end
if love.keyboard.isDown('a') then
self.dx = -MOVEMENT_SPEED
self.x = self.x + self.dx * dt
end
if love.keyboard.wasPressed('w') and count == 0 then
count = 1
self.dy = -2
end
if count == 1 then
if love.keyboard.isDown('w') then
self.dy = -2
self.timer = self.timer + dt
if self.timer > .5 then
count = 2
self.timer = 0
end
end
end
if self.y >= groundHeight then
self.y = groundHeight
count = 0
end
self.y = self.y + self.dy
RCar:collides()
end
function RCar:render()
love.graphics.draw(self.image, self.x, self.y, 0, 3, 3)
end