获取物体的面积
我正在尝试获取对象的四个角。来测试一下是否足够准确。当我的鼠标经过对象的底部、左侧、右侧或顶部时,使用一种方法来跟踪消息。当对象位于坐标 0,0 时。它工作完美。但当我重新定位它时。它关闭了,我不知道为什么。这是我的代码。
在我的班级中,有一个循环有两个methods.updateArea()。和检查球。更新区域常量更新对象所在的位置。我的显示对象焦点位于 Flash Stage 的左上角。所以我使用完整的宽度和高度。再次。它在坐标 0,0 处工作正常,但当我移动它时,它却不起作用。
package com.objects {
import flash.display.Sprite;
import flash.events.*;
import flash.display.Stage;
import flash.geom.Rectangle;
public class Brick extends Sprite {
public var Points = 100;
public var bWidth:Number = 50;
public var bHeight:Number = 20;
private var left:Number;
private var right:Number;
private var top:Number;
private var bottom:Number;
public var ball:Ball;
private var lastDistance:Number;
public var hit:Boolean;
public function Brick():void
{
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
updateArea();
checkBall();
}
private function updateArea():void {
left = x;
right = x + bWidth;
top = y;
bottom = y + bHeight;
}
private function checkBall():void
{
if(mouseY < bottom)
{
trace("Works!");
}
}
public function getBall(ball:Ball):void
{
this.ball = ball;
}
public function xDir():Number
{
if ((ball.xDir * ball.xspeed) < 0)
{
return(-1);
}
else if ((ball.xDir * ball.xspeed) > 0)
{
return (1);
}
return 0;
}
public function yDir():Number
{
if (ball.yspeed < 0)
{
return(-1);
}
else if (ball.yspeed > 0)
{
return (-1);
}
return 0;
}
}
}
I am trying to get the four corners of my object. To test out whether its accurate enough. use a method to trace a message when my mouse passes the objects bottom, left, right, or top. when the object is positioned at coord 0,0. it works perfect. but when I reposition it. It becomes off and I have no idea why. here is my code.
in my class, there is a loop that has 2 methods.updateArea(). and checkball. update area constant give update to where the object is. my display object forcus point is at the top left on my Flash Stage. so I use the full width and height. again. it works fine at coordinate 0,0 and when I move it, it doesnt.
package com.objects {
import flash.display.Sprite;
import flash.events.*;
import flash.display.Stage;
import flash.geom.Rectangle;
public class Brick extends Sprite {
public var Points = 100;
public var bWidth:Number = 50;
public var bHeight:Number = 20;
private var left:Number;
private var right:Number;
private var top:Number;
private var bottom:Number;
public var ball:Ball;
private var lastDistance:Number;
public var hit:Boolean;
public function Brick():void
{
addEventListener(Event.ENTER_FRAME, loop);
}
private function loop(e:Event):void
{
updateArea();
checkBall();
}
private function updateArea():void {
left = x;
right = x + bWidth;
top = y;
bottom = y + bHeight;
}
private function checkBall():void
{
if(mouseY < bottom)
{
trace("Works!");
}
}
public function getBall(ball:Ball):void
{
this.ball = ball;
}
public function xDir():Number
{
if ((ball.xDir * ball.xspeed) < 0)
{
return(-1);
}
else if ((ball.xDir * ball.xspeed) > 0)
{
return (1);
}
return 0;
}
public function yDir():Number
{
if (ball.yspeed < 0)
{
return(-1);
}
else if (ball.yspeed > 0)
{
return (-1);
}
return 0;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
x/y/mouseX/mouseY 始终相对于父级。您可以使用 localToGlobal 和 globalToLocal 将父坐标转换为全局坐标。
另外,如果您的对象不是正方形或有孔,您可以使用 hitTest 来测试碰撞,而不是根据坐标进行计算。
x/y/mouseX/mouseY are always relative to the parent. You can use localToGlobal and globalToLocal to translate from parent to global coordinates.
Also if your object isn't square or has holes, you can use hitTest to test for collision instead of calculating based on coordinates.