[Godot game development] FlappyBird: 9.1-2 Stop AnimationPlayer version of infinite ground

This article answers as a csdn blogger [Lao Wang of game development] thinking question: How to realize the stop of AnimationPlayer in FlappyBird.
[Realize the link of AnimationPlayer unlimited floor] [ Link to
ask a question]

It stands to reason that when the game ends, as the bird stops moving, the floor movement should also stop.
Let's take a look at the teacher's code.

#Floor.gd
extends Sprite

func _ready():
	add_to_group("GAME_STATE")

func on_game_over():
	material.set_shader_param("speed",0)

The idea is to make the shader speed zero in the on_game_over function

And the call to the on_game_over function is in 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方法【在这儿】

So, it means that when the game is over, the program will enter the on_body_entered function. And call the code behind it to complete the finishing work and stop the floor movement.

Considering that two Sprite objects are used in the AnimationPlayer version to realize the floor movement, in order not to repeat the stop code in the script corresponding to the two objects, it causes cumbersomeness. I consider doing this:

Add code
Choose to add the code in AnimationPlayer, the code is as follows

#AnimationPlayer.gd
extends AnimationPlayer

func _ready():
	add_to_group("GAME_STATE")
	self.play("floor_1")
	self.play("floor_2")
func stop_an():
    self.stop()

And add a sentence (the last sentence) in 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")#调用函数完成动画的停止工作

At this point, even if the AnimationPlayer version of the infinite ground is realized

Published 1 original article · praised 4 · visits 778

Guess you like

Origin blog.csdn.net/weixin_46359868/article/details/105425905