How do you count the number of objects on one side of the screen?

I’m trying to return the count of objects on the right half of the screen, but it keeps saying my variable rightSideCount is a boolean (false) instead of a number value, even if I globally declare local rightSideCount = 0 up above.

local rightSideCount = 0 ..bla bla bla local stage = display.getCurrentStage() rightSideCount = stage.numChildren \> centerX print(rightSideCount)

Obviously I have the syntax wrong, but not sure what it should be. Any ideas?

Thx

You need to loop through the individual objects on screen, and test each display object individually.

This code isn’t exactly what you need (I just hacked up something I use elsewhere), but it’s in the right direction

[lua]

            local stage = display.getCurrentStage()

            for index, value in pairs(stage) do
                if( type(value) == “table” ) then
                    if( value.x ~= nil ) then
                        if( value.x > centerX ) then   

                            rightSideCount = rightSideCount +1
                        end
                    end
                end

[/lua]

Still very confused …

When I used “stage” it just returned the x coordinate of the topmost layer in the z-index (a divider line in the center of the screen).

Am I supposed to replace “stage” with my table’s name?

I never used display.getCurrentStage() …

Most people create a new display.newGroup(), then insert items into that…

It’s typically that new group you create that something like this loops on.

It’s alos much cleaner to remove everything… You just display.remove that group, and don’t have to deal with all the items (same with moving it, scaling it, etc)

You need to loop through the individual objects on screen, and test each display object individually.

This code isn’t exactly what you need (I just hacked up something I use elsewhere), but it’s in the right direction

[lua]

            local stage = display.getCurrentStage()

            for index, value in pairs(stage) do
                if( type(value) == “table” ) then
                    if( value.x ~= nil ) then
                        if( value.x > centerX ) then   

                            rightSideCount = rightSideCount +1
                        end
                    end
                end

[/lua]

Still very confused …

When I used “stage” it just returned the x coordinate of the topmost layer in the z-index (a divider line in the center of the screen).

Am I supposed to replace “stage” with my table’s name?

I never used display.getCurrentStage() …

Most people create a new display.newGroup(), then insert items into that…

It’s typically that new group you create that something like this loops on.

It’s alos much cleaner to remove everything… You just display.remove that group, and don’t have to deal with all the items (same with moving it, scaling it, etc)