Scrolling background

Hi All,

I am Playing around with CocosSharp. Can anyone direct me to a sample of Scrolling background

Thanks

Posts

  • RamiTabbaraRamiTabbara AUMember, Xamarin Team Xamurai

    Hi Mazen,

    There's a new sample here that makes use of CCParallaxNode to perform scrolling of the background. However, keep in mind that the sample requires Xamarin.Forms.

    Alternatively, you can check out the parallax tests from our test suite here which can also give you an idea on how to achieve a scrolling background.

  • BenLevy.4265BenLevy.4265 USMember ✭✭

    Mazen,

    This code bit may help. This is just from my testing I am doing. Works though.

    Just call AddScrollingBG() and StartScheduling() from your AddedToScene().

    Hope this will give you a good start.

    This will scroll from right to left.

    '''

        CCSprite bg1, bg2;
    
        void AddScrollingBG()
        {
            bg1 = new CCSprite("backgrounds/scrollingbg_1");
            bg1.AnchorPoint = CCPoint.AnchorLowerLeft;
            bg1.Position = new CCPoint(0.0f, 0.0f);
    
            bg2 = new CCSprite("backgrounds/scrollingbg_2");
            bg2.AnchorPoint = CCPoint.AnchorLowerLeft;
            bg1.Position = new CCPoint(bg1.ContentSize.Width, 0.0f);
    
            AddChild(bg1);
            AddChild(bg2);
    
    
        }
        void StartScheduling()
        {
            Schedule(t => MoveBackground(t));
    
        }
        void MoveBackground(float t)
        {
            const float delta = 500.0f;
    
            // Our width is smaller then the width of the image.  
            // This eliminates the stiching we get from scaling the image.
            const float bgWidth = 2040.0f;
    
    
            if (bg1.Position.X < bg2.Position.X)
            {
                bg1.Position = new CCPoint(bg1.Position.X - (delta * t), 0.0f);
                bg2.Position = new CCPoint(bg1.Position.X + bgWidth, 0.0f);
            }
            else
            {
                bg2.Position = new CCPoint(bg2.Position.X - (delta * t), 0.0f);
                bg1.Position = new CCPoint(bg2.Position.X + bgWidth, 0.0f);
    
            }
    
            // reset if offscreen;
            if (bg1.Position.X < (-1 * bg1.ContentSize.Width))
            {
                // move it in front of bg2
                bg1.Position = new CCPoint(bg2.Position.X + bgWidth, 0.0f);
            }
            else if (bg2.Position.X < (-1 * bgWidth))
            {
                bg2.Position = new CCPoint(bg1.Position.X + bgWidth, 0.0f);
            }
        }
    

    '''

  • mazenlhmmazenlhm LBUniversity

    Hi,

    Rami Tabbara And Ben Levy ..Thank you both for your repsonse

Sign In or Register to comment.