I want to get x1 and x2 coordinates of spawned object.
Any suggestions how can i reach this ?
I want to get x1 and x2 coordinates of spawned object.
Any suggestions how can i reach this ?
Hello and welcome to the forum!
I'm not sure exactly what you are asking, but let me see if I can help.
Probably the most straight-forward thing is to get the "position" of the Sprite. For example:
var pos = my_sprite.position
That will return a Vector2 object with the x and y values of the sprite. Then you can adjust this number based on the size of the object. Sprite has a member function called "get_rect". This will give you a Rect object which has a size parameter that will be the size of the sprite.
So you can do something like this:
var pos = my_sprite.position
var rect = my_sprite.get_rect()
var half_size = Vector2(0.0, rect.size.y * 0.5)
var top_center = pos - half_size
var bottom_center = pos + half_size
This assumes the pivot point of the sprite is in the center of the object.
Not sure if that was what you were asking about, but maybe that will give you an idea. Cheers.
@cybereality Hi it's exactly what i look for, great thanks ! But it's giving me local position ... how i can translate coordinates to parent of giving object ? P.S. I have an root of hierarchy node2d and second object(M) as child of this node , so i want to spawn another object right behind (M) object. I tried line below but it's not worked correct.
1. objectM.position = objectM.get_parent().transform.xform(objectM.position)
2. get_parent().add_child(objectM)
You can just add the parent position to the object position, but this shouldn't be needed if both objects are on the same layer in the hierarchy. The position coordinates are local to the parent. So if you have 2 objects both as direct children of the parent, then their coordinates are in the same space.
Also, maybe your code has a typo. It is setting objectM position to itself, which doesn't make sense. Did you mean to set the position for another object?
@cybereality said: Also, maybe your code has a typo. It is setting objectM position to itself, which doesn't make sense. Did you mean to set the position for another object?
I'm not sure but my logic was so : take current position of object and translate to his parents transform but it seems like do nothing or not ?
Also your code above work only if i wrap it into to_global function:
to_global(bottom_center)
to_global(top_center )
So, maybe i'm doing something wrong ?
Ah, I get what you are trying to do. But a direct child of a parent is already in the parent's coordinate space. So calling xform will do nothing since it's the same value. If you want the position relative to the parent of the parent, you can do this:
# other code as above
var parent_pos = my_sprite.get_parent().position
var top_center = pos - half_size + parent_pos
var bottom_center = pos + half_size + parent_pos
But using "to_global()" is probably what you are actually looking for, so using that is fine too.
@cybereality func spawnObjects(objectsToSpawnArray): 0. var previousSpawnedObject 1. spawnPosition = $PlayerSpawnPosition.position 2. for i in objectsToSpawnArray: 3. var objectToSpawn = getObjectToSpawn() # return object with hierarchy on picture 4. if i == 0 : 5. objectToSpawn = spawnPosition 6. else : 7. var posToSpawn = previousSpawnedObject.getBottomCenter(previousSpawnedObject.get_node("Area2D/CollisionShape2D/Sprite")) 8. objectToSpawn.position = posToSpawn 9. objectToSpawn.position.x -= 180 # to place next object right below previous 10. previousSpawnedObject = objectToSpawn * 11. add_child(previousSpawnedObject)
So, i have LvlContainer as part of the level and when i try to instantiate this part in different from zero coordinates my spawned objects are scattered in different corrdinates , because they depend on the Mainscene position + the lvl container position. How can i make my spawnedObject to depend only on lvlcontainer position ?
!
- object to spawn !
- main scene !
Looks like you are using the sprite position (which is relative to several parents) rather than the MyArea2D position, which is what I think you want. Also be sure that you are calling add_child() on the object you want to spawn the items on (probably LvLCont).
var posToSpawn = previousSpawnedObject.getBottomCenter()
Add the "getBottomCenter" function to your root node of the spawned object. It doesn't need to accept any parameters, because it is replying with its own information.
LvLCont.add_child(previousSpawnedObject)
Hope that points you in the right direction.