我的播放器1 - 如何让它去不同的目的地? [英] My player1 - how to make it go to different destinations ?

查看:64
本文介绍了我的播放器1 - 如何让它去不同的目的地?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的播放器1不会使用ironField。它直接去城市。

如何前往不同的目的地?



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 ....

推荐答案

在你的实体中class定义这些



In your Entity class define these

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





内部timer_Moving_Tick修改如此





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);
}





这将导致你的Entity类在完成移动时调用事件处理程序,然后该事件处理程序开始下一个动作。



如果我是你,虽然我可能会创建一个列表或目标位置数组,然后用播放器定义,然后编码这样,当一个动作结束时,它会获得列表中的下一个位置(如果有一个)并移动到那个位置。这样你可以做类似的事情。



player1.AddLocation(10,10);

player1.AddLocation(20,20);

player1.Move();



和Move函数将移动到10,10然后是20,20。



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.


这篇关于我的播放器1 - 如何让它去不同的目的地?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆