dcsimg
A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Problems converting AS2 to AS3

  1. #1
    Junior Member
    Join Date
    Sep 2008
    Posts
    12

    Problems converting AS2 to AS3

    Hi

    I have some AS2 code that works fine, its just four movie clip thumbnails on the stage that scale when the mouse gets near to them.

    This is the AS2 code:
    Code:
    function proximity(clip) {
    	var x:Number = _root._xmouse;
    	var y:Number = _root._ymouse;
    	var cx:Number = clip._x;
    	var cy:Number = clip._y;
    	var prox:Number = Math.sqrt((x-cx)*(x-cx) + (y-cy)*(y-cy));
    	if(prox<100) {
    		clip._xscale = 200 - prox;
    		clip._yscale = clip._xscale;
    	}
    	else {
    		clip._xscale = 100;
    		clip._yscale = clip._xscale;
    	}
    }
    
    this.onEnterFrame = function() {
    	proximity(im1);
    	proximity(im2);
    	proximity(im3);
    	proximity(im4);
    }
    I have tried to convert this into AS3 but the movie just won't work properly can anyone tell me what I doing wrong?

    My AS3 code:
    Code:
    var ia:Array = new Array();
    
    ia = [im1, im2, im3, im4];
    
    for (var i:uint=0; i<4; i++) {
    	ia[i].buttonMode = true;
    	ia[i].addEventListener(Event.ENTER_FRAME, proximity);
    }
    
    function proximity(event:Event):void
    {
    	var x:Number = mouseX;
    	var y:Number = mouseY;
    	var cx:Number = event.target.x;
    	var cy:Number = event.target.y;
    	var prox:Number = Math.sqrt((x-cx)*(x-cx) + (y-cy)*(y-cy));
    	if(prox<100) {
    		event.target.scaleX = 200 - prox;
    		event.target.scaleY = event.target.scaleX;
    	}
    	else {
    		event.target.scaleX = 100;
    		event.target.scaleY= event.target.scaleX;
    	}
    }
    I don't think I'm far away.

    Thanks

    Ricky55

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You're not far away. In AS3, the scale values go from 0 - 1, rather than 0 - 100.

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    12
    I can't believe I missed this!!!!

    Thanks mate, I will amend and try again.

    This AS3 lark is becoming the bane of my life but it is satisfying when it works.

  4. #4
    Junior Member
    Join Date
    Sep 2008
    Posts
    12
    Finally got this working, my code for any one who's interested:

    Code:
    var ia:Array = new Array();
    
    ia = [im1, im2, im3, im4];
    
    for (var i:uint=0; i<4; i++) {
    	ia[i].buttonMode = true;
    	ia[i].addEventListener(Event.ENTER_FRAME, proximity);
    }
    
    function proximity(event:Event):void
    {
    	var x:Number = mouseX;
    	var y:Number = mouseY;
    	var cx:Number = event.target.x;
    	var cy:Number = event.target.y;
    	var prox:Number = Math.sqrt((x-cx)*(x-cx) + (y-cy)*(y-cy)) / 100;
    	if(prox<1) {
    		event.target.scaleX = 2 - prox;
    		event.target.scaleY = event.target.scaleX;
    	}
    	else {
    		event.target.scaleX = 1;
    		event.target.scaleY= event.target.scaleX;
    	}
    }

  5. #5
    Registered User
    Join Date
    Apr 2009
    Posts
    1

    one thing that would help

    good use of the Pythagorean theorem was just looking at your code the only thing i saw missing was what if they overlap. the one you are over should pop to the top so i added this

    ia[i].addEventListener(MouseEvent.MOUSE_OVER, top);

    function top(event:Event):void
    {
    var mc:MovieClip = MovieClip(event.currentTarget);
    //trace(mc.name);
    addChild(mc);
    }

    also i changed the check in the loop to: ia.length
    so the code now looks like this

    var ia:Array = new Array();

    ia = [im1, im2, im3];

    for (var i:uint=0; i<ia.length; i++) {
    ia[i].buttonMode = true;
    ia[i].addEventListener(Event.ENTER_FRAME, proximity);
    ia[i].addEventListener(MouseEvent.MOUSE_OVER, top);
    }

    function top(event:Event):void
    {
    var mc:MovieClip = MovieClip(event.currentTarget);
    //trace(mc.name);
    addChild(mc);
    }

    function proximity(event:Event):void
    {
    var x:Number = mouseX;
    var y:Number = mouseY;
    var cx:Number = event.target.x;
    var cy:Number = event.target.y;
    var prox:Number = Math.sqrt((x-cx)*(x-cx) + (y-cy)*(y-cy)) / 100;
    if(prox<1) {
    event.target.scaleX = 2 - prox;
    event.target.scaleY = event.target.scaleX;
    }
    else {
    event.target.scaleX = 1;
    event.target.scaleY= event.target.scaleX;
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center