Xamarin SpriteKit用键运行动作

时间:2016-04-17 09:48:27

标签: ios xamarin sprite-kit

我正在使用SpamaKit和Xamarin。我目前正在制作类似于飞鸟的游戏,除了管道还可以“扼杀”并像这样杀死你。因此,在任何给定时间,我的管道精灵可以运行两个动作,一个用于沿X轴移动(当玩家移动时),一个用于沿Y轴移动(关闭管道)。当玩家停止沿X轴移动时,我想要停止水平移动它们的管道的运行动作,但仍然保持咀嚼动作。虽然我在为行动设置密钥时遇到了麻烦。目前,这就是我所拥有的。

        public override void TouchesEnded (NSSet touches, UIEvent evt)
    {
        base.TouchesEnded (touches, evt);
        //TODO: Optimize by removing action by key instead of all of them. Figure out how to do this and impliment.
        //This needs to be done before refining chomp as it will flow into the chomp logic
        //by not stopping a chomp mid-way because the player lifted their finger. 

        poletop.RemoveAllActions();
        polebottom.RemoveAllActions();
        pole2top.RemoveAllActions ();
        pole2bottom.RemoveAllActions ();
        background.RemoveAllActions ();
        //restarting the chomping action if it was stopped before.
        chomped_return (false);
        chomped_return (true);
    }

所以我基本上停止所有操作,然后只重启chomp如果它正在运行(在chomped_return中重新启动)。

这不是很有效,并且在停止和开始时也会导致游戏中的一些延迟。

沿X轴开始动作的代码(玩家移动)。这是SKAction,我想拥有钥匙,所以我可以阻止它并且它一个人。 resetpoles是完成功能。

        SKAction movebottompole;
        SKAction movetoppole;
        movebottompole = SKAction.MoveToX (edgebottom.X, flTime);
        movetoppole = SKAction.MoveToX (edgetop.X, flTime);

        polebottom.RunAction (movebottompole, resetpoles);
        poletop.RunAction(movetoppole, resetpoles);

按下播放器是管道的即时传送端口,但这是运行以启动管道沿Y轴返回其原始位置的动作的代码。这是设置我不想在完成之前停止的操作。

        public void chomped_return(bool blFirstPole)
    {
        //define our two actions
        SKAction topreturn;
        SKAction botreturn;
        //define our floats for the time calculation
        float flTime = 0;
        float flMoveSpeed = 750;
        float flDistance = 0.0f;
        if (blFirstPole == true) 
        {
            flDistance = (float)polebottom.Position.Y;
        } 
        else if (blFirstPole == false) 
        {
            flDistance = (float)pole2bottom.Position.Y;
        }
        //calculate time based on distance and vector units/second desired.
        flTime = flDistance / flMoveSpeed;
        //setup our moveto actions and use the time calculated above. Start the action.
        topreturn = SKAction.MoveToY (750.0f, flTime);
        botreturn = SKAction.MoveToY (0.0f, flTime);
        if (blFirstPole == true) 
        {
            poletop.RunAction (topreturn);
            polebottom.RunAction (botreturn);
        }
        else if (blFirstPole == false) 
        {
            pole2top.RunAction (topreturn);
            pole2bottom.RunAction (botreturn);
        }

        return;

    }

变量blFirstPole用于确定我们是否在第一组杆或第二组上运行动作(因为一次可以在屏幕上有两个)

非常感谢任何帮助,如果您需要更多信息,请告诉我们!

由于

1 个答案:

答案 0 :(得分:1)

您可以使用RemoveActionForKey删除具有特定密钥的操作。

https://developer.xamarin.com/api/member/MonoTouch.SpriteKit.SKNode.RemoveActionForKey/p/System.String/

polebottom.RunAction(movebottompole, "movebottompole");

// remove it
polebottom.RemoveActionForKey("movebottompole");

但现在你不再拥有你的完成处理程序了。您可以通过将这两个动作组合成一个序列来解决它。

var poleactionWithCompletion = SKAction.Sequence(movebottompole, restpoles);
polebottom.RunAction(poleactionWithCompletion, "movebottompole");

如果您更频繁地需要这个,可以实现类似的扩展方法:

public static class SKNodeExtension
{
    public static void RunAction(this SKNode node, SKAction action, SKAction completion, string key)
    {
        var sequence = SKAction.Sequence(action, completion);
        node.RunAction(sequence, key);
    }
}

然后执行您的操作:

polebottom.RunAction(movebottompole, restpoles, "movebottompole");