Make sure whole object is below Y value?

I’m building this game below and I’m doing a check to make sure all the objects are below the dotted line. The only issue with that is  the cords are pulled from the center of the object. So the y value isn’t where I want it to be. What would be the best way to confirm that the whole object is below the line?

ZoSSJw7.png
 

Hi @dmglakewood,

You can use the “object.contentBounds” property for this, as follows:

[lua]

local box = display.newRect( 100,100,200,200 ) ; box:setFillColor(255)

local function boundCheck()

    local bounds = box.contentBounds 

    print("xMin: "… bounds.xMin) – xMin: 100

    print("yMin: "… bounds.yMin) – yMin: 100

    print("xMax: "… bounds.xMax) – xMax: 150

    print("yMax: "… bounds.yMax) – yMax: 150

end

transition.to( box, { time=20000, rotation=360 } )

timer.performWithDelay( 1000, boundCheck, 0 )

[/lua]

Notice how the values change as the box rotates. Basically, it’s just giving you the outer bounds of a theoretical rectangle drawn around the object.

If you’re using physics, the approach could be different… you could use a big sensor for the region the object isn’t supposed to be within (above the line) and ensure that all objects have exited that region.

Hope this helps!

Brent

Awesome thanks for the tips! I knew there had to be an easier way then what I was about to do :slight_smile:

Here is the image of the rock that is still above the line. I’m using the bounds method to make sure the y value is below the y value of the line.

qMkOY12.png
 

local bounds = innerGroup[blocks].contentBounds if(bounds.yMin \< levelGoalBar.y) then levelOver = false break end

I have this code loop through all the items on the screen. If any objects are above the line the level goes on. If all items are below the line the level is over. The image above threw a level over response even though the yMin is still above the bar. Any ideas on this?

I’d have to see how you’re structuring this “innerGroups[blocks]”. There must just be some tiny issue with how you’re referencing the objects…

Brent

Hi @dmglakewood,

You can use the “object.contentBounds” property for this, as follows:

[lua]

local box = display.newRect( 100,100,200,200 ) ; box:setFillColor(255)

local function boundCheck()

    local bounds = box.contentBounds 

    print("xMin: "… bounds.xMin) – xMin: 100

    print("yMin: "… bounds.yMin) – yMin: 100

    print("xMax: "… bounds.xMax) – xMax: 150

    print("yMax: "… bounds.yMax) – yMax: 150

end

transition.to( box, { time=20000, rotation=360 } )

timer.performWithDelay( 1000, boundCheck, 0 )

[/lua]

Notice how the values change as the box rotates. Basically, it’s just giving you the outer bounds of a theoretical rectangle drawn around the object.

If you’re using physics, the approach could be different… you could use a big sensor for the region the object isn’t supposed to be within (above the line) and ensure that all objects have exited that region.

Hope this helps!

Brent

Awesome thanks for the tips! I knew there had to be an easier way then what I was about to do :slight_smile:

Here is the image of the rock that is still above the line. I’m using the bounds method to make sure the y value is below the y value of the line.

qMkOY12.png
 

local bounds = innerGroup[blocks].contentBounds if(bounds.yMin \< levelGoalBar.y) then levelOver = false break end

I have this code loop through all the items on the screen. If any objects are above the line the level goes on. If all items are below the line the level is over. The image above threw a level over response even though the yMin is still above the bar. Any ideas on this?

I’d have to see how you’re structuring this “innerGroups[blocks]”. There must just be some tiny issue with how you’re referencing the objects…

Brent