Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Problem with function Detecting x position within table
Started by jacksmack148 Dec 26 2018 03:57 AM

- - - - -
No replies to this topic
table scope
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

jacksmack148

[GLOBAL: userInfoPane.html]
jacksmack148
  • Observer

  • 3 posts
  • Corona SDK

Hi i'm trying to create a level in a module which creates 5 pairs of pipes on the screen with a loop ( with a gap in the middle filled with an object called ice)

 

 

 two pipe objects with a 3rd ice object within the middle  -- >         ------|  ice   |-------

 

 

main.lua

_L = display.screenOriginX                        --  Left
_R = display.viewableContentWidth - _L   --  Right

 

bossObstacles.lua                ( the return value of this module is called group)

local iceTable = {}

    local no = 0
            
            local pipeNo = 0

            for i=1, 5 do

            
            local groupSingle = display.newGroup( )

            pipeLFile = fileL[math.random(#fileL)]
            pipeRFile = fileR[math.random(#fileR)]

            local leftPipe = display.newImageRect( groupSingle, "images/pipes/" .. pipeLFile .. ".png", 400, 44 )
            local rightPipe = display.newImageRect( groupSingle, "images/pipes/" .. pipeRFile .. ".png", 400, 44 )
            local ice = display.newImageRect( groupSingle, "images/pipes/River/RiverTrial.png", 100, 25 )
            local rand = math.random(-measurements.pipeWidthXRandomness, measurements.pipeWidthXRandomness)   

            table.insert( iceTable, ice ) -- Add to table

               function groupSingle:physicsNow()
                    physics.addBody( leftPipe, "static", { bounce = 0, friction = 0 })
                    physics.addBody( rightPipe, "static", { bounce = 0, friction = 0 })
                    physics.addBody( ice, "static", { bounce = 0, friction = 0 })
                end



               function groupSingle:downNow()
                    transition.to( leftPipe, {y=_aCH, time=1500, delta=true} )
                    transition.to( rightPipe, {y=_aCH, time=1500, delta=true} )
                    transition.to( ice, {y=_aCH, time=1500, delta=true} )
                end






                function groupSingle:sideBounce()

                    for i=1, 5 do
                        print( iceTable[i].x )

                        if iceTable[i].x < _L then


                    transition.to( group[i], { x=500, time=5000, delta=true} )


                    elseif iceTable[i].x > _R then

                    transition.to( group[i], { x=-500, time=5000, delta=true} )

    

                        end

                    end

                end



                function groupSingle:startPMoving()
                    no = no + 1
                    local leftOrRight = { 1 , 2 , 1, 2, 1}





                        if ( leftOrRight[no] == 2 ) then

                        transition.to( group[no], { x=-300, time=5000, delta=true} )



                        print( "RIGHT" )

                        elseif ( leftOrRight[no] == 1 ) then

                       transition.to( group[no], { x=300, time=5000, delta=true} )



                        end
   

                end




            leftPipe.anchorX = 1
            rightPipe.anchorX = 0
            ice.anchorX = 0
            --ice.anchorY = .5


            leftPipe.y = ( i * 60 ) - _aCH
            rightPipe.y = leftPipe.y
            leftPipe.x = _CX - measurements.gap + rand
            rightPipe.x = _CX + measurements.gap + rand--(leftPipe.width / 2.5)--- 37.5
            --leftPipe:setFillColor(.34,.23,0)
            --rightPipe:setFillColor(0,2,0)
            leftPipe:scale(1,1)
            rightPipe:scale(1,1)
            ice.x = _CX - measurements.gap + rand--leftPipe.width
            ice.y = leftPipe.y



            ice.name = "ice"
            ice.alpha = .9
            

            leftPipe.name = "pipe"; rightPipe.name = "pipe"

            

            --groupSingle.anchorX = .5




            group:insert( groupSingle )
            

            

            groupSingle:physicsNow()
            groupSingle:downNow()
            groupSingle:startPMoving()


                if i == 5 then
                    timer.performWithDelay( 20, groupSingle.sideBounce, -1)
                end
                
            end

The problem i'm having is that  the function groupSingle:sideBounce() when grabs the position of the ice object within the iceTable (for example iceTable[2].x = 46) it gets the original ice position is was when first created and can't grab the new position which is different as the all five ice objects (and the pipes) are transition left or right when they spawn.

 

Is there a way you have the function grab the new x position of all the ice objects within the table so that it can detect when the ice has gone over the side of the screen, transitoning it back the other direction.

 

 

If you can't understand what i'm saying just comment and i'll try to make it more clear.

 

Thank you in advance

 

 

 

 

 




[topic_controls]
[/topic_controls]

Also tagged with one or more of these keywords: table, scope