C#Enemy在Windows窗体中不关注播放器(不是XNA)

时间:2014-11-21 19:39:32

标签: c# sprite

更新

我设法自己解决了答案,发生的事情是我的移动功能只是在玩家在屏幕上移动时注册玩家原始静态位置而不是动态坐标。我所做的就是创造一个“上帝”。类公开存储玩家当前的X和Y坐标,然后参考“上帝”。我的敌人的移动功能中的类坐标。

现在,敌人不仅能够检测到玩家何时进入其中,还会跟随玩家相应的方向改变方向,直到它无法看到''他们了:D

如果有人想查看更新的代码,请询问:)

我试图创建一个包含玩家和敌人精灵的程序,但我对“敌人”的代码有点麻烦。这里的大多数编码问题都涉及我不使用的XNA,所以我想知道我是否能得到一些帮助。

基本上,当玩家在一定距离内移动时,敌人的移动功能应该激活,只要他们进入“检测区域”,它就会向玩家位置移动。敌人的精灵也有每个方向的步行动画,它应该根据它是否正在移动而翻转帧。

移动功能在计时器内,因此每次打勾都会更新。我有两个单独的函数来更新它的位置,一个用于根据从移动函数接收的数据在Form中物理地改变它的坐标,另一个用于从我的精灵表中更新当前帧。

问题是敌人不会移动任何东西,如果我摆脱它的检测和跟随玩家的代码,而只是将其移动到屏幕上,它完全正常。但不幸的是,这并不是我想要实现的目标。

代码对我的播放器精灵来说非常好(虽然它是由用户和键盘按键控制的),所以我不确定我做错了什么。

我认为问题在于运动功能,但我不确定这个问题到底是什么。

这是我的相关功能代码:

public void Movement() //This function is in a timer, and determines which direction the sprite will move and face
    {
        //The sprite rectangle is the position of the player sprite, and detection is the area where the enemy would 'see' the player if they entered it
        sprite = new Rectangle(charX - 1, charY + 1, charWidth * 2 + 2, charHeight * 2 + 2);
        Detection = new Rectangle(curX - 25, curY - 25, Enemy.Width * 6, Enemy.Height * 6);

        if (Detection.IntersectsWith(sprite)) //Checks to see if the player is within range
        {
              //Moves the enemy based on the players position and changes direction accordingly
            if (curX > charX) 
            {
                curDirection = RIGHT;
                dX = -1;
                dY = 0;
                numUpdates = 0;
                curAnimation = MOVING;
            }
            else if (curX < charX)
            {
                curDirection = LEFT;
                dX = 1;
                dY = 0;
                numUpdates = 0;
                curAnimation = MOVING;
            }

            if (curY > charY)
            {
                curDirection = UP;
                dY = -1;
                dX = 0;
                numUpdates = 0;
                curAnimation = MOVING;
            }
            else if (curY < charY)
            {
                curDirection = DOWN;
                dY = 1;
                dX = 0;
                numUpdates = 0;
                curAnimation = MOVING;
            }
        }
           //If not within range, don't move the sprite
        else
        {
            dY = 0;
            dX = 0;
            curAnimation = FINISHED;
        }     
    }

这些是移动敌人并更改当前帧的更新功能

        public void Follow() //Physcially moves the enemy across the form
    {         

        int xMod = 0;
        int yMod = 0;

        if (dX != 0)
            xMod = dX / Math.Abs(dX) * 2;

        if (dY != 0)
            yMod = dY / Math.Abs(dY) * 2;


        //Moves the sprite across the x and y axis
        curX += xMod;
        dX += -xMod;

        curY += yMod;
        dY += -yMod;

    }

       public void UpdateFrame(int direction = DOWN)
    {
        numUpdates++;

        switch (curAnimation)
        {
            //Stops the animation if the sprite stops moving
            case FINISHED:
                curFrame = 0;
                break;

            //Moves to the next frame when movement is found
            case MOVING:
                curFrame = (curFrame + 1) % 3;
                Follow();

                //check if done animation
                if (dX == 0 && dY == 0) curAnimation = FINISHED;
                break;
        }
    }

如果您需要更多代码,请咨询任何帮助:)

0 个答案:

没有答案
相关问题