Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Relative positioning and getDeviceInfo — Gideros Forum

Relative positioning and getDeviceInfo

saeyssaeys Member
edited April 2015 in Game & application design
Hello, in my project I'm using Letterbox and relative positioning of objects in my scene and it works fine with different resolutions. For that I use the examples in the App Coding Easy blog.
But for a future app feature I would like to get a value, say, a 1000th of the current device screen width & height to position objects. But I don't seem to work it out.

A question related to this is the getDeviceWidth() function. It returns the correct values in output but it places the object roughly only two thirds of the expected position. What am I missing?
	devicewidth = application:getDeviceHeight()
	deviceheight = application:getDeviceWidth()
	dx = application:getLogicalTranslateX() / application:getLogicalScaleX()
	dy = application:getLogicalTranslateY() / application:getLogicalScaleY()
	local blacksquare = Bitmap.new(Texture.new("images/blacksquare.png", true))
	blacksquare:setAnchorPoint(0.5, 0.5)
	blacksquare:setPosition(-dx + devicewidth, -dy + deviceheight)
Example: for a iPhone 5 retina 1136x640 resolution (landscape), the object ends up at ca 758x427.

Thanks in advance
thomas

Comments

  • dreiko65dreiko65 Member
    edited April 2015
    By looking at your code I suppose you want a black square sitting always on the lower right corner of your app.

    If that is what you want here is the code I wrote for you.
    devicewidth = application:getContentWidth()
    deviceheight = application:getContentHeight()
    dx = application:getLogicalTranslateX() / application:getLogicalScaleX()
    dy = application:getLogicalTranslateY() / application:getLogicalScaleY()
     
    local blacksquare = Bitmap.new(Texture.new("images/blackSquare.png", true))
    	blacksquare:setAnchorPoint(0.5, 0.5)
    	blacksquare:setPosition(dx + devicewidth  - (blacksquare:getWidth() / 2), dy + deviceheight - (blacksquare:getHeight() / 2))
    	stage:addChild(blacksquare)
    I ve made some changes as you see.
    first of all I replaced the deviceWidth and deviceHeight with getContentWidth and getContentHeight.
    And because you use anchor point (0.5,0.5) you should subtract the half of your sprites width and height.

    those are the other corner positions

    Upper left corner:
    blacksquare:setPosition(-dx + (blacksquare:getWidth() / 2), -dy + (blacksquare:getHeight() / 2))
    Upper Right Corner:
    blacksquare:setPosition(dx + devicewidth - (blacksquare:getWidth() / 2), -dy + (blacksquare:getHeight() / 2))
    Lower Left Corner:
    blacksquare:setPosition(-dx + (blacksquare:getWidth() / 2), dy + deviceheight - (blacksquare:getHeight() / 2))
    Hope that helps
  • saeyssaeys Member
    Well, the black square is just a test. :-) I'm trying to get an additional, kind of "dynamic" way to place an object. I'm well aware of the use of getContentWidth/Height and to get deltas using getLogicalTranslate/Scale/X/Y in order to place an object in relation to top/left/bottom/right corner. What I'm trying to do is create a formula that divides the whole screen width and height respectively into a specified amount of units, like 1000. For example, where 0 is most left, and 1000 is most right.

    I thought getDeviceHeight() would seem to help, at least as I understand the reference, but apparently it returns different results in output and on the screen. Does anyone know the practical use of getDeviceHeight/Width?
  • ar2rsawseenar2rsawseen Maintainer
    Accepted Answer
    @saeys unfortunately if you are using letterbox, the top left coordinate may be negative. So you need to take that into consideration.

    You could make a wrapper to setPosition function, or make your own, setUnits function which takes into consideration dx and dy values, like from -dx and -dy to contentWidth+dx and contentHeight+dy
  • saeyssaeys Member
    I'll try another round, thanks.

    What about getDeviceHeight() and getDeviceWidth? Any usability examples on that?
Sign In or Register to comment.