Hi!
edit (spoiler / if you don’t want to read the wall of text ):
there is no blocking issue anymore, but 2 questions
I just wanted to “manually” control how the animation frames at played, at my own pace, so I could speed up or slow it down the animation in runtime (ex: when the character himself slows down)
=> originally I just wanted to change the animation FPS in runtime, but it was not possible.
It’s probably not the most “elegant” way to do so (I don’t know… see code below if you want to take a look), but it works (and as said above, “I know why”, which is super important to me at this point)
My previous message said that “offset = 0” and “offset = 1” were targeting the same animation frame (Question: is it normal?), so I had to “exclude” offset =1 and consider than my max offset was no longer 1 but [1/(nb_frames_anim)*(nb_frames_anim-1)] … and it works.
Note: I don’t use playerback_rate to control the animation speed, and I ignore the FPS property that I wanted originally change.
Actually I think there is no blocking issue anymore.
I would have another question though: is there a way to get (in code) the number of frames of a given animation? (this way I wouldn’t have to manually update the dedicated local variables when adding a frame to an animation)
--- Anim "local" variables
local timer_max_anim_idle = 0.15
local timer_max_anim_walk_up = 0.1
local timer_max_anim_walk_down = timer_max_anim_walk_up
local timer_max_anim_walk_left = timer_max_anim_walk_up
local timer_max_anim_walk_right = timer_max_anim_walk_up
local nb_frames_anim_idle = 5
local nb_frames_anim_walk_up = 6
local nb_frames_anim_walk_down = 6
local nb_frames_anim_walk_left = 6
local nb_frames_anim_walk_right = 6
local play_properties = { -- char ation properties
offset = 0.0, -- normalized initial value of the animation cursor , 0 = 1st frame, 1 = last frame
playback_rate = 0
}
function init(self)
self.anim_timer = 0
function update(self, dt)
-----------------------------
--========== ANIM =========--
-----------------------------
local timer_max_anim = 0
local nb_frames_anim = 0
if self.direction_x == 0 and self.direction_y == 0 then
anim = hash("idle")
timer_max_anim = timer_max_anim_idle
nb_frames_anim = nb_frames_anim_idle
elseif self.direction_x > 0 then
anim = hash("right")
timer_max_anim = timer_max_anim_walk_right
nb_frames_anim = nb_frames_anim_walk_right
elseif self.direction_x < 0 then
anim = hash("left")
timer_max_anim = timer_max_anim_walk_left
nb_frames_anim = nb_frames_anim_walk_left
elseif self.direction_y > 0 then
anim = hash("top")
timer_max_anim = timer_max_anim_walk_up
nb_frames_anim = nb_frames_anim_walk_up
elseif self.direction_y < 0 then
anim = hash("down")
timer_max_anim = timer_max_anim_walk_down
nb_frames_anim = nb_frames_anim_walk_down
end
if self.anim_timer < timer_max_anim then
self.anim_timer = self.anim_timer + dt
elseif self.anim_timer >= timer_max_anim*self.anim_speed_factor then
self.anim_timer = 0
play_properties["offset"] = play_properties["offset"]+1/(nb_frames_anim)
--play_properties["offset"] = play_properties["offset"]+0.1
end
if play_properties["offset"] > 1/(nb_frames_anim)*(nb_frames_anim-1) then
play_properties["offset"] = 0
end
local color = vmath.vector4(0, 1, 1, 1)
debugdraw.text("Frame #: "..play_properties["offset"]*(nb_frames_anim)+1,p.x+40,p.y+30,color)
debugdraw.text("Offset: "..play_properties["offset"],p.x+40,p.y+15,color)
debugdraw.text("Timer: "..lume.round(self.anim_timer,0.1),p.x+40,p.y+0,color)
debugdraw.text("Speed_Factor: "..lume.round(self.anim_speed_factor,0.1),p.x+40,p.y-15,color)
--if anim ~= self.current_anim then
-- msg.post("#sprite", "play_animation", { id = anim })
sprite.play_flipbook("#sprite", anim, nil, play_properties)
--debugdraw.text("offset-frame: "..offset,p.x,p.y+100,color_white)
self.current_anim = anim
--end