2016-07-10 23 views
-1
local gfx = love.graphics 
    return{ 
     new = function(Image, Animation, Time)   
     return{ 
     current_frame = 1, 
     current_anim = 1, 
     image = Image, 
     a = Animation, 
     play = false, 
     time = Time or 0,2, 
     counter = 0, 

     update = function(dt) 
     if play then 
        counter = counter + dt 
        if counter >= time then 
         counter = 0 
         current_frame = current_frame + 1 
        end 

        if current_frame > #a[current_anim] then 
         current_frame = 1 
        end 
       else 

       end 
     end, 

     play = function() 
     play = true 
     end, 

     stop = function() 
     play = false  
     end, 

     set_animation = function(anim) 
     if anim > #a then error("there is no animation ", anim) return end 
     current_anim = anim 

     end, 

     draw = function(data) 
     gfx.draw(image, a[current_anim][current_frame], data[1], data[2]) 
     end 

     } 
     end 
    } 

scheint es mir, dass ich geben „a“ einen Wert - eine Tabelle, eine Reihe von Bildern enthalten, welche ich durch die zweite parametr zuweisen.Lua. Versuch Index global „a“ (ein Null-Wert)

----- 

    local quad = love.graphics.newQuad 
    local anim_data = { 
     quad(0,0, 32, 48, 192, 256), 
     quad(32,0, 32, 48, 192, 256), 
     quad(64,0, 32, 48, 192, 256), 
     quad(96,0, 32, 48, 192, 256), 
     quad(129,0, 32, 48, 192, 256), 
     quad(162,0, 32, 48, 192, 256) 

    } 
    local image = love.graphics.newImage("images/player_animation.png") 
    image:setFilter("nearest","nearest") 

hier i angeblich geben 'a' einen Wert

animation = require("animations"):new(
      image, 

      { 
       { -- idle 
       anim_data[1] 
       }, 
       { -- walk 
       anim_data[2], 
       anim_data[3], 
       anim_data[4], 
       anim_data[5], 
       anim_data[6] 
       }, 
      }, 

      0.2 
     ) 

    animation.play() 

Leider fo mein schlechtes Englisch und Dank im Voraus

Antwort

0

Die a Sie sind Suchen ist Teil des Tisches y Sie kommen von der new Funktion zurück. Es ist nicht sichtbar im Rahmen dieser Funktionen.

Die einfachste Lösung ist es, die Werte bewegen Sie in der Tabelle nach oben in den Rahmen der new Funktion platziert haben, Einkapseln sie. Dies entfernt sie jedoch aus der Tabelle, so dass sie außerhalb der Funktionen new, , .draw, .update oder .set_animation nicht sichtbar sind.

Eine bessere Lösung ist die Verwendung einer typischen OOP-Lösung, daher empfehle ich Kapitel 16 der Programmierung in Lua: Object-oriented Programming. Sobald du dieses Kapitel gelesen hast, lies alle anderen, um sicher zu gehen (besonders 5, 6 und 11).

local gfx = love.graphics 

return { 
    new = function (Image, Animation, Time)   
     local current_frame = 1 
     local current_anim = 1 
     local image = Image 
     local a = Animation 
     local play = false 
     local time = Time or 0,2 
     local counter = 0 

     return { 
      update = function (dt) 
       if play then 
        counter = counter + dt 
        if counter >= time then 
         counter = 0 
         current_frame = current_frame + 1 
        end 

        if current_frame > #a[current_anim] then 
         current_frame = 1 
        end 
       end 
      end, 

      play = function() 
       play = true 
      end, 

      stop = function() 
       play = false 
      end, 

      set_animation = function (anim) 
       if anim > #a then 
        error("there is no animation ", anim) 
        return 
       end 

       current_anim = anim 

      end, 

      draw = function (data) 
       gfx.draw(image, a[current_anim][current_frame], data[1], data[2]) 
      end 
     } 
    end 
}