0

我有一个返回 xml 的函数,但返回一个带有两个的 xml

   <?xml version="1.0"?>

标签

我无法修复呼叫以通过一个呼叫返回它。我尝试使用 SimpleXMLElement,所以当我将此 xml 加载到 simplexml 时,由于标签重复,它会出错。任何人都知道我如何在加载到 xml 之前删除标签

我试过做

$new_xml = preg_replace('/<?xml(.*)?>(.*)?<\/?>/', '', $xml);

我也做过(顺便说一句。只是不确定它是否是最好的方法,因为我不确定内部是否有时会有更多信息

  $xml = str_replace('<?xml version="1.0"?>', '', $xml);

Pong Paddle 碰撞速度和反弹角

好吧,我为此搜索了很多,但我能找到的只是人们说像做 pi * 方向,方向是我假设的球进入的角度。但我的问题是,我不知道我是如何得到球进入的角度的,所以我无法做到这些。如果有人能解释我将如何计算球撞击桨帽的角度,反弹后球的速度量以及到那时它应该增加的角度,那就太棒了。

感谢您的所有回复!

我的代码如下工作(所以你可以知道我想怎么做):

/* General Paddle Properties */
double PaddleLength = 80;  //Down-wards length of the paddle
double PaddleWidth = 8;  //How thick the paddle is

/* Positioning of user control paddle */
double UserPaddleTop = 0;  //How far away from the top of the screen the paddle is
double UserPaddleLeft = 10;  //How far left from the side of the client rectangle it is

/* Positioning of ai controled paddle */
double AIPaddleTop = 0;
double AIPaddleLeft = 10;

/* Ball properties and position */
double BallSize = 5;
double BallTop = 0; 
double BallLeft = 0;
double BallSpeedY = -0.01, BallSpeedX = -0.03;

方法:

private void UpdateBall()
        {
            if (((int)(UserPaddleLeft + PaddleWidth) == (int)BallLeft) && !((int)UserPaddleTop > (int)BallTop) && !((int)(UserPaddleTop + PaddleLength) < BallTop) 
                || ((int)(AIPaddleLeft - PaddleWidth) == (int)BallLeft) && !((int)AIPaddleTop > (int)BallTop) && !((int)(AIPaddleTop + PaddleLength) < BallTop)) //Collided
            {
                BallSpeedX = -BallSpeedX; //The height is 800 the balltop is 300
                BallSpeedY = Math.Cos(BallSpeedX
            }

            if ((int)BallTop == 0 || (int)BallTop == ClientRectangle.Height) //Hit the top 
            {
                BallSpeedY = -BallSpeedY;
            }

            if ((int)BallLeft == 0)
            {
                System.Diagnostics.Debug.WriteLine("AI gets one point!");
                BallSpeedX = -0.03; //Goes towards the user AI has scored
                Scores[0]++;
                this.Title = "Pong Testing - Scores: " + Scores[0] + "|" + Scores[1];
                ResetAll();
            }
            else if ((int)BallLeft == ClientRectangle.Width)
            {
                System.Diagnostics.Debug.WriteLine("User gets one point!");
                BallSpeedX = 0.03; //Goes towards the AI user has scored
                Scores[1]++;
                this.Title = "Pong Testing - Scores: " + Scores[0] + "|" + Scores[1];
                ResetAll();
            }

            BallLeft = (BallLeft + BallSpeedX);
            BallTop = (BallTop + BallSpeedY);
        }

        private void UpdateAI()
        {
            if(!((int)(BallTop + PaddleLength) == 0) && !( (int)(BallTop + PaddleLength) >= ClientRectangle.Height ) ) //Make sure updating it pos won't make it go out of bounds
                AIPaddleTop = BallTop; //Change to real ai by using offset
        }

        protected override void OnUpdateFrame(FrameEventArgs e)
        {
            base.OnUpdateFrame(e);

            if ( (int)UserPaddleTop != 0 && Keyboard[Key.Up])
            {
                UserPaddleTop = UserPaddleTop - MoveSpeed;
            }
            else if (Keyboard[Key.Down] && (int)(UserPaddleTop + PaddleLength) != ClientRectangle.Height)
            {
                UserPaddleTop = UserPaddleTop + MoveSpeed;
            }
        }

更新 1:

感谢大家的帮助,我已经能够为它想出一些基本代码,但现在这个代码只是让球飞得太快,以至于不可能得到它。有人可以帮忙吗?

代码:

        double AngleNormal = Math.Atan2(BallSpeedX,BallSpeedY);
        double AngleBallMovement = Math.Sqrt((BallSpeedX * BallSpeedX) + (BallSpeedY * BallSpeedY));
        double ReflectionAngle = AngleNormal - (AngleBallMovement - AngleNormal);
        BallSpeedY = Math.Sin(ReflectionAngle);
        BallSpeedX = Math.Cos(ReflectionAngle);
4

2 回答 2

1

这将删除第一行直到结束 > 和尾随的换行符。

<?php
$xml = "<?xml version=\"1.0\"?>\n\t<tag></tag>";

     $xml = preg_replace('!^[^>]+>(\r\n|\n)!','',$xml);

echo $xml;
?>

它也适用于不符合 1.0 版的其他 XML 文件。

于 2013-04-02T00:23:37.790 回答
0

@AbsoluteƵERØ 的回答帮助了我。

在我的例子中,我自己创建了 XML。

$doc = new DOMDocument();
$doc->formatOutput = true;

$root = $doc->createElement('TAG1');
$doc->appendChild($root);

// ... some loop that inserts more children of TAG1 with attributes

$xml_file = 'relative/path/to/file.xml';

$doc->save($xml_file); // Here the XML file is saved along with the line that poses problems

// Read created XML
$t_xml = file_get_contents($xml_file);

// Remove first line
$t_xml = preg_replace('!^[^>]+>(\r\n|\n)!', '', $t_xml);

// Save into same file as original
file_put_contents($xml_file, $t_xml);

于 2021-04-23T13:22:40.077 回答