Skip to content

Commit 2454963

Browse files
authored
Added an example about cancelling animations. (#171)
1 parent 2332b6b commit 2454963

11 files changed

Lines changed: 180 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
path_settings {
2+
path: "**"
3+
profile: "Default"
4+
}
5+
profiles {
6+
name: "Default"
7+
platforms {
8+
os: OS_ID_GENERIC
9+
formats {
10+
format: TEXTURE_FORMAT_RGBA
11+
compression_level: BEST
12+
compression_type: COMPRESSION_TYPE_DEFAULT
13+
}
14+
mipmaps: false
15+
max_texture_size: 0
16+
premultiply_alpha: true
17+
}
18+
}
146 KB
Binary file not shown.
3.65 KB
Loading
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
images {
2+
image: "/assets/images/shipBlue_manned.png"
3+
}
4+
extrude_borders: 2
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
font: "/assets/SourceSansPro-Semibold.ttf"
2+
material: "/builtins/fonts/font-df.material"
3+
size: 32
4+
output_format: TYPE_DISTANCE_FIELD
5+
characters: " !\"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
---
2+
tags: animation, input
3+
title: Cancel animation
4+
brief: This example shows how to use `go.cancel_animations()` to stop a running animation and start a new one.
5+
author: Defold Foundation
6+
scripts: cancel_animation.script
7+
---
8+
9+
The collection contains two game objects:
10+
- `ship` with one sprite and one script.
11+
- `description` with a label for displaying the instructions text
12+
13+
![setup](setup.png)
14+
15+
## How It Works
16+
17+
The script starts an animation of movement along the X axis in `init()`. The ship starts moving to the right and back. Then it listens for input so it can interrupt that animation.
18+
19+
Clicking or tapping cancels the currently played animations immediately and starts a new one to move the ship toward the new x position instead.
20+
21+
When the player presses the mouse button or touches the screen, the script first calls [`go.cancel_animations()`](https://defold.com/ref/go-lua/index.html#go.cancel_animations:url-property). Defold stops the running tween and leaves the game object's position exactly where it was at that moment.
22+
23+
The script then calls [`go.animate()`](https://defold.com/ref/go-lua/index.html#go.animate:url-property-playback-to-easing-duration-delay-complete_function) again with the clicked x coordinate as the new target. Because the old animation was canceled first, the new tween starts from the ship's current position instead of jumping back to the original starting point.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: "default"
2+
scale_along_z: 0
3+
embedded_instances {
4+
id: "ship"
5+
data: "components {\n"
6+
" id: \"script\"\n"
7+
" component: \"/example/cancel_animation.script\"\n"
8+
"}\n"
9+
"embedded_components {\n"
10+
" id: \"sprite\"\n"
11+
" type: \"sprite\"\n"
12+
" data: \"default_animation: \\\"shipBlue_manned\\\"\\n"
13+
"material: \\\"/builtins/materials/sprite.material\\\"\\n"
14+
"textures {\\n"
15+
" sampler: \\\"texture_sampler\\\"\\n"
16+
" texture: \\\"/assets/sprites.atlas\\\"\\n"
17+
"}\\n"
18+
"\"\n"
19+
"}\n"
20+
""
21+
position {
22+
x: 80.0
23+
y: 360.0
24+
}
25+
}
26+
embedded_instances {
27+
id: "description"
28+
data: "embedded_components {\n"
29+
" id: \"label\"\n"
30+
" type: \"label\"\n"
31+
" data: \"size {\\n"
32+
" x: 600.0\\n"
33+
" y: 32.0\\n"
34+
"}\\n"
35+
"line_break: true\\n"
36+
"text: \\\"Click or touch anywhere to cancel current animation and move to the cursor position\\\"\\n"
37+
"font: \\\"/assets/text32.font\\\"\\n"
38+
"material: \\\"/builtins/fonts/label-df.material\\\"\\n"
39+
"\"\n"
40+
" position {\n"
41+
" x: 360.0\n"
42+
" y: 60.0\n"
43+
" }\n"
44+
"}\n"
45+
""
46+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function init(self)
2+
msg.post(".", "acquire_input_focus") -- <1>
3+
go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, 600, go.EASING_INOUTSINE, 5.0) -- <2>
4+
end
5+
6+
function on_input(self, action_id, action)
7+
if (action_id == hash("mouse_button_left") or action_id == hash("touch")) and action.pressed then -- <3>
8+
go.cancel_animations(".", "position") -- <4>
9+
go.animate(".", "position.x", go.PLAYBACK_ONCE_FORWARD, action.x, go.EASING_INOUTSINE, 1.2) -- <5>
10+
end
11+
end
12+
13+
--[[
14+
1. Acquire input focus so the script receives mouse clicks and touch presses.
15+
2. Start with a long horizontal animation toward x = 600 so there is something to interrupt.
16+
3. React to either a left mouse click or a touch press.
17+
4. Cancel the running position animation. Defold keeps the property at its current value when the animation is canceled.
18+
5. Start a new animation from that current x position toward the clicked x coordinate.
19+
]]
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
[project]
2+
title = Cancel Animation
3+
version = 0.1
4+
5+
[bootstrap]
6+
main_collection = /example/cancel_animation.collectionc
7+
8+
[input]
9+
game_binding = /builtins/input/all.input_bindingc
10+
repeat_interval = 0.05
11+
12+
[display]
13+
width = 720
14+
height = 720
15+
high_dpi = 1
16+
17+
[physics]
18+
scale = 0.02
19+
gravity_y = -500.0
20+
21+
[script]
22+
shared_state = 1
23+
24+
[collection_proxy]
25+
max_count = 256
26+
27+
[label]
28+
subpixels = 1
29+
30+
[sprite]
31+
subpixels = 1
32+
max_count = 32765
33+
34+
[windows]
35+
iap_provider =
36+
37+
[android]
38+
package = com.defold.examples
39+
40+
[ios]
41+
bundle_identifier = com.defold.examples
42+
43+
[osx]
44+
bundle_identifier = com.defold.examples
45+
46+
[html5]
47+
show_fullscreen_button = 0
48+
show_made_with_defold = 0
49+
scale_mode = no_scale
50+
heap_size = 64
51+
52+
[graphics]
53+
texture_profiles = /all.texture_profiles
54+
55+
[collection]
56+
max_instances = 32765
57+
58+
[particle_fx]
59+
max_emitter_count = 1024
60+
61+
[render]
62+
clear_color_blue = 0.183594
63+
clear_color_green = 0.164063
64+
clear_color_red = 0.160156
65+
109 KB
Loading

0 commit comments

Comments
 (0)