Skip to content

Error src/states/PlayState.lua:23: attempt to call global 'Ball' (a nil value) #19

@kkeomystic

Description

@kkeomystic

im having trouble with my game because i keep running into the error mentioned in the title, heres my playstate.lua code:
--[[
GD50
Breakout Remake

-- PlayState Class --

Author: Colton Ogden
cogden@cs50.harvard.edu

Represents the state of the game in which we are actively playing;
player should control the paddle, with the ball actively bouncing between
the bricks, walls, and the paddle. If the ball goes below the paddle, then
the player should lose one point of health and be taken either to the Game
Over screen if at 0 health or the Serve screen otherwise.

]]

PlayState = Class{__includes = BaseState}

function PlayState:init()
self.paddle = Paddle()

-- initialize ball with skin #1; different skins = different sprites

23- self.ball = Ball(1) -- this is where the error occurs

-- give ball random starting velocity
self.ball.dx = math.random(-200, 200)
self.ball.dy = math.random(-50, -60)

-- give ball position in the center
self.ball.x = VIRTUAL_WIDTH / 2 - 4
self.ball.y = VIRTUAL_HEIGHT - 42

-- use the "static" createMap function to generate a bricks table
self.bricks = LevelMaker.createMap()

end

function PlayState:update(dt)
if self.paused then
if love.keyboard.wasPressed('space') then
self.paused = false
gSounds['pause']:play()
else
return
end
elseif love.keyboard.wasPressed('space') then
self.paused = true
gSounds['pause']:play()
return
end

-- update positions based on velocity
self.paddle:update(dt)
self.ball:update(dt)

if self.ball:collides(self.paddle) then
    -- reverse Y velocity if collision detected between paddle and ball
    self.ball.dy = -self.ball.dy
    gSounds['paddle-hit']:play()
end

-- detect collision across all bricks with the ball
for k, brick in pairs(self.bricks) do

    -- only check collision if we're in play
    if brick.inPlay and self.ball:collides(brick) then

        -- trigger the brick's hit function, which removes it from play
        brick:hit()
    end
end

if love.keyboard.wasPressed('escape') then
    love.event.quit()
end

end

function PlayState:render()
-- render bricks
for k, brick in pairs(self.bricks) do
brick:render()
end

self.paddle:render()
self.ball:render()

-- pause text, if paused
if self.paused then
    love.graphics.setFont(gFonts['large'])
    love.graphics.printf("PAUSED", 0, VIRTUAL_HEIGHT / 2 - 16, VIRTUAL_WIDTH, 'center')
end

end

any help would be appreciated, heres the traceback error too jic:

Error

src/states/PlayState.lua:23: attempt to call global 'Ball' (a nil value)

Traceback

[love "callbacks.lua"]:228: in function 'handler'
src/states/PlayState.lua:23: in function 'init'
lib/class.lua:79: in function <lib/class.lua:77>
src/StateMachine.lua:17: in function 'change'
src/states/StartState.lua:35: in function 'update'
src/StateMachine.lua:22: in function 'update'
main.lua:141: in function 'update'
[love "callbacks.lua"]:162: in function <[love "callbacks.lua"]:144>
[C]: in function 'xpcall'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions