Hi,
I know this is probably and hopefully just a basic programming question but it seems like it should work as intented. I tried changing multiple things but my animation states break at random.
Further explanation: in my game the player can “idle”, “dig” and get an “emerald”. These are the possible states. I want to switch from the “dig” state back to “idle” in case the player does not find an emerald.
This is done by a simple on_message when the animation is finished. But at random times I get stuck in the dig state on the last frame (because the animation is played once only). I can’t seem the figure out why. It would make sense if it would be stuck all the time but it is just random. I have the feeling that clicking more often (“touch” == click input) is more likely to break it.
Here is the whole player script. There is no other script within the game that could interfer with it.
function init(self)
msg.post(".", "acquire_input_focus")
math.randomseed(os.time())
self.anim = "idle"
self.states = {"idle", "dig", "emerald"}
self.state = self.states[1] -- Starting in idle state
self.emeraldProb = 0.10
self.money = 0
end
local function animate(self, dt)
local currentAnim = go.get("#sprite", "animation") --hashed!
self.anim = self.state
if currentAnim ~= hash(self.anim) then
msg.post("#sprite", "play_animation", {id = hash(self.anim)})
end
end
local function dig(self)
local checkProb = math.random()
if checkProb < self.emeraldProb then
self.state = self.states[3]
self.money = self.money + math.random(100, 300)
print(self.money)
else
return
end
end
function final(self)
end
function update(self, dt)
animate(self, dt)
print(self.state)
end
function fixed_update(self, dt)
end
function on_message(self, message_id, message, sender)
if message_id == hash("animation_done") then
self.state = self.states[1]
end
end
function on_input(self, action_id, action)
if action_id == hash("touch") and self.state == self.states[1] and action.pressed then
self.state = self.states[2]
dig(self)
end
if action_id == hash("right") and self.state == self.states[3] and action.pressed then
self.state = self.states[1]
end
end
function on_reload(self)
end
I would greatly appreciate looking into my problem because I have the feeling it is system related. And I would really like to know that it is just my missing programming skills .
Thanks in advance for reading!