Shoot arrow everytime mouse button is clicked

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Ninku
:warning: Old Version Published before Godot 3 was released.

So far this is my code.
My idea was that , the arrow would spawn on the archer and shoot every time I click my left mouse button.
At the moment, it only shoots once and doesn’t re-spawn the arrow to shoot again.

I dont know any difficult codes but this is what i managed to write up after a bit of research.
Any suggestion, help would be much appreciated.

Thanks.

 extends Node2D
 
# member variables here, example:
# var a=2
# var b="textvar"
 
func _ready():
        set_process_input(true)
        set_fixed_process(false)
        # Called every time the node is added to the scene.
        # Initialization here
        pass
 
var speed = 800
 
func _fixed_process(delta):
        var arrow = load("res://scn/arrow.tscn").instance()
        arrow.set_pos(Vector2(0,0))
        translate(Vector2(delta*speed, 0))
       
        pass
       
func _input(event):
        if Input.is_mouse_button_pressed(true):
               
                set_fixed_process(true)
               
                print("hello")
        pass
 
:bust_in_silhouette: Reply From: puppetmaster-

I don’t see the code where you add your arrow to the scene.

var arrow = load("res://scn/arrow.tscn").instance()
arrow.set_pos(Vector2(0,0))
add_child(arrow) # <---- missing

thanks for the suggestion.
But it didn’t work unfortunately.

Ninku | 2016-08-23 12:48

adding

add_child(arrow) # <---- missing

will never work


Look at this player script there is a function shoot() this creates the bullet(arrow) and shoot it.

In _fixed_process(delta) there is this check

if ( Input.is_action_pressed("shoot") ):
				if canShoot && canReload:
					shoot()

You don’t need to turn on or off the set_fixed_process().

puppetmaster- | 2016-08-23 14:20

Thanks again for the help. I looked at your code but I wasn’t sure what code I could use or add to my own project. I was looking for something simple and easy to understand.

And i found a tutorial ,
( http://codetuto.com/2016/06/create-catch-egg-game-godot-engine-5/ )

This tutorial shows how to spawn objects. I found some similarities between the code thats used in your script and this tutorial. I guess the two main ones were instance() and add_child.

So I made a wall , where the arrow hits it and disappears. I also made another new node and Position2D. I connected the signal from the wall to this new node. So now whenever the arrow hits the wall, it sends the signal to make new arrows.

So my code for this new node looks like this.

 extends Node

# member variables here, example:
# var a=2
# var b="textvar"

func _ready():
	pass


export (PackedScene) var arrow

func _on_border01_body_enter( body ):
	create_arrow()
	pass # replace with function body
	
func create_arrow():
	print("creating arrow")
	var a = arrow.instance()
	a.add_to_group("arrows")
	get_node("Position2D").add_child(a)

Ninku | 2016-08-24 04:59