How to create (duplicate) a sprite every click at the position of a click?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Suleymanov

Every explosion should leave a crater after itself. I click, explosion animation plays, so far so good. I have a sprite (crater) which I want to duplicate every time I explode (click) some place to appear at the place of a click and stay there. How can I do it by a script? Can you please, show me an example? Thank you for your time!

1 Like
:bust_in_silhouette: Reply From: jgodfrey

Here’s one way…

To use it, make sure you’ve added an event named click to your input map and associate it with the left-mouse button…

Next, create a separate scene named sprite that contains your crater sprite.

Finally, create a new 2D scene with the following script attached to the root node:

extends Node2D

var sprite = preload("res://sprite.tscn")

func _process(delta):
	if Input.is_action_just_pressed("click"):
		var s = sprite.instance()
		s.position = get_global_mouse_position()

That will create a new instance of the sprite scene (your crater sprite) at every position where the mouse is clicked.

I highly appreciate your time! I did exactly what you asked, and it returned error on “var s = sprite.instance()” line saying “Nonexistent function ''instance” in base “string”". I am using Godot v3.2.3 Stable.

Suleymanov | 2020-10-24 19:20

I think you have an error somewhere. sprite is not a string, but a reference to the sprite scene you should have created.

  • Did you create a scene containing your sprite?
  • Is that scene named sprite?
  • Is it in the root of your project? If not, change the path as needed on the var sprite = ... line of code

Otherwise, if you’ve done all of the above, maybe post your code? I’m confident that what I did works…

jgodfrey | 2020-10-24 19:29

I have CRATER.tscn that contains my crater sprite saved in PREFAB folder.

Then I have LEVEL_01.tscn that contains the main stuff.

I don’t have CRATER.tscn instance node in LEVEL_01 scene.

In LEVEL_01 scene I have a script attached to the root node that goes like:

extends Node2D

var Crater = ("res://PREFAB/CRATER.tscn")

func _ready():

func _process(delta):
    if Input.is_action_pressed("MLB"):
	    var s = Crater.instance()
	    s.position = get_global_mouse_position()

Suleymanov | 2020-10-24 19:40

You’re missing the preload() call where you define Crater. That should read:

var Crater = preload("res://PREFAB/CRATER.tscn")

jgodfrey | 2020-10-24 19:44

I missed “preload”, my bad. Works perfectly! Huge thanks!

Suleymanov | 2020-10-24 19:49

There’s one small problem remaining maybe you have suggestion for too. Now Explosion plays and Crater is instanced every time I click perfectly. But Crater appears on top of the explosion when it needs to be the other way round. No matter where I move my AnimatedSprite (Explosion), the problem remains.

Suleymanov | 2020-10-24 20:12

Problem Solved! I created a child node to the root and in the root script I added “get_node(child_node_path).” before “add_child(s)”.

Suleymanov | 2020-10-25 08:46

1 Like