Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
kkeomystic opened this issue Jul 8, 2023 · 0 comments

Comments

@kkeomystic
Copy link

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
[email protected]

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'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant