2017-10-14 151 views
1

我在说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 

回答

0

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

  1. 查找其中update方法被称为MovingGround对象上设置一个断点处。
  2. 运行您的程序启用调试,直到它到达断点。
  3. 步骤调用update方法。你最终会采取什么方法?这是你期待的方法吗?
  4. 现在你已经进入了update方法,看看self的metatable。是metatable MovingGround
  5. metatable设置在MovingGround:new的内部,所以在MovingGround:new的开头设置一个断点并重新启动程序。运行直至达到断点。
  6. 步骤前进(使用“步过”,而不是“跨进”),直到你看到的grd元表已经被设置为您在步骤4看到
  7. 看你刚刚跨过了线。什么需要改变?

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

相关问题