Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.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 如何创建碰撞检测x值(简单乒乓球游戏)ActionScript3_Actionscript 3 - Fatal编程技术网

Actionscript 3 如何创建碰撞检测x值(简单乒乓球游戏)ActionScript3

Actionscript 3 如何创建碰撞检测x值(简单乒乓球游戏)ActionScript3,actionscript-3,Actionscript 3,所以,我创建了一个简单的乒乓球游戏 当球击中球员的顶部/底部时,它会垂直反弹。问题是当球击中球员一侧的左/右时,它不会水平反弹 到目前为止,应修订或添加相关代码: var ballrec:Rectangle = new Rectangle(sball.x-(sball.width*0.50), sball.y-(sball.height*0.50), sball.width, sball.height); var playerrec:Rect

所以,我创建了一个简单的乒乓球游戏

当球击中球员的顶部/底部时,它会垂直反弹。问题是当球击中球员一侧的左/右时,它不会水平反弹

到目前为止,应修订或添加相关代码:

var ballrec:Rectangle = new Rectangle(sball.x-(sball.width*0.50), 
            sball.y-(sball.height*0.50), 
            sball.width, sball.height);

var playerrec:Rectangle = new Rectangle(splayer.x-(splayer.width*0.50), 
            splayer.y-(splayer.height*0.50), 
            splayer.width, 
            splayer.height);

if(ballrec.intersects(playerrec)){

            var intersection:Rectangle = ballrec.intersection(playerrec);

            accelerationy *= -1;
            yspeed *= -1;
}

提前谢谢!很抱歉给您添麻烦

很抱歉,我不知道哪个物体是移动的球,我无法发表评论,因为我的声誉让我不得不检查球是否击中舞台的顶部/底部,请尝试:

if (ball.y <= 0 || ball.y >= stage.stageHeight){

    //do whatever when the ball collisions with the top or bottom
}

你还必须在击中时反转x轴的值。我知道,但是你如何检查它是否击中侧面而不是顶部/底部?你已经准备好了。。你可能还想看看关于碰撞检测的内容:我建议你在游戏中使用starling框架。在while循环中使用hitTest,在里面你可以识别你的边界