Something FISHY about localToContent or Groups in Scroll Function

Hi Everone, 

This bug is really getting on my nerves. I cannot for the life of me work out what is going wrong. Can someone PLEASE help. I have taken the time to extract the code and put it into a working main.lua program with image file to make it easy for you to replicate error AND because I REALLY need your help. try running the zip file

What happens is the first time the scroll creates a new image for the background everything seems to be fine. you can see the red dot aligns with the new image join. And sets the new images to be the furthest right image to be checked for position. But after this first assignment, it all goes pear shaped and the join is not where it should be and cascades to multiple images and failure. 

I cannot see where my error is and i have set aside and picked this back up a few times. I am guessing its something simple.  I think i am using the localToContent function and groups correctly, but thats where i think the error is. Any ideas?

Im using this scroll approach as i want to have multiple parallax layers and the ability to alter the backgrounds images and vary their width dynamically as required.

cheers

Kadlugan (online2)

[lua]

local scroll = {}

local background = display.newRect( display.contentCenterX,display.contentCenterY, display.contentWidth, display.contentHeight)

background:setFillColor( 1,1,1 )

– I created a scroll system with layers that each have their own parallax setting.

– This is just a chunk of code with one layer to work with enterframe listner below

scroll.layers = {}

scroll.layers[1] = {}

layer = scroll.layers[1]

layer.objects = display.newGroup()

layer.distanceRatio = 1.2

local object = display.newImage(  “layer_01_2048x1546.png”,0,0 )

layer.objects:insert(object)

layer.rightObject = object

scroll.parallaxEnabled = true

scroll.shouldMove = true

local layers = scroll.layers

local screenScrollBuffer = 20

local rightThreshold = display.contentWidth + screenScrollBuffer

local _speed = 5

scroll.enterFrame = function ( self, event )

if self.shouldMove then

     if self.parallaxEnabled and (layers ~= nil) then

        for i=1,#layers do

    local layer = layers[i]

         if layer ~= nil then

          – scroll the layers according to parallax settings

     layer.objects.x = layer.objects.x - _speed * layer.distanceRatio

     – ########### ERROR BELOW HERE, i think #############

     – if rightObject in layer and its near edge of screen create the next scroll image

     local rightObject = layer.rightObject

     if rightObject ~= nil then

      local rightObjectX, rightObjectY = rightObject:localToContent( 0, 0 ) – 0,0 centre of the object

      – local rightObjectX = rightObject.x  – im pretty sure i need to convert the group to screen coords

      local rightMostEdge = rightObjectX + 0.5 * rightObject.contentWidth

      if rightMostEdge <= rightThreshold then

       local newRightObject = display.newImage(  “layer_01_2048x1546.png”)

      

       – position image

       newRightObject:translate( rightMostEdge + 0.5 * newRightObject.contentWidth, 0 )

      

       --[[Onscreen Test Marker]] local circle = display.newCircle( layer.objects, rightMostEdge, display.contentHeight/2, 10 ); circle:setFillColor( 1,0,0 ); circle:toFront( )

      

       – assign right image pointer

       layer.rightObject = newRightObject

       – self:addParallaxObject(i, newRightObject)

       layer.objects:insert(newRightObject) – group method

      end

     end

     – ########### ERROR ABOVE HERE, I think #############

    end

   end

     end

end

end

Runtime:addEventListener( “enterFrame”, scroll )

[/lua]

Fixed here: 

Note: You really need a config.lua in that example and I hope you have one in your project or you’ll get indeterminate behavior later.

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/03/scrollError.zip

http://raw.githubusercontent.com/roaminggamer/RG_FreeStuff/master/AskEd/2016/03/scrollError/scrollError/main.lua

@roaminggamer,

 

Thanks so much for the quick response.  Really REALLY appreciated. 

 

Im going through the code to see what it was that was causing my error.  I can see that you are first inserting the new background object into the group first then manipulating it within that coordinate system.  Makes sense.   I still quite don’t know what my error was doing it the other way around? Maybe just making things difficult for myself.  

 

With your solution the only line i might modify is line 62, which means if i want to use a different image with different width in scroll, eg changing the scenery, i can position according to half the new image width. Im pretty sure this will work for all new cases.  (obviously standard background widths fix this too) 

 

[lua]

– newRightObject.x = layer.rightObject.x + rw

newRightObject.x = layer.rightObject.x + layer.rightObject.contentWidth/2 + newRightObject.contentWidth/2  – ie half the current background + half the new background width

[/lua]

Fixed here: 

Note: You really need a config.lua in that example and I hope you have one in your project or you’ll get indeterminate behavior later.

http://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/03/scrollError.zip

http://raw.githubusercontent.com/roaminggamer/RG_FreeStuff/master/AskEd/2016/03/scrollError/scrollError/main.lua

@roaminggamer,

 

Thanks so much for the quick response.  Really REALLY appreciated. 

 

Im going through the code to see what it was that was causing my error.  I can see that you are first inserting the new background object into the group first then manipulating it within that coordinate system.  Makes sense.   I still quite don’t know what my error was doing it the other way around? Maybe just making things difficult for myself.  

 

With your solution the only line i might modify is line 62, which means if i want to use a different image with different width in scroll, eg changing the scenery, i can position according to half the new image width. Im pretty sure this will work for all new cases.  (obviously standard background widths fix this too) 

 

[lua]

– newRightObject.x = layer.rightObject.x + rw

newRightObject.x = layer.rightObject.x + layer.rightObject.contentWidth/2 + newRightObject.contentWidth/2  – ie half the current background + half the new background width

[/lua]