|
|
|
|
Reply From: |
kidscancode |
The problem is you’re only creating one instance, at load time. Instead, create a separate instance whenever you need:
var PlayerBasicShot = preload("res://PlayerBasicShot.tscn")
Then in your shooting code:
if randi() % 10 == 0:
var shot_instance = PlayerBasicShot.instance()
add_child(shot_instance)
shot_instance.ShotAngle = shipangle
See here in the docs for an example:
No success. Same problem.
I added your first line in func _ready() and second code at _process().
Just in case here’s the PlayerBasicShot code…
extends Node2D
var ShotDirection = Vector2()
var ShotLifeSpan = 0
var ShotAngle = 45
func _ready():
ShotDirection.x = cos(deg2rad(ShotAngle))
ShotDirection.y = -sin(deg2rad(ShotAngle))
func _process(delta):
position.x += ShotDirection.x*delta*4500
position.y += ShotDirection.y*delta*4500
ShotLifeSpan += 1 * delta
if ShotLifeSpan > 2:
self.queue_free()
Leandro | 2018-11-23 20:51
This is a whole separate problem. First, you’re not setting the instance’s position when you create it, so where is it supposed to start?
Second, that code in _ready()
is not only not necessary, but it’s not going to do anything because you will wind up setting the angle after the _ready()
code runs.
I say “not necessary” because when you’re working with vectors, you don’t need to muck around with trig functions or separate x/y variables. Vectors contain directional information already. So lose sin
and cos
code. Instead, you just need a velocity
vector which describes the direction and magnitude of your desired movement. This way your _process()
function just becomes:
position += velocity * delta
Second, did you look at the example I linked? The code there shows exactly what I’m talking about. The usual practice in situations like this (spawning instances via code) is to give the instance an initializer function (start()
in the linked example), and pass to it all the parameters it needs to know, such as position and direction.
kidscancode | 2018-11-23 21:04
Sorry about the delay and not reading the code example you linked before (I was about to leave the office and temporarily without Internet at home, so I wanted to do it quickly). I finally got it to work, many thanks!
Godot is so full of already made stuff it’ll take sometime for me to learn to do things in the easy way. Really loving it.
Leandro | 2018-11-24 18:55