我的精灵确实连接了,但在中间你仍然可以“掉下来”
本文关键字:掉下来 精灵 连接 在中间 我的 | 更新日期: 2023-09-27 18:33:24
我得到了2个精灵和一个球。我在 0,400 点开始了一个精灵,在 250,400 点开始了一个精灵。两个精灵是相同的,它们长 250 高 10 高(在我的 WorkshopContent 中)。我用一个球在精灵上弹跳,这有效。但是当球在精灵的中间(大约 250,400 个)时,球会掉下来。我不知道为什么。
我的球是29,30。
protected override void Initialize()
{
ball1 = new StuiterBall();
ball1.texture = "voetbal";
ball1.Position = new Vector2(0, 150);
allSprites.Add(ball1);
Obstakel obstakel1 = new Obstakel();
obstakel1.Position = new Vector2(0, 400);
obstakel1.texture = "witterechthoek";
allSprites.Add(obstakel1);
allSpriteObstakels1.Add(obstakel1);
Obstakel obstakel3 = new Obstakel();
obstakel3.Position = new Vector2(250, 400);
obstakel3.texture = "witterechthoek";
allSprites.Add(obstakel3);
allSpriteObstakels1.Add(obstakel3);
}
private void checkCollisions()
{
Rectangle rectball1 = new Rectangle((int)ball1.Position.X, (int) ball1.Position.Y, 30, 29);
foreach (ISprite s in allSpriteObstakels1) {
Rectangle rectSprite = new Rectangle((int)s.Position.X, (int)s.Position.Y, 250, 10);
Rectangle overlap = Rectangle.Intersect(rectball1, rectSprite);
if (!overlap.IsEmpty)
{
s.CollisionWith(ball1);
ball1.CollisionWith(s);
}
}
}
斯图特球.cs,碰撞方法:
public void CollisionWith(ISprite s)
{
Speed.Y = -Speed.Y;
}
要使用的方法是 Rectangle.Intersects():
bool isCollision = rectball1.Intersects(rectSprite);
if (isCollision)
{
s.CollisionWith(ball1);
ball1.ColisionWith(s);
}