PDA

View Full Version : AS2 to AS3 Problems again...



Ricky55
September 16th, 2008, 06:36 AM
I have this AS2 code that works fine



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(sanford);
proximity(dukes);
proximity(magnum);
}


I've managed to get this in AS3 but its not working properly, can anyone tell me where I'm going wrong please. I have tried with this but I just find AS3 really hard.



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;
}
}


Thanks

theCodeBot
September 16th, 2008, 06:54 AM
I have this AS2 code that works fine



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(sanford);
proximity(dukes);
proximity(magnum);
}
I've managed to get this in AS3 but its not working properly, can anyone tell me where I'm going wrong please. I have tried with this but I just find AS3 really hard.



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;
}
}

Thanks

That looks like it'll do it, assuming im1, im2, etc. Are references to the movieClips (magnum, etc) before you put them in the array.

Ricky55
September 16th, 2008, 10:53 AM
well i have four movie clip thumbnails on the stage called im1 im2 etc and it doesn't work I've not called them Magnum etc in my AS3 example.

The thumbnails are just green boxes for now, when I test the movie the whole movie just turns green and thats all it does.

Any ideas?

creatify
September 16th, 2008, 08:35 PM
see attached, comments supplied - hope this helps.

Ricky55
September 17th, 2008, 01:58 PM
Thanks mate I really appreciate your trouble.

Can I just ask you a couple of questions:

frameRunner.addEventListener(Event.ENTER_FRAME, proximity, false, 0, true);

What the false 0 true mean?

Also when you say its best to use one frame event, is this for performance reasons? and am I using one or four here?

Sorry if my questions are basic but I'm still learning.

creatify
September 17th, 2008, 07:12 PM
no worries, I think we're all still learning AS3...

the ,false, 0, true... you don't really have to modify those. Those are optional parameters when adding a listener (addEventListener) to an object. Without going into tons of detail... the last parameter (true) allows the event to be weak referenced. This means if the object that is associated with the event, is destroyed, that added listener will be cleared from memory vs. getting stuck. It has no direct effect on your code - see here for more (http://www.kirupa.com/forum/showthread.php?p=1919956) or google "Flash AS3 weak reference events".

And yes, the way I set it up, there is only one frame event running which, in this case, should improve performance over four running.

When it triggers the proximity function which then loops through all of your clips and sets the scale. This is in contrast to setting up four frame events, one for each object. If you end up with many more objects, you might want to run some tests to see which is faster, one frame event that loops through your objects, or a frame event for each object. Nonetheless, you currently do have this frame event always running - depending on the rest of your app, this may hog some memory and you may want to find a way to only turn the frame runner on when the mouse is within that general area. Just some things to think about.

Hope the info helps!