为什么这个方法没有被调用?

我指的是 MovingGround:update()。它不会崩溃,只是它没有执行方法中的任何内容。

我在方法中做了其他事情。我把玩家位置设置为 100 100,但是这并没有发生,所以方法本身可能没有错误,至少没有导致它什么都不做的错误。方法只是没有被调用!我想这个问题的答案已经很明显了!嗯....下面是代码!

-- 地面

Ground = {}
Ground.__index = Ground

function Ground:new(x,y,width,height)
    grd = {}
    setmetatable(grd, Ground)
    grd.x = x
    grd.y = y
    grd.w = width
    grd.h = height
    grd.moving = false
    return grd
end

function Ground:draw(r,g,b)
    love.graphics.setColor(r,g,b)
    love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
end

function Ground:update()
end

MovingGround = {}
MovingGround.__index = MovingGround

function MovingGround:new(x,y,w,h,spdx,spdy,stepsx,stepsy)
    grd = {}
    setmetatable(grd, Ground)
    grd.x = x
    grd.y = y
    grd.w = w
    grd.h = h
    grd.moving = true
    grd.spdx = spdx
    grd.spdy = spdy
    grd.stepsxmax = stepsx
    grd.stepsymax = stepsy
    grd.stepsx = 0
    grd.stepsy = 0
    grd.xdir = 1
    grd.ydir = 1
    return grd
end

function MovingGround:draw(r,g,b)
    love.graphics.setColor(r,g,b)
    love.graphics.rectangle("line",self.x,self.y,self.w,self.h)
    love.graphics.rectangle("fill",300,self.y,self.w,self.h)
end

function MovingGround:update()
    if self.stepsx > self.stepsxmax or self.stepsx < 0 then self.spdx = -self.spdx self.dirx = -self.dirx end
    if self.stepsy > self.stepsymax or self.stepsy < 0 then self.spdy = -self.spdy self.diry = -self.diry end
    self.x = self.x + self.spdx
    self.y = self.y + self.spdy
    self.stepsx = self.stepsx + self.dirx
    self.stepsy = self.stepsy + self.diry
end

-- 主函数

require"functions"
require"player"
require"grounds"

grounds= {}
width = love.graphics.getWidth()
height = love.graphics.getHeight()

function love.load()
    test = Player:new(100,100,"w","a","s","d")
    grounds[5] = Ground:new(2,2,25,595) --左侧
    grounds[2] = Ground:new(2,2,795,25) --上方
    grounds[3] = Ground:new(772,2,25,595) --右侧
    grounds[4] = Ground:new(2,572,795,25) --下方
    grounds[1] = MovingGround:new(50,400,100,20,0,3,0,15)
end

function love.draw()
    test:draw(255,0,255)
    love.graphics.print("y : " .. tostring(test.y),10,10)
    love.graphics.print("x : " .. tostring(test.x),10,30)
    love.graphics.print(test.spdy,10,60)
    love.graphics.print(gd,10,90)
    love.graphics.print(1000 / gd,10,150)
    love.graphics.print(boolToString(test.onGround),10,120)
    love.graphics.print(grounds[1].stepsy,10,210)
    for i,v in ipairs(grounds) do
        grounds[i]:draw(255,255,255)
    end
end

function love.update(d)
    gd = d
    test:update(d)
    for i,v in ipairs(grounds) do
        grounds[i]:update()
    end
end

function love.keypressed(key,code)
    if key == "space" then test:jump(-700) end
end
点赞
用户1108505
用户1108505

我真的建议使用调试器来确定这个问题的原因。以下是一步步指南:

  1. 找到在 MovingGround 对象上调用 update 方法的位置,并在那里设置断点。
  2. 启用调试功能运行您的程序,直到它到达断点位置。
  3. 进入 update 方法的调用。你会被带到哪个方法?它是你期望的方法吗?
  4. 现在你在 update 方法内部,请查看 self 的元表。元表是 MovingGround 吗?
  5. 元表是在 MovingGround:new 内部设置的,所以在 MovingGround:new 的开头设置断点并重新启动程序。运行直到它到达断点。
  6. 向前步进(使用“步进”而不是“步入”),直到看到 grd 的元表已经像第四步中看到的那样设置。
  7. 查看刚刚跨越的那一行。需要更改什么?

剧透:在 MovingGround:new 中,元表被设置为 Ground 而不是 MovingGround

2017-10-15 03:56:23
用户12230942
用户12230942

你应该将 dt 传递给任何更新函数中。

在这种情况下,函数应该是 MovingGround:update(dt) (ground.lua),以便它在每一帧更新。

同样,在 main.lua 的更新函数中,调用应该是 grounds[i]:update(dt)

2020-02-26 06:04:14