A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: a few efficiency questions

  1. #1
    aka Cody
    Join Date
    Sep 2004
    Location
    AZ, US
    Posts
    48

    a few efficiency questions

    I've been wondering about a few things that I could never really find the answer to.

    Would it be better to put all code such as onClipEvent(), on...(), etc in one movie clip?

    Is there any kind of (dis)advantage to using "my_mc.onEnterFrame = function()" on a frame or just "onClipEvent(enterFrame)" on the object?

    What about any other code that can be put on the object OR its frame. Is either better?

    Is there any (dis)advantage to looping code on one frame or just having the next frame move back to the last?

    Anything else I should know that might help make the code more efficient?

    Thanks
    Think of something clever and funny and imagine it here.

  2. #2
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    Code:
    Would it be better to put all code such as onClipEvent(), on...(), etc in one movie clip?
    This will improve efficiency only a tiny bit. Other things, like hitTest, can make a very big difference (e.g., put all hittable objects inside one movie clip, and run hitTest against that).

    Code:
    Is there any kind of (dis)advantage to using "my_mc.onEnterFrame = function()"
     on a frame or just "onClipEvent(enterFrame)" on the object?
    The first lets you (1) use the same function for many objects (saves memory and programming effort, no significant speed effect), and (2) change the function as needed (e.g., remove the onEnterFrame function when it is no longer needed, so it doesn't continue to run forever.)

    Code:
    What about any other code that can be put on the object OR its frame. Is either better?
    From the point of view of program construction and maintenance, code in a frame is almost always better.

    Code:
    Is there any (dis)advantage to looping code on one frame or just having the next frame move back to the last?
    Not sure quite what you mean by looping code. A for or while loop gets executed all at once, before the frame it's on is displayed. A timeline loop like the one you describe happens only at the frame rate.
    Code:
    // happens all in one frame
    for(var i = 0; i < 100; i++) {
        trace(i);
    }
    
    
    // happens over 8 seconds (at 12 fps)
    //in frame n - 1:
    i = 0;
    
    // in frame n:
    trace(i++);
    
    // in frame n + 1:
    if(i < 100) gotoAndPlay(_currentframe - 1);
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  3. #3
    aka Cody
    Join Date
    Sep 2004
    Location
    AZ, US
    Posts
    48
    Thank you for clarifying that for me and I guess your sig helped a bit as well. hehe

    btw, those are some REALLY nice flash examples you have on your site there.
    Care to let me in on the secret of that draggable cord example?
    Think of something clever and funny and imagine it here.

  4. #4
    ActionScript Insomniac
    Join Date
    Jan 2003
    Location
    43d03.21'N, 89d23.65'W
    Posts
    1,173
    Thanks!

    Here's the code for the string. Guess where it goes....

    Code:
    var plug = _root.createEmptyMovieClip("plug", 1);
    plug.lineStyle(10, 0x880000);
    plug.lineTo(1,0);
    plug._x = plug._y = 200;
    plug.onPress = function() {
    	this.onMouseMove = function() {
    		this._x = _root._xmouse;
    		this._y = _root._ymouse;
    		drawCord();
    		updateAfterEvent();
    	}
    }
    
    plug.onRelease = plug.onReleaseOutside = function() {
    	delete this.onMouseMove;
    }
    
    _root.createEmptyMovieClip("canvas", 2);
    
    drawCord = function(){
    	var angle, x, y;
    	canvas.clear();
    	canvas.lineStyle(2, 255);
    	canvas.moveTo(plug._x, plug._y);
    	points[0] = {x:plug._x, y:plug._y};	
    	for(var i = 1; i < points.length - 1; i++) {
    		angle = Math.atan2(points[i].y - points[i-1].y, points[i].x - points[i-1].x);
    		x = points[i-1].x + segmentLength * Math.cos(angle);
    		y = points[i-1].y + segmentLength * Math.sin(angle);
    		points[i].x = x;
    		points[i].y = y;
    		canvas.lineTo(x, y);		
    	}
    }
    
    var segmentLength = 5;
    var points = new Array();
    for(var i = 0; i < 80; i++) {
    	points[i] = {x:5*i, y:Math.sin(i/20)};
    }
    drawCord();
    EDIT: removed the !%@#$# smilies from the code...
    Last edited by mkantor; 03-10-2005 at 11:59 PM.
    Unless otherwise specified, all code goes in Frame 1 of main timeline. FlashGizmo.com

  5. #5
    aka Cody
    Join Date
    Sep 2004
    Location
    AZ, US
    Posts
    48
    Wow, I would have thought there would be a lot more code.

    Thanks, I learn best buy example.
    Think of something clever and funny and imagine it here.

  6. #6
    aka Cody
    Join Date
    Sep 2004
    Location
    AZ, US
    Posts
    48
    Back to one of my questions
    Is there any (dis)advantage to looping code on one frame or just having the next frame move back to the last?
    I actually worded that wrong as to what I meant, but I came across this question recently.
    Take for example an action for deceleration. I've seen this a lot by making an empty mc with code in its first frame and then prevFrame() on the second frame.
    I just put the code in an enterFrame clip event.

    Is either type of "loop" better?
    I guess in a way they are both kinda loops.

    Thanks
    Think of something clever and funny and imagine it here.

  7. #7
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I prefer to put all my scripts in one frame rather than to look for scripts in buttons, movieclips etc. That's why I prefer onEnterFrame=function or onPress=function for buttons rather than onClipEvents or on(press). It's a matter of choice. In the past flash versions that was not possible.

    Only for php parsing it makes a difference. But that is a special case.
    - The right of the People to create Flash movies shall not be infringed. -

  8. #8
    aka Cody
    Join Date
    Sep 2004
    Location
    AZ, US
    Posts
    48
    I see, but then would you rather put the code in the first frame in an onEnterFrame=function or just have the code there without the function and have the next frame loop back to the last?

    From examples i've seen, the looping back method seems to be more popular.

    Thanks
    Think of something clever and funny and imagine it here.

  9. #9
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,449
    I do not do looping back, because all functions etc is all in one frame, sometimes up to a 1000 lines. Looping back would be a disaster. Also using the new class (.as) system would not allow looping back but have the script in one frame.
    As I said only for sending var to php I do it differently, because I learnt it a certain way. Otherwise it is personal prference.
    - The right of the People to create Flash movies shall not be infringed. -

  10. #10
    aka Cody
    Join Date
    Sep 2004
    Location
    AZ, US
    Posts
    48
    ok Thanks for the input.
    Think of something clever and funny and imagine it here.

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