A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Writing code in AS file : problem calling function

  1. #1
    Senior Member
    Join Date
    Feb 2002
    Location
    Geneva, CH, Europe
    Posts
    521

    Writing code in AS file : problem calling function

    Hi,

    in a .as file, I have written two different functions, that I call in my main movie. The first one always works, and the second one never...

    Any reason? Do tell me if you need the code.

    Thanks,
    Manojo

  2. #2
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Yes, we'll need to see your code...

    K.

  3. #3
    Senior Member
    Join Date
    Feb 2002
    Location
    Geneva, CH, Europe
    Posts
    521
    ok here goes :
    there are two functions.. one to draw a Rectangle and one for a circle
    the one that works is drawRectangle

    code:


    // ActionScript Document
    degTorad = function(angle){
    return angle/180 * Math.PI;
    }

    function drawCirle (radius,x,y) {

    createEmptyMovieClip("circle",1);
    circle.lineStyle(2,0xFF0000,100);
    trace("hello");

    var x1 = x;
    var y1 = y;
    var ctrlX = x+radius;
    var ctrlY = y+radius;

    x1 += radius;

    circle.moveTo(x1,y1);

    x1 -=radius;
    y1 +=radius;
    circle.curveTo(ctrlX,ctrlY,x1,y1);

    ctrlX = x-radius;

    x1-=radius;
    y1-=radius;
    circle.curveTo(ctrlX,ctrlY,x1,y1);

    }


    function drawRectangle (width,height,round,x,y,rotation){

    createEmptyMovieClip("rectangle",1);
    rectangle.lineStyle(2,0xFF0000,100);

    var r = Math.sqrt(Math.pow(width/2,2)+Math.pow(height/2,2))
    var r1 = Math.sqrt(Math.pow(width/2-round,2)+Math.pow(height/2,2));
    var r2 = Math.sqrt(Math.pow(width/2,2)+Math.pow(height/2-round,2));

    //calculation of important angles
    var presAngle= (rotation == undefined) ? 0 : degTorad(rotation);
    var alpha1 = Math.atan((height/2-round)/(width/2));
    var alpha2 = Math.atan(height/width);
    var alpha3 = Math.atan((height/2)/(width/2-round))

    /**START HERE -- FIRST CURVE AND LINE**/
    presAngle += alpha1;

    var x1 = x + Math.cos(presAngle)*r2;
    var y1 = y + Math.sin(presAngle)*r2;

    rectangle.moveTo(x1,y1);
    presAngle-=alpha1;
    presAngle+=alpha2;
    var ctrlX = x + Math.cos(presAngle)*r;
    var ctrlY = y + Math.sin(presAngle)*r;

    //drawing the curved line
    presAngle -= alpha2;
    presAngle += alpha3;
    x1 = x + Math.cos(presAngle)*r1;
    y1 = y + Math.sin(presAngle)*r1;
    rectangle.curveTo(ctrlX,ctrlY,x1,y1);

    //drawing line to next curve point
    presAngle -= alpha3;
    presAngle += Math.PI-alpha3;
    x1 = x + Math.cos(presAngle)*r1;
    y1 = y + Math.sin(presAngle)*r1;
    rectangle.lineTo(x1,y1);


    /** SECOND CURVE AND LINE **/
    //curved line : again
    presAngle += alpha3 - alpha2;
    ctrlX = x + Math.cos(presAngle)*r;
    ctrlY = y + Math.sin(presAngle)*r;

    //drawing it
    presAngle += alpha2-alpha1;
    var x1 = x + Math.cos(presAngle)*r2;
    var y1 = y + Math.sin(presAngle)*r2;
    rectangle.curveTo(ctrlX,ctrlY,x1,y1);

    //line to next point
    presAngle -= (Math.PI -alpha1);
    presAngle += (Math.PI +alpha1);
    var x1 = x + Math.cos(presAngle)*r2;
    var y1 = y + Math.sin(presAngle)*r2;
    rectangle.lineTo(x1,y1);

    /** THIRD CURVE AND LINE **/
    //curved line 3
    presAngle -= alpha1;
    presAngle += alpha2;
    ctrlX = x + Math.cos(presAngle)*r;
    ctrlY = y + Math.sin(presAngle)*r;

    //draw the curve
    presAngle -= alpha2;
    presAngle += alpha3;
    x1 = x + Math.cos(presAngle)*r1;
    y1 = y + Math.sin(presAngle)*r1;
    rectangle.curveTo(ctrlX,ctrlY,x1,y1);

    //drawing line to next curve point
    presAngle -= alpha3;
    presAngle += Math.PI-alpha3;
    x1 = x + Math.cos(presAngle)*r1;
    y1 = y + Math.sin(presAngle)*r1;
    rectangle.lineTo(x1,y1);

    /**FOURTH CURVE AND LINE**/
    //curved line : again
    presAngle += alpha3 - alpha2;
    ctrlX = x + Math.cos(presAngle)*r;
    ctrlY = y + Math.sin(presAngle)*r;

    //drawing it
    presAngle += alpha2-alpha1;
    var x1 = x + Math.cos(presAngle)*r2;
    var y1 = y + Math.sin(presAngle)*r2;
    rectangle.curveTo(ctrlX,ctrlY,x1,y1);

    //line to next point
    presAngle -= (Math.PI -alpha1);
    presAngle += (Math.PI +alpha1);
    var x1 = x + Math.cos(presAngle)*r2;
    var y1 = y + Math.sin(presAngle)*r2;
    rectangle.lineTo(x1,y1);

    }






    Thanks for your help,

    Manojo

  4. #4
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    And how are you calling that code?

    K.

  5. #5
    Senior Member
    Join Date
    Feb 2002
    Location
    Geneva, CH, Europe
    Posts
    521
    here's what on the 1st frame of my move :

    code:

    #include "drawObjects.as"
    drawRectangle(100,200,30,300,300);
    drawCircle(20,100,100);



    I tried inversing the order, doesn't work either.

    thanks,
    Manojo

  6. #6
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Well, a couple of things I notice in your code - first, you have a typo in your circle function - drawCirle should be drawCircle

    Secondly, both your circle and rectangle clips are being created at the same depth (1) - so only one or the other will exist at any given time...

    K

  7. #7
    :
    Join Date
    Dec 2002
    Posts
    3,518
    ---

  8. #8
    Senior Member
    Join Date
    Feb 2002
    Location
    Geneva, CH, Europe
    Posts
    521
    Hi,

    sorry for that.. I really should have checked the typo error. As for the levels, is that logical to what is possible when you draw these figures? -> indeed, you can have multiple MCs at the same level.

    thanks,

    Manojo

  9. #9
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    You can only have one movieclip per depth level...see:

    http://livedocs.macromedia.com/flash....html#wp122014

    K.

  10. #10
    Senior Member
    Join Date
    Feb 2002
    Location
    Geneva, CH, Europe
    Posts
    521
    hi,
    thanks for that info,
    Manojo

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