0 votes

Hi all, I had a little question I've been thinking over for a day now.

In my game, I have the main scene. in it, I'm instancing a Particles2D, and I want to be able to toggle it on and off with the signals emitted for pressing the "Play" button, and when an enemy enters a certain area.

Level.gd

extends Node2D

const SCREEN_HEIGHT = 580
const SCREEN_WIDTH = 1010

signal game_over

var timescore
var kills
var finalscore
var EnergyShot = preload ('res://player-side/EnergyShot.tscn')
var eyeworm = preload ('res://Enemy.tscn')
var SpawnWaitTime = 5
var highscore = 0.0

func _ready():
    randomize()

func _on_SpawnTimer_timeout():
    var enemy = eyeworm.instance()
    enemy.position = Vector2(SCREEN_WIDTH + 10, rand_range(0, SCREEN_HEIGHT))
    enemy.connect("kill", self, "_on_kill")
    add_child(enemy)
    SpawnWaitTime -= 0.15
    if (SpawnWaitTime < 0.4):
        SpawnWaitTime += 4.85
    $Node/SpawnTimer.set_wait_time(SpawnWaitTime)

func _on_SurvivalTimer_timeout():
    timescore += 1
    $HUD.score_count(timescore)

func _on_kill():
    kills += 1
    $HUD.kill_count(kills)

func _on_BaseArea_area_entered(area):
    $Node/SpawnTimer.stop()
    $Node/SurvivalTimer.stop()
    finalscore = timescore + kills*5
    if highscore < finalscore:
        highscore = finalscore
    $HUD.player_loss(finalscore, highscore)
    emit_signal('game_over')
    $BaseArea/CollisionShape2D.disabled = true
    get_tree().call_group("EyeWorms", "queue_free")

func _on_HUD_play_game():
    timescore = 0
    kills = 0
    SpawnWaitTime = 5
    finalscore = 0
    $Node/SurvivalTimer.start()
    $Node/SpawnTimer.start()
    $HUD.show_message("Survive!")
    $BaseArea/CollisionShape2D.disabled = false

func _on_HUD_credits():
    get_tree().change_scene("Credits.tscn")

HUD.gd

extends CanvasLayer

signal play_game
signal credits

func show_message(text):
    $SystemMessage.text = text
    $SystemMessage.show()
    $MessageDuration.start()

func _on_MessageDuration_timeout():
    $SystemMessage.hide()

func player_loss(finalscore, highscore):
    show_message("Game Over")
    yield($MessageDuration, "timeout")
    $Interactive/PlayButton.show()
    $SystemMessage.text = "Play Again?"
    $SystemMessage.show()
    $HighScore.show()
    $CurrentScore.show()
    $HighScore.text = str(highscore)
    $CurrentScore.text = str(finalscore)
    $Interactive/HelpButton.show()
    $Interactive/CreditsButton.show()

func score_count(score):
    $TimeSurvived.text = str(score)

func kill_count(count):
    $Kills.text = str(count)

func _on_Button_pressed():
    $Interactive/PlayButton.hide()
    $Interactive/HelpButton.hide()
    $Interactive/CreditsButton.hide()
    $CurrentScore.hide()
    $TimeSurvived.text = str("0")
    $Kills.text = str("0")
    emit_signal('play_game')

func _on_HelpButton_pressed():
    $Help/ShowHelpDuration.start()
    $Help/Worm.show()
    $Help/HelpLabel1.show()
    $Help/HelpLabel2.show()
    $Help/HelpLabel3.show()
    $Help/HelpLabel4.show()
    $Help/HelpLabel5.show()
    $HelpfulLabel.show()
    $SystemMessage.hide()
    $Interactive/PlayButton.hide()
    yield($Help/ShowHelpDuration, "timeout")
    $Help/Worm.hide()
    $Help/HelpLabel1.hide()
    $Help/HelpLabel2.hide()
    $Help/HelpLabel3.hide()
    $Help/HelpLabel4.hide()
    $Help/HelpLabel5.hide()
    $HelpfulLabel.hide()
    $SystemMessage.show()
    $Interactive/PlayButton.show()

func _on_CreditsButton_pressed():
    emit_signal("credits")


func _on_HUD_credits():
    get_tree().change_scene("Credits.tscn")

Thruster.gd (This is the Particles2D node)

extends Particles2D

var playing = false
in Engine by (197 points)

1 Answer

0 votes

First of all your Thruster.gd should look like this if you want it to not have autoplay when beign instanced.

 extends Particles2D

_ready():
    emitting = false
    one_shot = true # false if you want it to loop

Then signals to your button and area nodes.

func _on_Area2D_body_entered(body):
        get_node("Thuster").emitting = true
    pass

func _on_Button_pressed():
       get_node("Thuster").emitting = true
pass
by (102 points)
edited by
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.