A Flash Developer Resource Site

Results 1 to 20 of 20

Thread: AS3. Enter Frame... grrrh.

  1. #1
    Member
    Join Date
    Oct 2005
    Posts
    51

    AS3. Enter Frame... grrrh.

    I've just gone through some of the AS3 documentation from Adobe, as well as FK... but, I'm still pulling my hair out trying to jump from AS2 to 3. Appreciate any help with this...

    basically, I've got a mc in the root called "text1_mc" which is 180 frames, when the playhead gets to 180 within the mc, I'm trying to get the root playhead to jump to frame 5. This is the code I've got on frame 1 of the root:
    PHP Code:
    text1_mc.addEventListener(Event.ENTER_FRAMErepeat);
    function 
    repeat (myevent:Event):void 
            if (
    this.currentFrame == 180
            {
    root.gotoAndPlay(5);
            }

    what am I doing wrong here?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Not telling us what error you got.

    But, the actual error is probably "possibly undefined method gotoAndPlay", right? Because root is typed as DisplayObject, which does not have a gotoAndPlay function. You need to cast it to MovieClip to use that.

    Code:
      (root as MovieClip).gotoAndPlay(5);

  3. #3
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    Maybe something like this.

    PHP Code:
    text1_mc.addEventListener(Event.ENTER_FRAMErepeat);

    function 
    repeat(myevent:Event):void
    {
        if (
    myevent.target.currentFrame == myevent.target.totalFrames)
        {
            
    text1_mc.removeEventListener(Event.ENTER_FRAMErepeat);
            
    MovieClip(root).gotoAndPlay(5);
        }
    // end of repeat 
    Don't use this.currentFrame because with "this" you are telling flash to watch the timeline where you have your code and not the timeline of the movie clip you are referring too.
    Last edited by fx.barrett; 02-06-2008 at 04:45 PM.



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  4. #4
    Member
    Join Date
    Oct 2005
    Posts
    51
    Interesting. That current frame = total frames is interesting.
    forgot about "stage". the code work up to a point. The first frame (of the root) mc (text1_mc) plays, then at frame 180 of the text1_mc, the playhead in the root jumps (gotoAndPlay(text_2)), where I've got the same code sniffing for the frame 180 of the mc text2_mc... but, then I get this error msg:

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at iam_test1_fla::MainTimeline/text_1()

    The code on frame 1 ("text_1") is:
    PHP Code:
    stage.addEventListener(Event.ENTER_FRAMEtext_1);
    function 
    text_1 (myevent:Event):void 
            if (
    text1_mc.currentFrame == 180
            {
    text1_mc.removeEventListener(Event.ENTER_FRAMEtext_1);
                
    gotoAndPlay("text_2");
            }

    The code on the frame 10 ("text_2") where the playhead jumps when it reaches frame 180 of the text1_mc is:
    PHP Code:
    stop();
    stage.addEventListener(Event.ENTER_FRAMEtext2);
    function 
    text2 (myevent:Event):void 
            if (
    text2_mc.currentFrame == 180
            {
    this.gotoAndPlay("text_1");
            }

    basically, I'm just testing a loop at this point, but I get that error msg that I reported above... over, and over, and over, and over. Obviously, the Enter Frame needs to be removed,... yet I tried that and the msg still persisted.

  5. #5
    Member
    Join Date
    Oct 2005
    Posts
    51
    ahhhhh. fixed. forgot about a couple of my syntax friends:
    (code for frame 10 - "text_2")
    PHP Code:
    stop();
    stage.addEventListener(Event.ENTER_FRAMEtext2);
    function 
    text2 (myevent:Event):void 
            if (
    text2_mc.currentFrame == 180
            {
                
    stage.removeEventListener(Event.ENTER_FRAMEtext2);
                
    gotoAndPlay("text_1");
            }

    Thanks for the help.

  6. #6
    trace("AKA: Biro Barna");
    Join Date
    Oct 2007
    Location
    RO.Timişoara
    Posts
    1,403
    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at iam_test1_fla::MainTimeline/text_1()

    If you don't have the movie clip initialized where you have the code then it won't see it. In order to make it work, place the code on a separate frame ( same spot where the movie clip first appears ) or make the movie clip invisible for the first part of the animation and make it visible again once mc_text1 finishes...



    | Windows MSN: birobarna [at] hotmail [dot] com | Skype: barna.biro |
    WebLog: http://blog.wisebisoft.com/ |
    | Software Developer / Flash & Flex Developer | Student ( Computer Science ) | Interested in: Sharing Knowledge |
    |
    Romanian Adobe Flash, Flex, AIR Forum: http://www.flashforum.ro/
    | By perseverance the snail reached the ark. |


  7. #7
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    ok I have a similar problem to this, where I can't use this.currentFrame, but I dont know how reference the current sprite from the stage. On the main stage I have a movieclip (my contianer) called mcContainer, then through my main class file I create multiple sprites of the class miniSprites and addchild to mcContainer. Now inside one of the miniSprites I want to to see if the movieclip this class is tied to is in frame 2 before running a portion of the code. How ever if I do this:


    private function enterFrame(e:Event):void{
    if(e.target.currentFrame == 2){
    ......
    }
    or if I do this:
    if(this.currentFrame == 2){
    ......
    }
    so what do I do to reference the actual timeline of that sprite?

  8. #8
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Sprite's don't have timelines...

  9. #9
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    yea your right, but why is it still moving on timline when I go this.gotoAndStop(2);

    in my main class I do this:

    public var miniSprite:Sprite;

    then in the min function:

    miniSprite = new mcStuff(1);
    playerClip.addChild(miniSprite);

    mcStuff is a class that has linkage to a movieclip with multiple frames, so in a sence the sprite is holding a movieclip (the mcStuff class extends to MovieClip)?

  10. #10
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Ok - this is a sqaures/rectangles issue...MovieClips extend Sprite so at the sprite level you can use them interchangeably, but the inheritance only goes one way:

    PHP Code:
    var a:Sprite = new Sprite();
    var 
    b:Sprite = new MovieClip();
    var 
    c:MovieClip = new Sprite();  //  FAIL
    var d:MovieClip = new MovieClip(); 
    Basically you're telling the compiler what type the variable is so it's only able to validate the functionality of that class (Sprite), not whatever subclass you decide to put into it (MC).

    PHP Code:
    //  buttonMode is a Sprite property
    a.buttonMode true;
    b.buttonMode true;
    d.buttonMode true;

    //  nextFrame is a MC method
    a.nextFrame();  //  FAIL
    b.nextFrame();  //  FAIL
    c.nextFrame(); 

  11. #11
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by neznein9 View Post
    Ok - this is a sqaures/rectangles issue...MovieClips extend Sprite so at the sprite level you can use them interchangeably, but the inheritance only goes one way:

    PHP Code:
    var a:Sprite = new Sprite();
    var 
    b:Sprite = new MovieClip();
    var 
    c:MovieClip = new Sprite();  //  FAIL
    var d:MovieClip = new MovieClip(); 
    Basically you're telling the compiler what type the variable is so it's only able to validate the functionality of that class (Sprite), not whatever subclass you decide to put into it (MC).

    PHP Code:
    //  buttonMode is a Sprite property
    a.buttonMode true;
    b.buttonMode true;
    d.buttonMode true;

    //  nextFrame is a MC method
    a.nextFrame();  //  FAIL
    b.nextFrame();  //  FAIL
    c.nextFrame(); 

    Hey just a question regarding that code you wrote at the top, you can write "a:Sprite = new MovieClip();"? So then what, you can use the properties of a MovieClip to create your object but yet 'a' will be treated as a Sprite?

  12. #12
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Yes - exactly.

    A more relevant example would be if you wanted to fade out everything on stage...you really don't know what kinds of objects are out there (sprites, mcs, textfields, whatever) - but you know that they are all subclasses of DisplayObject, and .alpha is a property of that class so you could just use a variable typed as DisplayObject to reference each child without caring about what each specific child is.

  13. #13
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Interesting. I'll be going to sleep smarter tonight :P

  14. #14
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    [QUOTE=neznein9;4166795]Ok - this is a sqaures/rectangles issue...MovieClips extend Sprite so at the sprite level you can use them interchangeably, but the inheritance only goes one way:

    PHP Code:
    var a:Sprite = new Sprite();
    var 
    b:Sprite = new MovieClip();
    var 
    c:MovieClip = new Sprite();  //  FAIL
    var d:MovieClip = new MovieClip(); 
    PHP Code:
    //  buttonMode is a Sprite property
    a.buttonMode true;
    b.buttonMode true;
    d.buttonMode true;

    //  nextFrame is a MC method
    a.nextFrame();  //  FAIL
    b.nextFrame();  //  FAIL
    c.nextFrame(); 
    ok in your example you have b.nextFrame(); is failing, but in my example

    my spirte = new MovieClip(); [miniSprite = new mcStuff(1);]

    in the mcStuff class I can go this.gotoAndStop(2).

    How come I can use the gotoAndStop property? isn't it the same thing as your b?

  15. #15
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Is there a reason it as to be a sprite? Or could you simply change everything to movieclips

  16. #16
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    the gotoAndStop works either way, if its a sprite or a moviecip, but I can't figur out why its working when its a sprite. But my main problem is this.currentFrame

    public var miniSprite:Sprite;

    then in the min function:

    miniSprite = new mcStuff(1);
    playerClip.addChild(miniSprite);

    in mcStuff I do a command that says

    this.gotoAndStop(2);

    then before running a portion of the code I go:

    if(this.currentFrame == 2){
    ...code that references a specific movieclip on that frame....
    }

    the problem is that this.currentFrame = 2 before the timeline actually gets to the frame 2, so I need to reference the specific MovieClip mcStuff from the root stage, but how can I do that? Whats the path I take?

    root.playerClip.this?

  17. #17
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    any help?

  18. #18
    Lunatic
    Join Date
    Nov 2002
    Location
    AS3 Forum
    Posts
    342
    Quote Originally Posted by shamimsaad03 View Post
    the gotoAndStop works either way, if its a sprite or a moviecip, but I can't figur out why its working when its a sprite. But my main problem is this.currentFrame

    public var miniSprite:Sprite;

    then in the min function:

    miniSprite = new mcStuff(1);
    playerClip.addChild(miniSprite);

    in mcStuff I do a command that says

    this.gotoAndStop(2);

    then before running a portion of the code I go:

    if(this.currentFrame == 2){
    ...code that references a specific movieclip on that frame....
    }

    the problem is that this.currentFrame = 2 before the timeline actually gets to the frame 2, so I need to reference the specific MovieClip mcStuff from the root stage, but how can I do that? Whats the path I take?

    root.playerClip.this?

    I am not sure what you are trying to do. If you are trying to access a movieClip on the stage from inside another movie clip, you would go:

    MovieClip(root).nameOfYourMovieClip.whateverFuncti on();

    Can you explain your problem better? Are you getting an error? Could we see more code?

  19. #19
    Senior Member
    Join Date
    Mar 2003
    Location
    Canada
    Posts
    166
    all im trying to do is access a movieclip from stage from another class. But That movieclip is dynamically created.

    root.dynamicMC

    i create the movieclip in my document class file, by going

    someMovieclip = new dynamicMC();

    where dynamic MC is linked to a specific MovieClip.

    Now how do I access someMovieClip from another class?

  20. #20
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Ok - back to squares and rectangles.

    You can make dynamicMC a public parameter of your document class -- that's great, except the reference you use to target your document class needs to be of the same type!

    Calling someObj.parent might give you a reference up to the document class but the .parent property returns a DisplayObjectContainer - anything that comes back as a parent will be upcast to that data type and you can't use any more specific methods or properties.

    The hackey way would be this:

    PHP Code:
    import MyDocumentClass;

    //  ...

    MyDocumentClass(parent).dynamicMC 
    The correct way would be to dispatch an event letting the document class know you want to change something and having the higher class handle the change - this preserves the decoupling in your code and makes your classes more re-usable in the future.

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