I'm getting the following error:
Attempt to index global "a" (a nil value)
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
}
it seems to me, that I do give "a" a value - a table, containing a set of images, which I assign through second parameter.
-----
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")
Here I supposedly give 'a' a value
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()
The a
you are looking for is part of the table you are returning from the new
function. It is not visible from the scope of those functions.
The easiest solution is to move the values you've placed in the table up in the scope of the new
function, encapsulating them. However, this removes them from the table, so they won't be visible outside of the new
, .play
, .pause
, .draw
, .update
, or .set_animation
functions.
A better solution is to employ a more typical OOP solution, so I would suggest reading chapter 16 of Programming in Lua: Object-oriented Programming . Once you've read that chapter, go read every other just to be safe (particularly 5, 6, and 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
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.