【Godot游戏开发】FlappyBird:9.1-2停止无限地面之AnimationPlayer版

Olive ·
更新时间:2024-09-21
· 977 次阅读

本文作为csdn博主【开发游戏的老王】的思考题的回答:如何实现AnimationPlayer在FlappyBird中的停止问题。
【实现AnimationPlayer无限地板的链接】
【提出问题的链接】

按道理,当游戏结束之后,随着小鸟的停止运动,地板的移动也应该停止。
我们来看一下老师的代码。

#Floor.gd extends Sprite func _ready(): add_to_group("GAME_STATE") func on_game_over(): material.set_shader_param("speed",0)

大意就是在on_game_over函数中让shader的速度为零

而对on_game_over函数的调用在bird.gd中

#bird.gd func on_body_entered(_body): if _body is StaticBody2D:#碰撞以后 call_deferred("set_physics_process",false)#停用_physics_process(delta) call_deferred("set_contact_monitor",false)#关闭碰撞检测 AudioManager.play("sfx_hit")#播放碰撞音效 $AnimationPlayer.play("die")#动画切换到死亡状态 GameData.update_record()#更新最好成绩记录 get_tree().call_group("GAME_STATE","on_game_over")#调用GAME_STATE的on_game_over方法【在这儿】

那么,意思就是 当游戏结束后,程序会进入on_body_entered函数。并且调用它后面的代码完成收尾工作并停止地板的运动。

考虑到AnimationPlayer版中使用了两个Sprite对象来实现地板的移动,为了不把停止代码在两个对象所对应的脚本中重复导致累赘。我考虑这样完成:

加入代码
在AnimationPlayer中选择加入代码,代码如下

#AnimationPlayer.gd extends AnimationPlayer func _ready(): add_to_group("GAME_STATE") self.play("floor_1") self.play("floor_2") func stop_an(): self.stop()

并且在bird.gd中加入一句(最后一句)

#bird.gd func on_body_entered(_body): if _body is StaticBody2D:#碰撞以后 call_deferred("set_physics_process",false)#停用_physics_process(delta) call_deferred("set_contact_monitor",false)#关闭碰撞检测 AudioManager.play("sfx_hit")#播放碰撞音效 $AnimationPlayer.play("die")#动画切换到死亡状态 GameData.update_record()#更新最好成绩记录 get_tree().call_group("GAME_STATE","on_game_over") get_tree().call_group("GAME_STATE","stop_an")#调用函数完成动画的停止工作

至此,停止无限地面之AnimationPlayer版就算实现了


作者:cauchy733



godot

需要 登录 后方可回复, 如果你还没有账号请 注册新账号
相关文章