Multiple Animations on AbsoluteLayout

AgredoAgredo USMember ✭✭

Hello Guys!

I want to develop a simple game with some random npc's spawning every second for example .

The NPC's (tiny squares) should walk over the gray way.

Here ist my Code:

` public MapPage()
{

        InitializeComponent();

        myRandom = new Random(3);

        for (int i = 0; i < 10; i++)
        {
            CreateRandomNPC();
        }
    }

    public void CreateRandomNPC()
    {
        var rnd = myRandom.Next(1, 3);

        if(rnd==1)
        {
            CreateNPC(new Rectangle(100,100,10,10));
            MoveNPC(GameMapAbsolutLayout.Children.Last(),1);
        }
        else if (rnd == 2)
        {
            CreateNPC(new Rectangle(1000, 100, 10, 10));
            MoveNPC(GameMapAbsolutLayout.Children.Last(), 2);
        }
        else if (rnd == 3)
        {
            CreateNPC(new Rectangle(220, 100, 10,10));
            MoveNPC(GameMapAbsolutLayout.Children.Last(), 3);
        }
    }


    public void CreateNPC(Rectangle StartPosition)
    {
        var npc= new BoxView() { Color = Color.Orange, HeightRequest = 10, WidthRequest = 10 };
        AbsoluteLayout.SetLayoutBounds(npc, StartPosition);
        GameMapAbsolutLayout.Children.Add(npc);
    }`

My problem is, that only two npc's were built.

Thanks

Christian

Best Answer

Answers

  • AgredoAgredo USMember ✭✭

    @JimBennett said:
    The second parameter to Random.Next is an EXCLUSIVE upper bound, so by calling myRandom.Next(1, 3) your random number will only ever be 1 or 2.

    https://msdn.microsoft.com/en-us/library/2dx6wyd4(v=vs.110).aspx

    Try myRandom.Next(1, 4)

    Thanks, This solvs the Pproblem, that not all three NPC's was built. But now i only get three not randomly ten like I defined in my For Loop:

    for (int i = 0; i < 10; i++) { CreateRandomCustomer(); }

  • JimBennettJimBennett GBXamarin Team, Insider, University, Developer Group Leader ✭✭✭✭

    Are you only getting three, or are you getting ten but positioned over the top of each other in 3 locations? Can't see the rest of your code so I can't tell where in the absolute layout they are being positioned.

  • AgredoAgredo USMember ✭✭

    So here is my MoveNPC Method

    public void MoveNPC(View NPC, int NPCStartIndex) { if(NPCStartIndex == 1) { var task = NPC.TranslateTo(1000, 0, 10000, Easing.Linear); } else if(NPCStartIndex == 2) { NPC.TranslateTo(-700, 0, 10000, Easing.Linear); //var animate = new Animation(d => NPC.TranslationX = d, 0, -700, Easing.Linear); //animate.Commit(NPC, "Test", 60, 10000, Easing.Linear); } else if (NPCStartIndex == 3) { var animate = new Animation(d => NPC.TranslationY = d, 0, 500, Easing.Linear); animate.Commit(NPC, "Test", 60, 10000, Easing.Linear); } }

    Your right all other are on top on ech other :disappointed: I have to make a delay between the starts of ech Animation. is this possible?

Sign In or Register to comment.