compsci_godot_albert/_subsections/sec03/lesson-30.org

1.9 KiB

Section 03 - Lesson 30 | Section Review

Notes

paddle scene

node composition

  • root: Area2D
  • Sprite2D
  • CollisionShape2D

script view: paddle.gd

  • extends Area2D
use @export to create speed variable
  • can be modified in Inspector
  • give default value
override _process function
  • check if Input.is_action_pressed

    • is 'left'
    • is 'right'
  • move paddle

    • get the Node2D.position property

      • multiply speed by delta and add or subtract it from position.x
      • delta is number of seconds that elapsed since the previous frame

gem scene

node composition

  • root: Area2D
  • Sprite2D
  • CollisionShape2D

script view: gem.gd

  • extends Area2D
set class name
  • class_name Gem
  • this allows us to instantiate Gem objects in scripts
set a signal property
  • signal on_gem_off_screen
  • allows us to attach listeners who will be notified when we emit a signal
set export property
  • @export var speed
  • lets us set the speed in the Inspector

modify _process

determine if the gem has gone out of bounds
  • this is going to be the edge of the viewport
  • check position property against the viewport rect property

    if position.y > get_viewport_rect().size.y
if gem went out of bounds
emit signal
on_gem_off_screen.emit()
stop the gem and remove it from the node tree
  • stop going through the process function
  • delete node at the end of the current frame
set_process(false)
queue_free()

Game scene