Author Topic: Side Scrolling issue  (Read 3008 times)

Pratik Parikh

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 24
    • View Profile
Side Scrolling issue
« on: June 30, 2014, 02:46:07 PM »
Hi,
  I am making a side scrolling game in NGUI & I am attaching background one after another. But when the new background is added it flickers the screen for a sec & then it works fine. I tested the game in run mode by pausing the game & then moving game frame by frame. I realized that the new background transform position getting set at 0 for X,Y,Z. & then it takes the position where it needs to set.

How can i resolve this issue. Please check the below mentioned code

public class AddBg:MonoBehaviour
{
float main mainBgCounter;
public GameObject mainBg;

   public void AddMainBg()
    {
        Vector2 newPos = GetMainBgPos();
        mainBg = Resources.Load("mainBackground") as GameObject;
        mainBg.name = "MainBackground" + mainBgCounter;
       // mainBg.transform.position = Vector2.zero;
        Debug.Log("Maing bg new pos " + newPos);
        mainBg.transform.localPosition = newPos;
        NGUITools.AddChild(mainBackground, mainBg);

        mainBgCounter++;

    }

    public Vector2 GetMainBgPos()
    {
        Vector2 newPos;
        int childCount = mainBackground.transform.childCount;

        if (childCount > 0)
        {
            Transform lastChild = mainBackground.transform.GetChild(childCount - 1);
            newPos = new Vector2((lastChild.localPosition.x + mainBgWidth), 0);
           
        }
        else
        {
           
            leftx = (stageWidth - bgWidth) / 2;
            newPos = new Vector2(leftx, 0);
        }
        return newPos;
    }
}

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Side Scrolling issue
« Reply #1 on: July 01, 2014, 04:35:27 AM »
  1. mainBg.transform.localPosition = newPos;
Why are you changing the position of a prefab? You should be changing the position of the instantiated object returned by NGUITools.AddChild.