Set a sprite at half the width of the screen or canvas or stage

When I move Sprite 1 to X ScreenWidth / 2 & Y ScreenWidth / 2 it’s not at all in the middle of the screen.
If I create a label to get the canvas width the label displays “Fill Container” which makes sense bc that’s the setting I have chosen. There is no getStageWidth, there’s only a drawingWidth which is how wide the pen tool or whatever would be so that’s not it.

Is there a simple way to make a sprite be in the middle of the canvas?

Hi @overshield,

I see. I just change the length and width Canvas to Relative size 100% which also will fill the containter and return values with digits.

And then we can use the same way to retrieve the middle of height/width which can be used to place the Sprite position.

2 Likes

when I set the width of Canvas to Relative 100% then canvas width value is 100% and it didn’t show stage when I click on the Preview button

This post is a year old, and I’m not sure if there are changes to how canvas and stages work, but let me tell you what’s working for me right now. I’m using the legacy (not beta, not drag-and-drop) interface. If you’re on the new beta drag-and-drop interface, this may not apply to you.

Set your canvas to whatever dimensions make it fit on the screen right. (Mine is set to fill container or 100% in both dimensions.) Set your stage’s size to either the actual pixels of your background image, or whatever size you want it. The stage will scale to fit the canvas.

All positioning of sprites is done relative to the pixels you set for the stage, NOT the actual pixels on the phone screen. So if you pack a 1000x500 px stage into a canvas on a small phone screen that’s only 500 x 250px, when you place sprites, you’ll place them on the 1000x500 grid, NOT 500x250. You don’t need to get the actual height of the canvas or stage. Use the stage dimensions you set. Note that sprites measure from the middle of the sprite, so if you want it to sit on the bottom of the stage, you’ll have to do a little math to get it there. :slight_smile:

4 Likes

The canvas component needs a lot of small improvements. Just adding that a sprite’s x/y position values are determined by the edges of the sprite rather than its center. I’ve put in a feature request for the values to be relative to the center so I’m hopeful that will change soon.

4 Likes

@tatiang are you sure about that? Because I’m

Are you sure about that? Because I’ve got working (legacy, not DND) code that says otherwise…

1 Like

Maybe it’s been fixed? I haven’t checked it in a week or more. I just know @jane was aware of it.

I’ve been doing WDC projects in drag and drop so I think that’s where I noticed it.

1 Like