2017-10-14 13 views
1

私はMovingGround:update()について話しています。それはクラッシュしません。それは方法にある何もしません。このメソッドが呼び出されないのはなぜですか?

私はこの方法で他のことをしました。私はプレーヤーのポジションを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はMovingGroundですか?
  5. MovingGround:newの内部にmetatableが設定されているため、MovingGround:newの先頭にブレークポイントを設定してプログラムを再起動してください。ブレークポイントに達するまで実行します。
  6. ステップ4で見たように、grdのメタテーブルが設定されるまで、ステップを進めます(「ステップオーバー」ではなく「ステップイン」)。
  7. ステップオーバーした行を見てください。何を変更する必要がありますか?

スポイラー:MovingGround:newに、メタテーブルがGround代わりのMovingGroundに設定なっています。

関連する問題