From 16b4cd92d83f33db58990b3611bb0a4fce03e462 Mon Sep 17 00:00:00 2001 From: yuki Date: Sun, 9 Nov 2025 06:53:47 -0300 Subject: [PATCH] fixed up exercise 23 (tween cancel) --- main.lua | 6 +++--- obj/Healthbar.lua | 18 +++++++++++------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/main.lua b/main.lua index 25f2189..5699824 100644 --- a/main.lua +++ b/main.lua @@ -1,7 +1,7 @@ -- debug -- -if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then - require("lldebugger").start() -end +-- if os.getenv("LOCAL_LUA_DEBUGGER_VSCODE") == "1" then +-- require("lldebugger").start() +-- end -- libraries -- Object = require 'lib/classic/classic' diff --git a/obj/Healthbar.lua b/obj/Healthbar.lua index 2e578ac..70b39a4 100644 --- a/obj/Healthbar.lua +++ b/obj/Healthbar.lua @@ -10,14 +10,18 @@ function Healthbar:new(x, y, hp) end function Healthbar:decrease(amount) - if self.hp - amount <= 0 then - self.hp = 0 - else - self.hp = self.hp - amount + if self.hp ~= 0 then + if fg then timer:cancel(fg) end + if bg then timer:cancel(bg) end + if self.hp - amount <= 0 then + self.hp = 0 + else + self.hp = self.hp - amount + end + local target_width = self.max_width * (self.hp / 100) + fg = timer:tween(0.3, self, {qwidth = target_width}, 'in-out-cubic') + bg = timer:tween(0.6, self, {lwidth = target_width}, 'in-out-cubic') end - local target_width = self.max_width * (self.hp / 100) - timer:tween(0.3, self, {qwidth = target_width}, 'in-out-cubic') - timer:tween(0.6, self, {lwidth = target_width}, 'in-out-cubic') end function Healthbar:update(dt)