我有2張圖片(bar和greenBall1)。酒吧可以上下移動取決於用戶的反應。而greenBall1正在屏幕上移動。如果兩個圖像相互接觸,我想進行圖像碰撞,greenBall1將改變其速度。我對greenBall1的代碼如下。如何在使用Windows Phone應用程序的Windows Phone 8的C#中執行圖像衝突?
private void OnUpdate(object sender, object e)
{
Canvas.SetLeft(this.GreenBall1, this.greenBallVelocityX + Canvas.GetLeft(this.GreenBall1));
Canvas.SetTop(this.GreenBall1, this.greenBallVelocityY + Canvas.GetTop(this.GreenBall1));
var greenBallPositionX1 = Canvas.GetLeft(this.GreenBall1);
var greenBallPositionY1 = Canvas.GetTop(this.GreenBall1);
var maximumGreenBallX = ActualWidth - this.GreenBall1.ActualWidth;
var maximumGreenBallY = 400 - this.GreenBall1.ActualHeight; //Improvise: Instead of 360, get maximum height of canvas
if (greenBallPositionX1 > maximumGreenBallX || greenBallPositionX1 < 0)
{
this.greenBallVelocityX *= -1;
}
if (greenBallPositionY1 > maximumGreenBallY || greenBallPositionY1 < 0)
{
this.greenBallVelocityY *= -1;
}
}
你想要做圈的碰撞? http://stackoverflow.com/questions/1736734/circle-circle-collision –
不,我希望做一個矩形的碰撞。基本上我就像在球擊中矩形杆時試圖做出反彈一樣,現在球會穿過我的酒吧。 –
是否限制像Pong一樣上下移動? –