Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3中一个简单物理系统的问题_Actionscript 3_Collision Detection - Fatal编程技术网

Actionscript 3 AS3中一个简单物理系统的问题

Actionscript 3 AS3中一个简单物理系统的问题,actionscript-3,collision-detection,Actionscript 3,Collision Detection,首先,我是一名学生,所以我恳请你耐心回答。顺便说一句,我也不会说英语。 一切都开始了,因为我要参加一场游戏赛,我已经开始写(或试图写)一个简单的“物理”代码,我真的认为这很容易。 我使用类并使用hitTestObject进行冲突检测。 事实上,我从一开始就有问题,但我一直在努力解决它。在这个时候,代码真的很糟糕,我已经在用另一种方式来做了。但有些事情让我困惑。 下面是我已经编写的代码的一部分(可能看起来非常新手)。代码的大部分都是胡说八道,看看最后一个“elseif”。 注意:这是“block”

首先,我是一名学生,所以我恳请你耐心回答。顺便说一句,我也不会说英语。 一切都开始了,因为我要参加一场游戏赛,我已经开始写(或试图写)一个简单的“物理”代码,我真的认为这很容易。 我使用类并使用hitTestObject进行冲突检测。 事实上,我从一开始就有问题,但我一直在努力解决它。在这个时候,代码真的很糟糕,我已经在用另一种方式来做了。但有些事情让我困惑。 下面是我已经编写的代码的一部分(可能看起来非常新手)。代码的大部分都是胡说八道,看看最后一个“elseif”。 注意:这是“block”类,“obj”是播放器的电影剪辑

public function checkObj(obj:MovieClip):void
{   
    if (this.hitTestObject (obj))
    {
        if (this.y < obj.y)
        {
            if (this.x - this.width/2 <= obj.x+obj.width/2 && this.x + this.width/2 >= obj.x-obj.width/2)
            {
                downCol = true;
            }
        }

        else if (this.y + this.height/2 > obj.y-obj.height/2)
        {
            if (this.x - this.width/2+2 <= obj.x+obj.width/2 && this.x + this.width/2-2 >= obj.x-obj.width/2)
            {
                upCol = true;
                onBox = true;
            }
        }
        if (this.x - this.width/2 >= obj.x+obj.width/2-2)
        {
            if (this.y - this.height/2<= obj.y+obj.height/2&& this.y + this.height/2>= obj.y-obj.height/2)
            {
                leftCol = true;
            }
        }
        else if (this.x + this.width/2 <= obj.x-obj.width/2-2)
        {
        if (this.y + 5>= obj.y-obj.height/2 && this.y - 5<= obj.y+obj.height/2)
            {
                trace ("Collided on right");
                rightCol = true;
            }
        }
    }
}
公共功能检查obj(obj:MovieClip):无效
{   
if(this.hitTestObject(obj))
{
如果(本年<本年)
{
if(this.x-this.width/2=obj.x-obj.width/2)
{
downCol=true;
}
}
否则如果(this.y+this.height/2>obj.y-obj.height/2)
{
if(this.x-this.width/2+2=obj.x-obj.width/2)
{
upCol=真;
onBox=true;
}
}
如果(this.x-this.width/2>=obj.x+obj.width/2-2)
{
if(this.y-this.height/2=obj.y-obj.height/2)
{
leftCol=true;
}
}

else if(this.x+this.width/2=obj.y-obj.height/2&&this.y-5在最后一个else if中尝试这个

else if (this.x + this.width / 2 <= obj.x + obj.width / 2 - 2) {
        if (this.y + 5 >= obj.y - obj.height / 2 && this.y - 5 <= obj.y + obj.height / 2) {
            trace("Collided on right");
            rightCol = true;
        }
    }  

else如果(this.x+this.width/2=obj.y-obj.height/2&&this.y-5好吧,算了吧。我写了一个全新的代码,没有测试,它工作起来很有魅力。我想Math.abs解决了负数问题。
谢谢你的帮助,kare81。
代码如下:

public function checkObj(obj:MovieClip,place:int):void
        {   
            if (obj.x + obj.width / 2 > this.x - width / 2 && obj.x < this.x - width / 2 + 7 && Math.abs (obj.y - y) < height / 2)
            {
                obj.x = this.x - width / 2 - obj.width / 2;
            }
            if (obj.x - obj.width / 2 < this.x +width / 2 && obj.x > this.x +  width / 2 - 7 && Math.abs (obj.y - y) < height / 2)
            {
                obj.x = this.x +width / 2 + obj.width / 2;
            }
            if (Math.abs (obj.x- this.x) < width / 2 + obj.width / 2 && obj.y<y-height/2 && obj.onFloor > y-height/2 && obj.onBlock != place)
            {
                obj.onFloor = y - height / 2;
                obj.onBlock = place;
            }
            if (Math.abs (obj.x - this.x) >= width / 2 + obj.width / 2 && obj.onBlock == place)
            {
                obj.onFloor = 450;
            }
            if (obj.y - obj.height / 2 < y + height /2 && obj.y > y && Math.abs (obj.x - this.x) < width / 2 + obj.width / 2)
            {
                obj.y = y + height / 2 + obj.height / 2;
            }
        }
public function checkObj(obj:MovieClip,place:int):无效
{   
如果(obj.x+obj.width/2>this.x-width/2&&obj.xthis.x+width/2-7&&Math.abs(obj.y-y)=width/2+obj.width/2&&obj.onBlock==place)
{
obj.onFloor=450;
}
if(obj.y-obj.height/2y&&Math.abs(obj.x-this.x)
谢谢你的回答!现在可以了!但现在他们遇到了另一个问题:当我在左侧跳跃并与之碰撞,但仍按“D”键时,物体播放器刚好摔倒,我想让他们做的是谁。但当我在右侧碰撞并仍按“A”键时按钮,玩家在空中停留直到释放按钮…我只是在玩家类上有这样一个:“如果(block.leftCol){player.x-=speed}或者如果(block.rightCol){player.x+=speed)”你应该支持更多关于按键功能的代码
public function checkObj(obj:MovieClip,place:int):void
        {   
            if (obj.x + obj.width / 2 > this.x - width / 2 && obj.x < this.x - width / 2 + 7 && Math.abs (obj.y - y) < height / 2)
            {
                obj.x = this.x - width / 2 - obj.width / 2;
            }
            if (obj.x - obj.width / 2 < this.x +width / 2 && obj.x > this.x +  width / 2 - 7 && Math.abs (obj.y - y) < height / 2)
            {
                obj.x = this.x +width / 2 + obj.width / 2;
            }
            if (Math.abs (obj.x- this.x) < width / 2 + obj.width / 2 && obj.y<y-height/2 && obj.onFloor > y-height/2 && obj.onBlock != place)
            {
                obj.onFloor = y - height / 2;
                obj.onBlock = place;
            }
            if (Math.abs (obj.x - this.x) >= width / 2 + obj.width / 2 && obj.onBlock == place)
            {
                obj.onFloor = 450;
            }
            if (obj.y - obj.height / 2 < y + height /2 && obj.y > y && Math.abs (obj.x - this.x) < width / 2 + obj.width / 2)
            {
                obj.y = y + height / 2 + obj.height / 2;
            }
        }