跳到主要内容

会动的图片和卷动背景

图片元素加上 timeline,就能照你写的几个时间点移动和淡入淡出。再加 timeline-loop: true,它会一直循环。这一篇先让一张图上下浮动,再做一条一直往左卷的装饰带,最后用按钮控制它停下和继续。

timeline 只对图片(type: image)有效,用到的图都是云界面自带的。

第 1 步:让一张图上下浮动​

在 plugins/CloudUI/screens/ 新建 tut_scroll.yml:

portrait:
enabled: false
camera:
animation: false
elements:
backdrop:
type: image
image: proxy/backdrop.png
x: 0
y: 0
resolution: 0.25
fit: cover
logo:
type: image
image: main/title.png
x: 530
y: 150
motion: none
timeline:
- {t: 0, y: 0}
- {t: 30, y: 16}
- {t: 60, y: 0}
timeline-loop: true

/cui reload,/cui open tut_scroll。画面上方的标题图应该缓缓往下沉 16 像素再浮回来,三秒一轮,一直重复。按 F 或 Shift 关掉。

timeline 是一串关键帧,每一帧可以写四样东西:

键说明
t第几 tick,20 tick 是一秒。从画面出现时算起
x、y相对图片原本位置的偏移,画布像素。x 正数往右,y 正数往下
alpha不透明度,0 是完全透明,1 是原样

没写的值沿用前一帧的。两帧之间云界面会自己补上平滑的移动。

要循环得顺,最后一帧要和第一帧一样。上面第一帧和最后一帧都是 y: 0,接回去时就看不出跳动。

motion: none 是不要进场的滑入动画,图片一开始就待在原位。做循环动画的图片建议都写上。

第 2 步:加上闪烁​

alpha 可以和位移一起写。把 logo 的 timeline 换成:

timeline:
- {t: 0, y: 0, alpha: 1}
- {t: 30, y: 16, alpha: 0.5}
- {t: 60, y: 0, alpha: 1}

/cui reload 后再打开,标题图沉下去的同时变淡,浮上来时变回来。

只想播一次、不要循环(例如奖励弹出来再消失),把 timeline-loop 那行拿掉:

timeline:
- {t: 0, y: -40, alpha: 0}
- {t: 8, y: 0, alpha: 1}
- {t: 40, alpha: 1}
- {t: 60, alpha: 0}

这段是从上方 40 像素处淡入落下,停一秒半,再淡出。

第 3 步:做一条无缝卷动的装饰带​

卷动的原理:同一张图排好几份,一份接一份,每份之间刚好隔一张图的宽度。全部用同一个 timeline 往左移一张图的宽度,然后循环。一轮结束时,每一份刚好走到前一份原本的位置,接回起点看不出来。

teleport/ornament.png 宽 840 像素。画布宽 1920,要盖满再多出一张的宽度,需要四份,放在 x = 0、840、1680、2520。在 elements: 下面加:

strip-a:
type: image
image: teleport/ornament.png
x: 0
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true
strip-b:
type: image
image: teleport/ornament.png
x: 840
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true
strip-c:
type: image
image: teleport/ornament.png
x: 1680
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true
strip-d:
type: image
image: teleport/ornament.png
x: 2520
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true

/cui reload 后打开,画面底部应该有一条装饰线一直往左流动,12 秒走一张图的宽度,看不到接缝。

要换成自己的图,记住三件事:

  1. 图的左右两边要接得上(左边缘和右边缘画成连续的)。
  2. 每一份的 x 相差一张图的宽度,timeline 最后一帧的 x 是负的图片宽度。
  3. 份数要够:图宽乘以份数,至少是 1920 加上一张图的宽度。整个画面宽的图(1920)两份就够,放在 x = 0 和 1920,timeline 写 [{t: 0, x: 0}, {t: 200, x: -1920}]。

想往右卷,把四份的 x 改成 -840、0、840、1680,最后一帧改成 x: 840。想快一点就把 t 调小。

第 4 步:用变量控制播放​

timeline-play 写一个变量,变量有值时才播放。值是空的、hidden、false 或 0 时,图片停在第一帧。

在文件最上面加默认值:

values:
run: '1'

四份装饰带各加一行:

timeline-play: '{run}'

再加两个文字按钮去改这个值:

stop:
type: text
text: '停下'
font: text
size: 24
color: '#E2EEF4'
hover-color: '#FFE08A'
align: center
x: 880
y: 900
click:
left: ['console: cui set {player} run=0']
go:
type: text
text: '继续'
font: text
size: 24
color: '#E2EEF4'
hover-color: '#FFE08A'
align: center
x: 1040
y: 900
click:
left: ['console: cui set {player} run=1']

/cui reload 后打开,点「停下」装饰带回到起点不动,点「继续」重新开始卷。插件给的变量、PlaceholderAPI 变量也能拿来当 timeline-play,例如奖励可以领的时候才让光效动起来。

完整文件​

plugins/CloudUI/screens/tut_scroll.yml:

values:
run: '1'
portrait:
enabled: false
camera:
animation: false
elements:
backdrop:
type: image
image: proxy/backdrop.png
x: 0
y: 0
resolution: 0.25
fit: cover
logo:
type: image
image: main/title.png
x: 530
y: 150
motion: none
timeline:
- {t: 0, y: 0, alpha: 1}
- {t: 30, y: 16, alpha: 0.5}
- {t: 60, y: 0, alpha: 1}
timeline-loop: true
strip-a:
type: image
image: teleport/ornament.png
x: 0
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true
timeline-play: '{run}'
strip-b:
type: image
image: teleport/ornament.png
x: 840
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true
timeline-play: '{run}'
strip-c:
type: image
image: teleport/ornament.png
x: 1680
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true
timeline-play: '{run}'
strip-d:
type: image
image: teleport/ornament.png
x: 2520
y: 980
motion: none
timeline: [{t: 0, x: 0}, {t: 240, x: -840}]
timeline-loop: true
timeline-play: '{run}'
stop:
type: text
text: '停下'
font: text
size: 24
color: '#E2EEF4'
hover-color: '#FFE08A'
align: center
x: 880
y: 900
click:
left: ['console: cui set {player} run=0']
go:
type: text
text: '继续'
font: text
size: 24
color: '#E2EEF4'
hover-color: '#FFE08A'
align: center
x: 1040
y: 900
click:
left: ['console: cui set {player} run=1']
leave:
type: button
image: main/close.png
hover: main/close_hover.png
x: 1664
y: 22
hit: [1664, 22, 236, 52]
click:
left: ['close']
leave-text:
type: text
text: '关闭'
font: text
size: 20
color: '#BECDD7'
align: center
x: 1782
y: 39

常见问题​

背景图写了 timeline 却不动​

fit: cover 的图不吃 timeline。铺满整个画面的背景要卷动,就不要写 fit: cover,改成普通图片,照第 3 步放两份。

每一轮接回去的时候顿一下​

先看最后一帧和第一帧是不是对得上(卷动的话是位移刚好一张图的宽度)。对得上还是有一点停顿:一般模式下每轮接回起点会停大约一个 tick(0.05 秒),卷得慢看不出来,越快越明显。光影兼容模式没有这个停顿。

想做小框里的跑马灯​

做不到。图片移出范围时不会被裁掉,整张都会画出来。所以卷动适合整个画面宽的背景,或者两端刚好被别的图盖住的位置。

关键帧的顺序乱了​

t 要一帧比一帧大。写得比前一帧小或一样时,会被当成前一帧的下一个 tick,动作就挤在一起了。

相关页面​

0:000:00