ActionScript 将精灵旋转应用到 startDrag() 的矩形边界

发布于 2024-08-28 06:25:04 字数 1930 浏览 5 评论 0原文

从我的主类中,我调用创建一个精灵并将其添加到舞台,

private function addSwatch(evt:MouseEvent):void
 {
 if (stage.getObjectsUnderPoint(mousePoint()).length == 0)
  {
  var swatchSide:Number = 100;
  var newSwatch:Sprite = new Swatch(0 - swatchSide/2, 0 - swatchSide/2, swatchSide, swatchSide);
  newSwatch.x = mouseX;
  newSwatch.y = mouseY;
  addChild(newSwatch);
  }
 }

我已将一个样本精灵添加到舞台,当拖动时,该精灵包含在设定的边界内。

this.startDrag(false, swatchBounds());

...

private function swatchBounds():Rectangle
 {
 var stageBounds = new Rectangle ( 
         0 - defaultSwatchRect.x,
         0 - defaultSwatchRect.y,
         stage.stageWidth - defaultSwatchRect.width,
         stage.stageHeight - defaultSwatchRect.height
         );
 return stageBounds;
 }

如果方形精灵被缩放,则以下返回的矩形边界将起作用,

private function swatchBounds():Rectangle
 {
 var stageBounds = new Rectangle ( 
         0 - defaultSwatchRect.x * swatchObject.scaleX,
         0 - defaultSwatchRect.y * swatchObject.scaleY,
         stage.stageWidth - defaultSwatchRect.width * swatchObject.scaleX,
         stage.stageHeight - defaultSwatchRect.height * swatchObject.scaleY
         );
 return stageBounds;
 }

现在我正在尝试将方形精灵旋转包含到混合中。数学当然不是我的强项,但我觉得我正在写作。然而,我似乎无法全神贯注地把它做好

private function swatchBounds():Rectangle
 {
 var stageBounds = new Rectangle ( 
         0 - defaultSwatchRect.x * swatchObject.scaleX * Math.cos(defaultSwatchRect.x * swatchObject.rotation),
         0 - defaultSwatchRect.y * swatchObject.scaleY * Math.sin(defaultSwatchRect.y * swatchObject.rotation),
         stage.stageWidth - defaultSwatchRect.width * swatchObject.scaleX * Math.cos(defaultSwatchRect.width * swatchObject.rotation),
         stage.stageHeight - defaultSwatchRect.height * swatchObject.scaleY * Math.sin(defaultSwatchRect.height * swatchObject.rotation)
         );
 return stageBounds;
 }

from my main class i call to create a sprite and add it to the stage

private function addSwatch(evt:MouseEvent):void
 {
 if (stage.getObjectsUnderPoint(mousePoint()).length == 0)
  {
  var swatchSide:Number = 100;
  var newSwatch:Sprite = new Swatch(0 - swatchSide/2, 0 - swatchSide/2, swatchSide, swatchSide);
  newSwatch.x = mouseX;
  newSwatch.y = mouseY;
  addChild(newSwatch);
  }
 }

i've added a swatch sprite to the stage which, when dragged, is contained within set boundaries.

this.startDrag(false, swatchBounds());

...

private function swatchBounds():Rectangle
 {
 var stageBounds = new Rectangle ( 
         0 - defaultSwatchRect.x,
         0 - defaultSwatchRect.y,
         stage.stageWidth - defaultSwatchRect.width,
         stage.stageHeight - defaultSwatchRect.height
         );
 return stageBounds;
 }

if the square sprite is scaled, the following returned rectangle boundary works

private function swatchBounds():Rectangle
 {
 var stageBounds = new Rectangle ( 
         0 - defaultSwatchRect.x * swatchObject.scaleX,
         0 - defaultSwatchRect.y * swatchObject.scaleY,
         stage.stageWidth - defaultSwatchRect.width * swatchObject.scaleX,
         stage.stageHeight - defaultSwatchRect.height * swatchObject.scaleY
         );
 return stageBounds;
 }

now i'm trying to include the square sprites rotation into the mix. math certainly isn't my forté, but i feel i'm on the write track. however, i just can't seem to wrap my head around it to get it right

private function swatchBounds():Rectangle
 {
 var stageBounds = new Rectangle ( 
         0 - defaultSwatchRect.x * swatchObject.scaleX * Math.cos(defaultSwatchRect.x * swatchObject.rotation),
         0 - defaultSwatchRect.y * swatchObject.scaleY * Math.sin(defaultSwatchRect.y * swatchObject.rotation),
         stage.stageWidth - defaultSwatchRect.width * swatchObject.scaleX * Math.cos(defaultSwatchRect.width * swatchObject.rotation),
         stage.stageHeight - defaultSwatchRect.height * swatchObject.scaleY * Math.sin(defaultSwatchRect.height * swatchObject.rotation)
         );
 return stageBounds;
 }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一袭白衣梦中忆 2024-09-04 06:25:04

我的想法是,不要使用复杂的三角函数,只需使用 swatchObject.getRect() 获取 swatchObject 的边界矩形,然后基于此创建 stageBounds 即可。它应该足以满足您的目的。

如果这不是你想要的,我可以帮你算出数学公式。

抱歉,在我弄清楚 defaultSwatchRect 是什么 - 它的 x 和 y 在哪里以及它应该做什么之前,我无法真正给你一个函数。

将来您可能需要记住的另一件事是:数学函数(cos、sin)期望角度以弧度为单位,而 .rotation 属性以度为单位,因此您必须在使用之前进行转换。

My idea is that instead of using complicated trig, just get the bounding rect of swatchObject using swatchObject.getRect() and then create your stageBounds based on that. It should be more than good enough for your purposes.

If that is not what you want, I can help you figure out the math.

And sorry, I can't really give you a function until I figure out what defaultSwatchRect is - where its x and y is and what it's supposed to do.

Another thing you may want to keep in mind for the future: the Math functions (cos, sin) expect the angle in radians, whereas the .rotation property is in degrees, so you must convert before using.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文