初始化一个布尔干扰了我的逻辑 [英] Initializing a bool is interfering with my logic

查看:135
本文介绍了初始化一个布尔干扰了我的逻辑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这方面的工作方法,它涉及碰撞检测。我有一个问题与方向布尔被初始化为true。有时,射击的对象时,角度是相反的,因为它期待布尔是假的。

如果该对象(在我code为0至350)最初旨在朝向左侧时,布尔应该是真的。如果它的目的是朝着正确的(350和700之间),布尔应该是假的。

如何设置初始布尔到适当的真/假值,而随后的碰撞检测的干扰?

我认为这个问题是当我尝试检查什么方向应该再设置它,我不经意间施加约束到的对象。我无法弄清楚如何将两人分开。理想的是,在0和350检查,并且在350〜700检查应只发生一次,但它们是在每次调用方法时被检查,这导致了问题。

  //移动泡沫
    公共无效MoveBubble(泡泡,Vector2距离)
    {
        如果(bubble.stop ==假)
        {
            velocityX =速度* distance.X;
            velocityY =速度* distance.Y;

            bubble.X + = velocityX;

            //问题部分
            如果(bubble.Y大于0&安培;&安培; bubble.Y&所述; 350)
                方向= TRUE;

            如果(bubble.Y> 350安培;&安培; bubble.Y< 700)
                方向= FALSE;
            //问题到底节

            如果(方向==真)
                bubble.Y + = velocityY;

            如果(方向==假)
                bubble.Y  -  = velocityY;

            如果(bubble.Y< = 0 || bubble.Y> = 350)
                !方向=方向;
        }
    }
 

固定code:

  //移动泡沫,扭转它的速度,如果它与墙壁碰撞
    内部空隙MoveBubble(泡泡,Vector2距离)
    {
        如果(bubble.stopped ==假)
            如果(bubble.Y℃,|| bubble.Y> 350)
                bubble.velocityY = -bubble.velocityY;

            bubble.Y + = bubble.velocityY;
            bubble.X + = bubble.velocityX;
    }
 

解决方案

您必须在您将它移动到检查气泡的位置。在你拥有了它在移动的结束位置,你可能会在屏幕的界限的。

解释我的意思

我看到你在这里做的是:

  1. 寻找你的方向 将移动
  2. 将气泡
  3. Checkif泡沫已经改变 方向

在MI看来,你所要做的就是

  1. 检查遵循的方向
  2. 将气泡

当你开始另一次运动,你会检查方向气泡必须采取

I'm working on this method which deals with collision detection. I'm having an issue with the direction bool being initialized to true. Sometimes, when firing an object, the angle is reversed, because it's expecting the bool to be false.

If the object is initially aimed towards the left (between 0 and 350 in my code), the bool should be true. If it's aimed towards the right (between 350 and 700), the bool should be false.

How can I set the initial bool to the appropriate true/false value without interfering with subsequent collision detection?

I think the problem is when I try to check what the direction should be then set it, I'm inadvertently imposing constraints onto the objects. I can't figure out how to separate the two. Ideally, the between 0 and 350 check, and the between 350 and 700 check should only happen once, but they are being checked every time the method is called and this is causing the issue.

//Move bubble
    public void MoveBubble(Bubble bubble, Vector2 distance)
    {
        if (bubble.stop == false)
        {
            velocityX = speed * distance.X;
            velocityY = speed * distance.Y;

            bubble.X += velocityX;

            //Problem section
            if (bubble.Y > 0 && bubble.Y < 350)
                direction = true;

            if (bubble.Y > 350 && bubble.Y < 700)
                direction = false;
            //End Problem section

            if (direction == true)
                bubble.Y += velocityY;

            if (direction == false)
                bubble.Y -= velocityY;

            if (bubble.Y <= 0 || bubble.Y >= 350)
                direction = !direction;
        }
    }    

Fixed code:

    //Move bubble and reverse it's velocity if it collides with a wall
    internal void MoveBubble(Bubble bubble, Vector2 distance)
    {
        if (bubble.stopped == false)
            if (bubble.Y < 0 || bubble.Y > 350)
                bubble.velocityY = -bubble.velocityY;

            bubble.Y += bubble.velocityY;
            bubble.X += bubble.velocityX;
    }

解决方案

You have to check the position of the bubble before you move it. In the position you have it at the end of the move, you might be out of the bounds of the screen.

Explaining of what I mean

What I see you're doing here is:

  1. Looking for the direction you're going to move
  2. Move the bubble
  3. Checkif the bubble has to change direction

In mi opinion, what you have to do is

  1. Check the direction to follow
  2. Move the bubble

When you start another movement, you will check the direction the bubble have to take

这篇关于初始化一个布尔干扰了我的逻辑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆