官术网_书友最值得收藏!

What is a tween?

A tween is a way to interpolate (change gradually) some value over time (from a start value to an end value) using a particular function. For example, you might choose a function that steadily changes the value or one that starts slow but ramps up in speed. Tweening is also sometimes referred to as easing.

When using a Tween node in Godot, you can assign it to alter one or more properties of a node. In this case, you're going to increase the Scale of the coin and also cause it to fade out using the Modulate property.

Add this line to the _ready() function of Coin:

$Tween.interpolate_property($AnimatedSprite, 'scale',
$AnimatedSprite.scale,
$AnimatedSprite.scale * 3, 0.3,
Tween.TRANS_QUAD,
Tween.EASE_IN_OUT)

The interpolate_property() function causes the Tween to change a node's property. There are seven parameters:

  • The node to affect
  • The property to alter
  • The property's starting value
  • The property's ending value
  • The duration (in seconds)
  • The function to use
  • The direction

The tween should start playing when the player picks up the coin. Replace queue_free() in the pickup() function:

func pickup():
monitoring = false
$Tween.start()

Setting monitoring to false ensures that the area_enter() signal won't be emitted if the player touches the coin during the tween animation.

Finally, the coin should be deleted when the animation finishes, so connect the Tween node's tween_completed() signal:

func _on_Tween_tween_completed(object, key):
queue_free()

Now, when you run the game, you should see the coins growing larger when they're picked up. This is good, but tweens are even more effective when applied to multiple properties at once. You can add another interpolate_property(), this time to change the sprite's opacity. This is done by altering the modulate property, which is a Color object, and changing its alpha channel from 1 (opaque) to 0 (transparent). Refer to the following code:

$Tween.interpolate_property($AnimatedSprite, 'modulate', 
Color(1, 1, 1, 1),
Color(1, 1, 1, 0), 0.3,
Tween.TRANS_QUAD,
Tween.EASE_IN_OUT)
主站蜘蛛池模板: 浦城县| 介休市| 沅江市| 赤水市| 黄冈市| 巨鹿县| 宜城市| 昌宁县| 乌拉特后旗| 洪泽县| 连平县| 东兰县| 凌源市| 莆田市| 金乡县| 日照市| 武鸣县| 桃源县| 安义县| 五大连池市| 西平县| 内乡县| 靖西县| 屏东县| 南木林县| 湟源县| 达州市| 邓州市| 东乌珠穆沁旗| 庆元县| 惠来县| 滦南县| 庄浪县| 定安县| 长兴县| 永宁县| 夏津县| 北川| 中山市| 台中市| 绥化市|