바위 쏘기
Coddy Godot 여정의 프로젝트 섹션에 포함된 레슨. 36개 중 17번째.
바위는 Area2D이고 총알은 몸체이므로, body_entered는 총알이 바위에 부딪힐 때 바위에 알려 줍니다. 모든 총알은 bullets 그룹에 속합니다.
func _on_body_entered(body):
if body.is_in_group("bullets"):
body.queue_free()
burst()
queue_free()총알도 제거하세요. 그렇지 않으면 총알이 계속 날아가 뒤에 있는 바위를 부숩니다. burst()는 이미 rock.gd에 있습니다. 바위가 있던 자리에 먼지구름을 일으킵니다. queue_free()보다 먼저 호출하세요.
챌린지
중급총알로 바위를 부수세요.
rock.gd에서_ready안의body_entered를_on_body_entered에 연결하세요.- 바디가
bullets그룹에 속해 있으면 총알을 제거하고,burst()를 호출한 다음 바위를 제거하세요.
‘플레이’를 누르고 바위를 쏘세요. 바위가 먼지로 흩어집니다. 그런 다음 ‘확인’을 누르세요.
직접 해보기
extends Area2D
var velocity = Vector2.ZERO
func _physics_process(delta):
position += velocity * delta
rotation += 0.5 * delta
if position.x < -30:
position.x += 700
if position.x > 670:
position.x -= 700
if position.y < -30:
position.y += 420
if position.y > 390:
position.y -= 420
# Bursts into dust where the rock is. Call it when the rock breaks.
func burst():
var dust = CPUParticles2D.new()
dust.position = position
dust.one_shot = true
dust.explosiveness = 1.0
dust.amount = 20
dust.lifetime = 0.7
dust.spread = 180.0
dust.gravity = Vector2.ZERO
dust.initial_velocity_min = 30.0
dust.initial_velocity_max = 120.0 * scale.x
dust.scale_amount_min = 2.0
dust.scale_amount_max = 5.0
dust.color = Color(0.75, 0.82, 0.95)
dust.finished.connect(dust.queue_free)
get_parent().add_child(dust)
dust.emitting = true
프로젝트의 모든 레슨
직접 연습해 보세요: Godot 플레이그라운드