Avatar of 1Cougar
1Cougar

asked on 

actionscript 3.0 scale rectangle

Hello,

I am testing some animation and I am new at controlling it entirely by actionscript.  I have a test fla that creates an object of a custom class "Easing1" and adds it to the stage.  Easing1 creates a box and drops it on the stage.  I want to then scale this box to a certain size, keeping it centered on the stage.  The code I have scales the object but it goes off the stage in a diagonal and I can't seem to figure out how to keep it centered and scale it there.....

I am pretty sure this is something easy but I can't seem to figure it out.

Thanks in advance!
//Easing1 class
 
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.display.Graphics;
 
	public class Easing1 extends Sprite {
 
		private var box:Box;
		private var side:Number;
		private var color:uint;
		private var shapes:Sprite = new Sprite();
		private var gr:Graphics=shapes.graphics;
		private var easing:Number=0.2;
		private var targetX:Number=370;
		private var targetY:Number=220;
		private var targetAlpha:Number=1;
		private var easing1:Number=.04;
		private var scaleTargetY:Number=200;
		private var scaleTargetX:Number=200;
 
		public function Easing1() {
			init();
		}
 
		private function scaleMe():void {
			box.width=300;
			box.height=300;
			box.x=0;
			box.y=0;
 
		}
 
		private function init():void {
			box = new Box();
			addChild(box);
			addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
 
		private function onEnterFrame(event:Event):void {
			var dx:Number=targetY-box.y;
			if (Math.abs(dx)<1) {
				box.y=targetY;
				removeEventListener(Event.ENTER_FRAME,onEnterFrame);
				trace("done");
				addEventListener(Event.ENTER_FRAME,onEnterFrame1);
			} else {
				var vy:Number = (targetY - box.y) * easing;
				box.y+=vy;
			}
 
		}
		private function onEnterFrame1(event:Event):void {
 
			var dx:Number=scaleTargetX-box.width;
			trace("Math:" + Math.abs(dx));
			if (Math.abs(dx)<5) {
				box.width=scaleTargetX;
				box.height=scaleTargetY;
				removeEventListener(Event.ENTER_FRAME,onEnterFrame1);
				trace("done scaling");
 
			} else {
				//var vx1:Number = (scaleTargetX - box.width) * easing1;
				//var vy1:Number = (scaleTargetY - box.height) * easing1;
				var vx1:Number = ((1+easing1) * box.width);
				var vy1:Number = ((1+easing1) * box.height);
				box.scaleIt(vx1,vy1,box.x,box.y);
			}
 
		}
 
	}
 
}
 
 
 
// Box class
 
package {
	import flash.display.Sprite;
	import flash.display.Graphics;
 
	public class Box extends Sprite {
		private var side:Number;
		private var color:uint;
		public var shapes:Sprite = new Sprite();
		public var gr:Graphics=shapes.graphics;
 
		public function Box() {
			init();
		}
 
		public function init():void {
			gr.lineStyle(6,0x00000,.5);
			gr.beginFill(0x000000,1);
			gr.drawRect(0,0,40,40);
			gr.endFill();
			shapes.x=370;
			shapes.y=0;
			addChild(shapes);
 
		}
		
		public function scaleIt(toX:Number,toY:Number,xCoor:Number,yCoor:Number):void {
			var xScale:Number = 1 + toX;
			var yScale:Number = 1 + toY;
			
			this.height=toX;
			this.width=toY;
			
			this.x=xCoor;
			this.y=yCoor;
			
			trace("in scale x:" + this.x);
			trace("in scale y:" + this.y);
 
 
		}
 
 
	}
 
 
}

Open in new window

Web Development SoftwareAdobe Flash

Avatar of undefined
Last Comment
1Cougar

8/22/2022 - Mon