extends Sprite2Dfunc _process(delta): # Flip sprite based on movement direction var velocity = get_parent().velocity if velocity.x < 0: flip_h = true elif velocity.x > 0: flip_h = false
# Play animationplay("run")# Pause without resettingpause()# Resume from pauseplay() # No arguments resumes current animation# Stop and reset to frame 0stop()
extends Sprite2Dfunc _input(event): if event is InputEventMouseButton and event.pressed: # Check if click is on opaque pixel var local_pos = to_local(event.position) if get_rect().has_point(local_pos): if is_pixel_opaque(local_pos): print("Clicked on sprite!")
# For pixel art (crisp, no blur)texture_filter = CanvasItem.TEXTURE_FILTER_NEAREST# For smooth graphics (default)texture_filter = CanvasItem.TEXTURE_FILTER_LINEAR
You can also set the default texture filter in Project Settings under Rendering > Textures > Canvas Textures > Default Texture Filter.
extends CharacterBody2D@onready var sprite = $AnimatedSprite2Dconst SPEED = 200.0const JUMP_VELOCITY = -400.0func _physics_process(delta): # Add gravity if not is_on_floor(): velocity += get_gravity() * delta # Jump if Input.is_action_just_pressed("jump") and is_on_floor(): velocity.y = JUMP_VELOCITY sprite.play("jump") # Horizontal movement var direction = Input.get_axis("move_left", "move_right") if direction != 0: velocity.x = direction * SPEED sprite.flip_h = direction < 0 if is_on_floor(): sprite.play("run") else: velocity.x = move_toward(velocity.x, 0, SPEED) if is_on_floor(): sprite.play("idle") # In air animation if not is_on_floor() and velocity.y > 0: sprite.play("fall") move_and_slide()