0 Favourites

How do I set an object's position to the borders?

  • We want the object to always start in the border. We need it to fit the size of the cellphone no matter what resolution we are using.

  • Anyone?

  • Take a look at the "Anchor" Behavior. Its meant for things like buttons or text that should always be in the same place.

  • I tried with this, but the object I want, should always stay on the edges of the window. It will work like an invisible wall, and must always be in that position no matter the screen size.

    The problem is that the scale of the screen changes, when it is getting much in the middle and white edges appear.

    Or do I improve the way I make the scale of the game, or simply adjust the object position and makes a bigger background.

  • Unless I've used anchor behavior wrongly, do not know if it will serve. Can someone explain to me how to do this?

  • Your probably using it wrong then... this is exactly the functionality your looking for. It guarantees the position of the object relative to the boundary of the layout regardless of scrolling or scaling. You only need to specify what corner (top,left - bottom,right , etc) to make sticky.

    I my case I have text in the top left corner of the screen but I use inner scaling so it can crop of some of screen and potentially hide some of the text. To ensure my text is always approximately 50 pixels (give or take) or so from the top left corner of the view-able layout I set the Anchor Property "Left edge" to "window left" and the "top edge" to "window top" ensuring that the top left corner of my text is fixed in its location to the top left corner of the view-able screen.

    I would play with it a little more. Or check out the manual. I'm sure this is what your looking for.

  • The anchor behavior only keeps an object in its position relative to the borders of the screen - it does not move it to the edge of the screen.

    Have a look in the manual for "viewport". You can place the object using that system expression. For example, if you want to place an object touching the left edge of the viewable screen:

    set Sprite.X to ViewportLeft("HUD" + Sprite.Width * 0.5) .... where "HUD" is the layer name of you HUD.

    Have a look at this demo I put together for an example of this.

  • I will test the hint here, then inform the result, hope this helps someone else ^^

    Thanks Colludium, is there any other topic that I can use to tell me more about it?

  • Always happy to help! I found these forums extremely helpful when I first started (I'm hardly an expert now!) so it only seems fair. Feel free to ask away when you get stuck.

  • Construct 3

    Buy Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Buy Now Construct 3 users don't see these ads
  • Colludium yes, this forum is a great help for any developer. And people like you make this forum grow further, thanks ^ ^.

    How can I adjust the size of my sprite according to the screen size? Could anyone help me with this? ^^

  • vitorfgd, that is a very complicated subject and you need to be sure of exactly what it is that you want to achieve.

    1. Do you want the sprite to take up exactly the number of pixels on the screen irrespective of screen resolution? For example, a 128x128 sprite will look ok on an 800x480 screen but it would look really small on a full HD 1920x1080 screen - and the apparent difference in size would be very striking if the screen dimensions were about the same.

    2. Do you want the sprite to take up the same proportion of the viewable screen length/width irrespective of screen resolution or aspect ratio? C2 handles this option very well with its Scale Outer scaling option (and the others, but they are less on mobile usable IMO).

    The common advice is to draw your game for the largest aspect ratio you can expect (16:9 or 16:10) and then ensure that your background fits smaller aspect ratios to good effect. There are some great tutorials and threads on this, so do a quick search and have a look through. It's not a decision you can just glibly ignore at the start and then hope to fix later in your development. It also depends on your export target - I understand that C2 does scaling very well so, providing the sprites have the detail, you could draw all of your sprites and layout for 800x480 for example, with each sprite drawn at 1/2 scale, so that if there is any upscaling to be done (full HD / retina screen) then the upscale of your sprites can be done by adding that detail rather than by interpolating pixels. Again, plenty of tutorials ect will cover this better than I can. Personally I have scaled my last few apps to a middle of the road aspect ratio that is better suited for iOS - something like 960x640 or 1136x640 - both are ok for android and iOS and the scale outer can make some exports look longer and some look wider, but I'm personally happy with the average appearance. Whatever works for you...

  • I suppose I didn't answer your question exactly! Looking at my list above, if you want to achieve option 1 then you need to obtain the canvas size and scale your sprites so that they will appear at 1:1 resolution on screen, which means they will need to be scaled in the layout at run time according to the screen resolution - because C2 will try and then scale them so they appear in true proportion to the layout on screen.

    For example, let's say you have a 64x64 sprite that you want to have rendered as 64 pixels on screen. If your original layout is 640x640 then C2 will try and draw the sprite so it appears to be 1/10 the width/height of the screen. If the screen resolution is 1280x1280 then you would need to scale the sprite to 1/2 its width/height at run time so C2 would then draw it to appear 1/20 the screen size - which would still be 64x64. Add in the complication of different aspect ratios and you'll begin to see that this is not a good place to go - because you haven't even started you game mechanics yet!

    I would advise steering clear of this to begin with and I can't think of many game options where this would be an attractive proposition (there are some, though...).

  • Colludium Explaining better, in my problem, I have 4 transparent objects in which each will be positioned on the edges (top, bottom, left, right) screen. With the solution you taught me previously, and which by the way works excellently, I can determine that the object reallocate according to the window size, but it will eventually become smaller than her, because its size does not change.

    I would like to change its size according to the size of the window (in this case the size of the smartphone).

  • Ah OK - you want the object to fill the screen:

    Set object.Width to ViewportRight("HUD") - ViewportLeft("HUD") and do similar to the object's height, assuming it's on the HUD layer and depending on whether it's width or height you're interested in.

    Hope that's what you're after?

  • Yes, it's exactly that I'm talking about. But, when i need to set height I feel lost. How i could do that with height?

    I tried some ways, but the object took disproportionate to the size I wanted (the width worked normal only for objects that I wanted to change the height).

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)