[Solved]Problem with position when creatin an object

Im trying to create an object when i pressed a key with this code:

it works at the start when im not moving the player yet

but when i move and press the key again the object is not created at the center


i was thinking the problems is something with the layers or camera of the “player” object
bc if i create something at 0,0 (top, left screen) is fine and dont move:

and yes, both of them are in UI Layer and player in base layer
help :c

Hi Karm369 and welcome.

Unfortunately I don’t understand your pictures as in which bit is the player sprite but someone else might. So I can’t solve your problem, but can make some suggestions.

It’s a good idea to use Trigger once with the z key pressed condition. Because pressing the z key takes time and you have to ask yourself how many times do you want the InventoryBackG object to be created during that time? Probably just once.

The other thing is if you have the origin point of InventoryBackG in the center then you can say Create InventoryBackG at position Player.Center(X), Player.Center(Y) and delete your third action.

1 Like

I believe your UI layer’s position is fixed. I’m just guessing here so I could be wrong. But here’s what I think is happening.

I’m assuming you put your camera to follow the player.
So at the start of the scene, if your game is set to let’s say 800x600, and the player starts at the center, that would be at coordinates x=400 and y=300. On the UI layer, it would correspond to the same coordinates. So if you create your object at player’s position, it matches.

But if you move your player to the bottom right, around x=700 and y=500. The camera would register the player as still in the center. But since the coordinates on the UI layer is fixed, x=700 and y=500 is still bottom right. And that’s where the object appears. If you go outside the starting 800x600 boundaries, you probably wouldn’t even see the created object appear.

Instead of the player position. I might suggest setting a reference point or an invisible object that’s placed on the UI layer where you would create the object.

sorry for the confusion, i didn’t explain it very well, probably i was to tired late night,

the problem was that (1pic) the square sprite was created right, (is over the player), then i just have to change the width and height to be like a Menu

but (2pic) when i move and then create again the menu, the position was not right,

i solve it by
1 the sprite actually was and png, thats why doesnt had center points (i could us sprite.width()/2 but didnt work to)
so, i created a sprite and give it the png

and this code:


To Levio: you were right, i noticed that using the x,y of player on the UIlayer was doing that
So i changed the playerPosition as reference to CameraCenter and done.

Ty, and sorry if i didnt explain well, still learning (english too xd)

1 Like

All good! Glad to hear you got it sorted out. Cheers!

1 Like