为什么不调用这个方法?

时间:2017-10-14 22:38:17

标签: oop lua love2d

我在说MovingGround:update()。它没有崩溃;它只是没有做任何方法。

我在方法中做了其他事情。我将播放器pos设置为100 100,但这没有发生,因此方法本身(可能)没有错误 - 至少不会使它无效。该方法不会被调用!!我认为这个问题几乎就是其余的! Sooooo ....这是代码!

- 理由

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) --links
    grounds[2] = Ground:new(2,2,795,25) --oben
    grounds[3] = Ground:new(772,2,25,595) --rechts
    grounds[4] = Ground:new(2,572,795,25) --unten
    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

2 个答案:

答案 0 :(得分:0)

我真的建议使用调试器来确定此问题的原因。这是一个演练:

  1. update对象上找到调用MovingGround方法的位置,并在那里设置断点。
  2. 在启用调试的情况下运行程序,直到它到达断点。
  3. 进入update方法的调用。你最终采取了什么方法?这是你期待的方法吗?
  4. 现在您已经在update方法内部,请查看self的元表。是metatable MovingGround
  5. metatable设置在MovingGround:new内,因此在MovingGround:new的开头设置断点并重新启动程序。运行直到到达断点。
  6. 前进(使用&#34;跳过&#34;而不是&#34;进入&#34;)直到您看到grd的元表已按照您在步骤4中的设置进行设置。
  7. 看看你刚刚走过的那条线。需要改变什么?
  8. Spoiler:在MovingGround:new中,metatable设置为Ground而不是MovingGround

答案 1 :(得分:0)

您应该将dt传递到任何更新函数中。

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

类似地,在main.lua更新功能中,调用应为grounds[i]:update(dt)