Simply animation example (SOLVED)

#1

Hi,

I’m starting to play with Defold. I’m trying to understand a simple thing.
I have 2 objects: Fish and Fish1 and a script

image

function init(self)
	go.property("speed", 8)
	msg.post(".", "acquire_input_focus") 
    go.animate(".", "euler.z", go.PLAYBACK_LOOP_FORWARD, 360, go.EASING_LINEAR, 60/self.speed)
end

function on_input(self, action_id, action)
	if action_id == hash("touch") and action.pressed then
		self.speed=self.speed+20
		go.cancel_animations("fish1", "euler.z")
		go.animate("fish1", "euler.z", go.PLAYBACK_LOOP_FORWARD, 360, go.EASING_LINEAR, 60/self.speed)
	end
end

I wish fish1 would spin faster with each click. But it doesn’t. Why?

0 Likes

#2

Hi Martin! So what happens when you touch the screen? Nothing?

0 Likes

#3

The first time it turns faster, but not full 360. The next time it turns more slowly and only a few degrees

0 Likes

#4

Isn’t the problem that you click at any point in the rotation, not only when exactly at 0? Example:

  • You click when the fish has rotated 270 degrees at which point you cancel the animation
  • When you start the new animation it starts from where it was cancelled (from 270 degrees) and rotates the remaining 90 degrees over the same time it should have rotated a full 360.

The quick’n’dirty solution is to get the rotation when it is cancelled and rotate 360 degrees from that value:

self.speed=self.speed+20
go.cancel_animations("fish1", "euler.z")
local rot = go.get("fish1", "euler.z")
local to = rot + 360
go.animate("fish1", "euler.z", go.PLAYBACK_LOOP_FORWARD, to, go.EASING_LINEAR, 60/self.speed)
2 Likes

#5

Yes you are right. Thank you very much. Sorry for this question

0 Likes

#6

@britzl And one more question. Do I have to (should) delete the animation in this case? Will the speed increase apply to the old “process” or the new?

0 Likes

#7

You can only have a single animation running for a property. I can’t remember if the system will ignore a call to go.animate() for a property if that property is already animating or if it will replace the running animation.

1 Like

#8

It replaces the running animation

1 Like