【问题标题】:I am trying to create an object AllBalls in my Playstate but when ever i try to initialize the object i get an error我正在尝试在我的 Playstate 中创建一个对象 AllBalls 但是当我尝试初始化该对象时出现错误
【发布时间】:2020-10-29 05:56:27
【问题描述】:

在我的游戏雕像中,如果我的对象是这样的,我会创建一个实例

function PlayState:enter(params)
    self.paddle = params.paddle
    self.bricks = params.bricks
    self.health = params.health
    self.score = params.score
    self.highScores = params.highScores
    self.level = params.level

    self.recoverPoints = 5000
    self.extendPoints = 7000

    -- give ball random starting velocity

    self.inPlayBalls= AllBalls(params.ball)
    self.inPlayBalls[1].dx = math.random(-200, 200)
    self.inPlayBalls[1].dy = math.random(-50, -60)
end

AllBalls 类是一个简单的类

AllBalls = Class{}

function AllBalls:init(p)
    self.ball=p
    self.inPlayBalls={self.ball}
end

function AllBalls:update(dt,target)
    for k, ball in pairs(self.inPlayBalls) do
        ball:update(dt)
        if ball:collides(target) then
            -- raise ball above paddle in case it goes below it, then reverse dy
            ball.y = target.y - 8
            ball.dy = -target.dy
    
            --
            -- tweak angle of bounce based on where it hits the paddle
            --
    
            -- if we hit the paddle on its left side while moving left...
            if ball.x < target.x + (target.width / 2) and target.dx < 0 then
                ball.dx = -50 + -(8 * (target.x + target.width / 2 - ball.x))
            
            -- else if we hit the paddle on its right side while moving right...
            elseif ball.x > target.x + (target.width / 2) and target.dx > 0 then
                ball.dx = 50 + (8 * math.abs(target.x + target.width / 2 - ball.x))
            end
    
            gSounds['paddle-hit']:play()
        end

        if math.abs(ball.dy) < 150 then
            ball.dy = ball.dy * 1.02
        end
        if ball.remove then
            table.remove(self.AllBalls, k)
        end
    end
    
end

function AllBalls:collides(target)
    for k, ball in pairs(self.inPlayBalls) do
        if ball:collides(target) then
            if ball.x + 2 < brick.x and ball.dx > 0 then
                
                -- flip x velocity and reset position outside of brick
                ball.dx = -ball.dx
                ball.x = brick.x - 8
            
            -- right edge; only check if we're moving left, , and offset the check by a couple of pixels
            -- so that flush corner hits register as Y flips, not X flips
            elseif ball.x + 6 > brick.x + brick.width and ball.dx < 0 then
                
                -- flip x velocity and reset position outside of brick
                ball.dx = -ball.dx
                ball.x = brick.x + 32
            
            -- top edge if no X collisions, always check
            elseif ball.y < brick.y then
                
                -- flip y velocity and reset position outside of brick
                ball.dy = -ball.dy
                ball.y = brick.y - 8
            
            -- bottom edge if no X collisions or top collision, last possibility
            else
                
                -- flip y velocity and reset position outside of brick
                ball.dy = -ball.dy
                ball.y = brick.y + 16
            end
        end
    end
end

function AllBalls:add(ball)
    local newBall=Ball(ball.skin)
    newBall.x=ball.x
    newBall.dx=math.random(-200,200)
    newBall.y=ball.y
    newBall.dy=ball.dy

    table.insert(self.inPlayBalls, newBall)
end

function AllBalls:out()
    for k, ball in pairs(self.inPlayBalls) do
        if ball.y >= VIRTUAL_HEIGHT then
            ball.remove=true
        end
    end
end

当我进入播放状态时,我会收到以下错误

src/states/PlayStatue.lua:37 尝试索引 nil 值

作为一个整体,我是 Lua 的新手,我可能犯了一个我找不到的愚蠢错误,所以会得到任何帮助

【问题讨论】:

  • 在之前的状态中,我确实在球中传递了一个非零值
  • 你用的是什么游戏引擎?
  • 我正在使用love2d 0.10.2
  • 您的主文件中的 load.load()、love.update(dt) 和 love.draw() 函数是什么样的?
  • 如果对github.com/KiwiFace99/StartingLua987654321@ 有帮助,则将整个代码上传到以下存储库中

标签: lua game-development love2d


【解决方案1】:

问题可能来自这个作业

self.inPlayBalls= AllBalls(params.ball)

通过查看您的 GitHub,inPlayBallsAllBalls 实例的属性。这意味着您必须先创建 AllBalls 实例,然后获取属性以将其分配给变量,如下所示:

self.allBalls = AllBalls(params.ball)
self.inPlayBalls = self.allBalls.inPlayBalls

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    • 1970-01-01
    • 2012-05-14
    • 2015-10-06
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    相关资源
    最近更新 更多