Click here to Skip to main content
15,891,607 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
My player1 is not going to ironField. It's going to city directly.
How to make it go to different destinations ?

public Form1()
{
    player1.GoTo(ironField.x, ironField.y);
    player1.GoTo(city.x, city.y);
}
Entity player1 = new Entity();


public class Entity
{
    public void Initialize1()
    {

        Moving.Tick += new EventHandler(timer_Moving_Tick);
        Moving.Interval = 10;
    }

    private Timer Moving = new Timer();
    void timer_Moving_Tick(object sender, EventArgs e)
    {
        if (Xloc < x) //Xloc + width + 1
        {
            x -= speed;
        }
        if (Xloc > x)
        {
            x += speed;
        }

        float YlocB = Yloc - height - 1;
        if (YlocB < y)
        {
            y -= speed;
        }
        if (YlocB > y)
        {
            y += speed;
        }

        if (Xloc == x & YlocB == y)
        {
            Moving.Stop();
        }
    }

    float Xloc = 0, Yloc = 0;
    public void GoTo(float X, float Y)
    {
        Xloc = X; Yloc = Y;
        Moving.Start();
    }

Thank you.

What I have tried:

the code i put in ....
Posted
Updated 15-Nov-18 5:31am
Comments
F-ES Sitecore 15-Nov-18 9:47am    
You're not waiting for "player1.GoTo(ironField.x, ironField.y);" to end before calling "player1.GoTo(city.x, city.y);" so it's like the first call is never made.

You need to implement some kind of mechanism that allows your code to wait and respond to other events. You could possibly use an even in your timer method and that event is raised when the player has got to the destination. You could then use that event to move them to the second destination.
_Q12_ 15-Nov-18 9:56am    
Thank you! I was thinking on using events but i don't know how to implement Custom events. Can you make an example how you think it should work?

1 solution

In your Entity class define these

public delegate void MoveFinishedHandler();
public event MoveFinishedHandler OnMoveFinished;


Inside timer_Moving_Tick amend like so

if (Xloc == x & YlocB == y)
{
    Moving.Stop();

    if (OnMoveFinished != null)
    {
        OnMoveFinished();
    }
}


Kick your movement off like

player1.OnMoveFinished += Player1_OnMoveFinished;
player1.GoTo(ironField.x, ironField.y);


and define the event handler;

private void Player1_OnMoveFinished()
{
    // unregister the event as we no longer want to do anything when moving has finished
    player1.OnMoveFinished -= Player1_OnMoveFinished;
    player1.GoTo(city.x, city.y);
}


That will cause your Entity class to call the event handler when it has finished moving, and that event handler then starts the next movement.

If I was you though I'd probably create a list or array of target locations instead and define that with the player, and then code it so that when one movement ends it gets the next location in the list (if there is one) and moves to that too. That way you could do something like

player1.AddLocation(10, 10);
player1.AddLocation(20, 20);
player1.Move();

and the Move function would move to 10,10 and then 20,20.
 
Share this answer
 
Comments
_Q12_ 15-Nov-18 12:09pm    
Ok, is working - Thank You!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900