views:

160

answers:

1

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;
 }
+1  A: 

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.

jonathanasdf
ridiculous! lol. that is unbelievably way easier than what i've been trying to do all day. thanks a million!
TheDarkInI1978
the result is sometimes quite a lot simpler than you thought it'd be :)and in other cases quite a lot harder.
jonathanasdf