2016-07-10 2 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 
    } 

мне кажется, что я действительно дают «а» значение - таблица, содержащая набор изображений, которым я назначить через второй Parametr.Lua. Попытка глобальный индекс «а» (нилъ значение)

----- 

    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") 

здесь я якобы дать 'а' значение

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() 

Sorry Ф.О. мой бедный английский и заранее спасибо

ответ

0

a вы поиск является частью таблицы y ou возвращаются из функции new. Это не видно из объема этих функций.

простой решение перенести значения, которые вы поместили в таблице вверх в области видимости функции new, герметизирующего их. Однако это удаляет их из таблицы, поэтому они не будут видны за пределами new, .play, .pause, .draw, .update или .set_animation.

Лучшим решением является использование более типичного решения ООП, поэтому я бы предложил прочитать главу 16 «Программирование в Луа»: Object-oriented Programming. После того, как вы прочтете эту главу, прочитайте все, чтобы быть в безопасности (особенно 5, 6 и 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 
} 
Смежные вопросы