Werkzeugkasten (Snippets)

Aus Makerspace Wiki
Zur Navigation springen Zur Suche springen

Level

TileSets

Door / Transition

Godot-door2.png

Erzeuge folgende Area2D

# Door.gd
extends Area2D

# Declarations
export(String, FILE, "*.tscn") var target_level

func _on_Door_body_entered(body):
	if "Player" in body.name:
		get_tree().change_scene(target_level)

Fader

Godot-Fader.png

Fader-Node ist vom Typ ColorRect

# Fader.gd
extends ColorRect

func fade_in():
	$AnimationPlayer.play("fade_in")
	return $AnimationPlayer
	
func fade_out():
	get_tree().paused = true
	$AnimationPlayer.play("fade_out")
	return $AnimationPlayer

im Level-Script

onready var fader = $Fader

func _ready():
	...
	fader.fade_in()

Level/Szene wechseln

# Export-Variable anlegen. Szene im Dateimanagement wählbar.
export(String, FILE, "*.tscn") var target_level

# der eigentliche Wechsel
get_tree().change_scene(target_level)

Player

Player tscn v1 0.png

Declarations

# Declarctions
const SPEED = 60
const FLOOR = Vector2(0, -1)
const GRAVITY = 10
const JUMP_POWER = 350

Steuerung

func _physics_process(delta):
	velocity.x = 0
	velocity.y += GRAVITY 
	
	check_key_input()
	
	velocity = move_and_slide(velocity, FLOOR)
	
	set_animation()


func check_key_input():
	if Input.is_action_pressed("ui_left"):
		velocity.x = - SPEED
	if Input.is_action_pressed("ui_right"):
		velocity.x = SPEED
	if Input.is_action_just_pressed("ui_up"):
		velocity.y = -JUMP_POWER

Animation

func set_animation():
	if velocity.x < 0:
		sprite.flip_h = true
		sprite.play("walk")
	
	if velocity.x > 0:
		sprite.flip_h = false
		sprite.play("walk")
	
	if velocity.x == 0:
		sprite.play("idle")
		
	if !is_on_floor():
		sprite.play("air")

Camera

  • Camera-Node in Player einfügen
  • enable Smothing and play width the speed
  • Limits einstellen
  • Editor | Draw Limits auf ON

oder Camera in die Game-Node und per Script connecten

onready var camera = $Camera2D

# Aufruf in _ready()-Function
func connect_camera_on_player():
	remove_child(camera)
	player.add_child(camera)

Gegner / Gegner KI

Vorlage