|
|
|
|
Reply From: |
bruteforce |
var bg = get_parent().get_parent()
or
var bg = get_node("../../")
or
var bg = get_node("../../../Background")
etc…
Thanks for responding. Even using these examples I can’t seem to get the texture width of the sprite Background.
I type “get_parent().get_parent()” then a “.” However, none of the properties of a Sprite are available in the auto complete drop down. I figured I would get the “get_rect” or “get_texture” or “get_pos” etc would be available, but none of them are. What can I do to get the width of the sprite?
Thanks
ondesic | 2017-04-03 15:06
You are doing something wrong
Here is a screenshot:
And the code completion also works properly:
bruteforce | 2017-04-03 16:48
Bruteforce,
For some reason your screenshots show as broken. I can’t see the pictures.
When I print get_type on the var bk, it says “Sprite”, so I know I am in the right place, I just can’t assess the properties. Your screen shots will probably help when I can see them.
ondesic | 2017-04-03 18:17
OK, the first:
extends RigidBody2D
func _ready():
print( get_node("../../").get_texture().get_size() )
print( get_node("../../").get_texture().get_width() )
print( get_node("../../").get_texture().get_height() )
The output:
(58, 94)
58
94
The second image is just a screenshot about code completion
Direct links:
1.) http://i.imgur.com/HurTNTF.jpg
2.) http://i.imgur.com/P5mH6rP.jpg
bruteforce | 2017-04-03 18:40
I have followed everything and it will not work.
I did a test and added a new node and a rigidbody2d. This time it works.
However, like I mentioned in the original post, the Player node (with the rigidbody2d) is an instance. I wonder if this has anything to do with it?
ondesic | 2017-04-03 20:02
Give us more details about the error (error message, the complete project / relevant code snippet… or something)!
You said:
“…When I print get_type on the var bk, it says Sprite…”
So you found the sprite
node…
The sprite
has a get_texture()
member function, which returns a Texture
object.
Doc: Sprite class
If this texture
is not null, you can call it’s get_size()
, get_width()
, get_height()
… functions.
Doc: Texture class
bruteforce | 2017-04-04 11:38
Bruteforce,
I completely understand that the sprite should have get_width()
, get_texture()
etc. It just simply isn’t there.
In the best detail I can give:
I created a scene with a node called “Player”.
I added 3 children under this node called “RigidBody2D”, “Sprite”, and “AnimationPlayer”
I saved the scene.
Next, I opened my main scene that has the background sprite in it.
I add the Player scene to the Main scene as an Instance.
I had to click on the Player node and select “Editable Children” to gain access to the RidigBody2D inside Player.
Next I added a script to the RigidBody2D.
It is in this script that no matter what I do I cannot access the Main scene’s Background sprite’s properties.
It is strange because if I add a node directly to the Main scene (not an instance), I can access the background spite’s properties. However, I can’t through the children of the Instantiated Player node. Does that make sense?
ondesic | 2017-04-04 15:51