2017-10-14 152 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

相關問題