我有一個應用程序,其中是球(橢圓)和4個矩形(每個窗口的邊緣)。我嘗試編寫一個應用程序,讓球在移動並從邊緣彈起。我不知道如何做出一個方法,當碰撞是真的時球會改變方向。橢圓反彈使用IntersectsWith
XAML:
<Window x:Class="naszybkodlatestu.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="320" Width="640">
<Canvas>
<Rectangle Name="boardTop"
Height="2"
Fill="Black"
Width="609" Canvas.Left="7" Canvas.Top="10"/>
<Rectangle Name="boardLeft"
Height="270"
Fill="Black"
Width="2" Canvas.Left="614" Canvas.Top="10"/>
<Rectangle Name="boardRight"
Height="269"
Fill="Black"
Width="2" Canvas.Left="7" Canvas.Top="12"/>
<Rectangle Name="boardBot"
Height="2"
Fill="Black"
Width="609" Canvas.Left="7" Canvas.Top="278" />
<Ellipse Name="ball"
Width="15"
Height="15"
Fill="Red"
Canvas.Left="303"
Canvas.Top="107"/>
</Canvas>
</Window>
C#:
DispatcherTimer dTimer;
private void InitTimer()
{
dTimer = new DispatcherTimer();
dTimer.Tick += new EventHandler(TickdTimer);
dTimer.Interval = new TimeSpan(0, 0, 0, 0, 1);
dTimer.Start();
}
public MainWindow()
{
InitializeComponent();
InitTimer();
InitMove();
}
private bool isMovingUp = false;
private bool isMovingDown = false;
private bool isMovingLeft = true;
private bool isMovingRight = false;
private double ballDirectionX = 0;
private double ballDirectionY = 0;
private double ballSpeed = 1;
private bool CollisionDetection()
{
if (CheckCollision(ball, boardBot) || CheckCollision(ball, boardRight) ||
CheckCollision(ball, boardTop) || CheckCollision(ball, boardLeft))
{
return true;
}
else return false;
}
public static bool CheckCollision(FrameworkElement a, FrameworkElement b)
{
Rect rect1 = new Rect((double)a.GetValue(Canvas.LeftProperty), (double)a.GetValue(Canvas.TopProperty), a.Width, a.Height);
Rect rect2 = new Rect((double)b.GetValue(Canvas.LeftProperty), (double)b.GetValue(Canvas.TopProperty), b.Width, b.Height);
if (rect1.IntersectsWith(rect2))
{
return true;
}
else return false;
}
private void InitMove()
{
if (isMovingDown)
{
ballDirectionY += ballSpeed;
}
if (isMovingUp)
{
ballDirectionY -= ballSpeed;
}
if (isMovingLeft)
{
ballDirectionX -= ballSpeed;
}
if (isMovingRight)
{
ballDirectionX += ballSpeed;
}
ball.SetValue(Canvas.TopProperty, ballDirectionY);
ball.SetValue(Canvas.LeftProperty, ballDirectionX);
}
所以我試圖像水木清華在InitMove()。這只是一個測試代碼,可以告訴你什麼是問題。
private void InitMove()
{
.......
if (CollisionDetection())
{
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
}
}
所以球從左向右改變方向。現在好了。
問題現在開始,當我想把下一個「如果」。
private void InitMove()
{
.......
if (CollisionDetection())
{
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
}
if (isMovingRight)
{
isMovingRight = false;
isMovingLeft = true;
}
}
現在球通過矩形的邊緣,沒有反彈。所以這是我的問題。
如何使之正確?
//編輯
當我加入其他
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
}
else if (isMovingRight)
{
isMovingRight = false;
isMovingLeft = true;
}
球正好停在窗口的邊緣。
// EDIT2
行,所以我想我知道如何做到這一點THX你
if (isMovingLeft)
{
isMovingLeft = false;
isMovingRight = true;
ballDirectionX += 2;
}
else if (isMovingRight)
{
isMovingRight = false;
isMovingLeft = true;
ballDirectionX -= 2;
}
所以我加了一些舉措來推動這個球出牆碰撞。我現在沒有看到另一種方式。
現在球剛停在邊緣。 – morad1n 2013-05-07 21:27:53
單個「if(isMovingLeft){...}」是否仍然有效? – 2013-05-07 21:34:35
當我添加您的語句球只停在邊緣。如果(isMovingLeft){...}沒有其他屬性,則爲單個。 – morad1n 2013-05-07 21:36:37