0 votes

I have Node2D (main scene), and a child node Canvas layer. In cavas layer I have another Node2D named HUD with a label to storage the score. I have an object Area2D named "coin".

My doubt: how to incremente the score when my player colide with the coin
I created on body entered. But how to associate these codes?

in Engine by (35 points)

1 Answer

0 votes

If main scene is game level then use Node instead of Node2D.
Why do you want to have HUD as a child of Canvas layer, is there any reason?
....I would use Control node and label node as his child.
...then in Node( main scene) create script then get label in script by

var score_count = $label

or in your case var score_count = $CanvasLayer/Control(use Control node!)/label
Then with Area2D coin you created a signal bodyentered....connect this signal
function _on
Area2Dbodyentered() will be created in mainscene(root node) script.
script for Node (main
scene.gd)

extends Node

onready var score_count  = $label
var coin = int(0)
var coin_hit = bool (false)

func _ready():
      score_count.set("text", str(coin))

func _process(delta):
      if coin_hit == true:
          coin = +1
          score_count.set("text", str(coin))

func _on_Area2D_body_enter():
      coin_hit = true

....the solutions is more...download the documentation in pdf and search there

by (450 points)
edited by

Bishop, thanks for your help

an error was occured

"indentifier not found: coin_hit"

on body entered that I'm using is player

and the variable coin_hit was declared in game (main scene)

Well,....so,it might be better to use and create two areas one in the player node and of course the coin area and using groups....in coin area go to groups and
name it for example "coin".....and in the player node and his area connect
signal in player script...signal - areaentered(Area2D area)......then you have a function
_on
playerareaarea_entered(area): in the player script.
and because you have now group "coin" we can get this group when player area entered coin area.
in player.gd script

func _on_player_area_area_entered(area):
       if area.is_in_group("coin"):
            coin += 1
            score_count.set("text", str(coin))
            area.queue_free() 

I created a test scene
player - KinematicBody 2D
coin - Area2D ...group "coin"
CanvasLayer/hud (Control node)/coin_count(Label mode)
This is my player.gd script

extends KinematicBody2D

var vel_x = Vector2(1,0)
var speed = int(50)

var coin = int(0)
onready var coin_count = $"../CanvasLayer/hud/coin_count"
#---------------------------------------------------------------------
func _ready():
       coin_count.set("text", str(coin))
#---------------------------------------------------------------
func _physics_process(delta):
       if Input.is_action_pressed("ui_right"):
            translate(vel_x * speed * delta)
       if Input.is_action_pressed ("ui_left"):
            translate (-vel_x * speed * delta)
#--------------------------------------------------------------------------
func _on_player_area_area_entered(area):
       if area.is_in_group("coin"):
             coin += 1
             coin_count.set("text", str(coin))
             area.queue_free()
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.