Skip to main content
jamesp67108574
Participant
March 11, 2019
Answered

Using Parent Elements

  • March 11, 2019
  • 5 replies
  • 412 views

Hello, for my class I must make a Ruth Goldberg machine and have it operated based on symbols(elements). This is what I have so far.

Here is the code I have so far:

Golf Tee Symbol Frame 1:

import flash.events.MouseEvent;

stop();

addEventListener(MouseEvent.CLICK,startAnimation);

function startAnimation(m:MouseEvent){

gotoAndPlay(2);

}

Golf Tee Symbol (Last Frame):

import flash.events.Event;

var e:Event = new Event("go");

dispatchEvent(e);

Ball Symbol Frame 1:

import flash.events.Event;

stop();

parent.getChildAt(parent.getChildIndex(this)-1).addEventListener("go",startAnimation);

function startAnimation(e:Event){

gotoAndPlay(2);

}

Ball Symbol (Last Frame):

import flash.events.Event;

var ev:Event=new Event("pop");

dispatchEvent(ev);

stop();

Ok, so right now the animation runs correctly. You click on the tee, and it hits the ball and the ball rolls into the hole and stops. Now I need to add another symbol, and I started out just testing how I would do that by having a rectangle move across the screen after the ball makes it in the hole, but nothing I try works. How would I move on?

I got some of the code from my professor but I'm not sure what the 2 represents in "gotoAndPlay(2);". I also don't understand the -1 in the parent symbol and how that would work when I add 8 more symbols. Finally, why does the dispatchEvent go from e to ev?

Any help would be appreciated my professor, I don't understand my professor!

    This topic is closed to new replies. Start a new post to keep the conversation going.
    Correct answer JoãoCésar17023019

    Hi.

    What I understand is that your teacher wants to create a chain reaction in which the click on the golf tee kicks off the whole process.

    Then, in the last frame of each symbol instance of your machine, you fire up an event like "go" and "pop".

    After that, every single subsequent instance that should react must be listening to previous event of the previous instance in the queue.

    So you have:

    GolfTee symbol:

    [First frame]

    import flash.events.MouseEvent;

    function startAnimation(m:MouseEvent)

    {

        gotoAndPlay(2);

    }

    stop();

    addEventListener(MouseEvent.CLICK, startAnimation);

    [Last frame]

    import flash.events.Event;

    var e:Event = new Event("go"); // fires up the "go" event that will be listened by the Ball instance

    dispatchEvent(e);

    Ball symbol:

    [First frame]

    import flash.events.Event;

    function startAnimation(e: Event)

    {

        gotoAndPlay(2);

    }

    stop();

    MovieClip(root).tee.addEventListener("go", startAnimation); // listen to the "go" event

    [Last frame]

    import flash.events.Event;

    var ev:Event = new Event("pop"); // fires up the "pop" event that will be listened by the Rec instance

    stop();

    dispatchEvent(ev);

    Rec symbol

    [First frame]

    import flash.events.Event;

    function startAnimation(e: Event)

    {

        gotoAndPlay(2);

    }

    stop();

    MovieClip(root).ball.addEventListener("pop", startAnimation); // listen to the "pop" event

    [Last frame]

    stop();

    // so on...

    About your questions:

    - gotoAndPlay(2) is a instruction to play the current timeline starting from the frame 2. As this code lives in the Ball instance, it will play the timeline of Ball instance.

    - With this line...

    parent.getChildAt(parent.getChildIndex(this)-1).addEventListener("go",startAnimation);

    ... your teacher is trying to get the tee symbol in the main timeline. It will work as long as you don't change the number of instances in the main timeline or the z index of the ball and the tee. I recommend you to use MovieClip(root).nameOfYourInstance like I did in the sample above. Because it doesn't depend on the number of children or z index.

    - Your teacher just didn't follow a pattern. But the name of the Event variables don't really need to be the same. You can call them e and/or ev and/or event. Just try to be consistent.

    Please let us know if this clarifies things for you.

    Regards,

    JC

    5 replies

    JoãoCésar17023019
    Community Expert
    JoãoCésar17023019Community ExpertCorrect answer
    Community Expert
    March 12, 2019

    Hi.

    What I understand is that your teacher wants to create a chain reaction in which the click on the golf tee kicks off the whole process.

    Then, in the last frame of each symbol instance of your machine, you fire up an event like "go" and "pop".

    After that, every single subsequent instance that should react must be listening to previous event of the previous instance in the queue.

    So you have:

    GolfTee symbol:

    [First frame]

    import flash.events.MouseEvent;

    function startAnimation(m:MouseEvent)

    {

        gotoAndPlay(2);

    }

    stop();

    addEventListener(MouseEvent.CLICK, startAnimation);

    [Last frame]

    import flash.events.Event;

    var e:Event = new Event("go"); // fires up the "go" event that will be listened by the Ball instance

    dispatchEvent(e);

    Ball symbol:

    [First frame]

    import flash.events.Event;

    function startAnimation(e: Event)

    {

        gotoAndPlay(2);

    }

    stop();

    MovieClip(root).tee.addEventListener("go", startAnimation); // listen to the "go" event

    [Last frame]

    import flash.events.Event;

    var ev:Event = new Event("pop"); // fires up the "pop" event that will be listened by the Rec instance

    stop();

    dispatchEvent(ev);

    Rec symbol

    [First frame]

    import flash.events.Event;

    function startAnimation(e: Event)

    {

        gotoAndPlay(2);

    }

    stop();

    MovieClip(root).ball.addEventListener("pop", startAnimation); // listen to the "pop" event

    [Last frame]

    stop();

    // so on...

    About your questions:

    - gotoAndPlay(2) is a instruction to play the current timeline starting from the frame 2. As this code lives in the Ball instance, it will play the timeline of Ball instance.

    - With this line...

    parent.getChildAt(parent.getChildIndex(this)-1).addEventListener("go",startAnimation);

    ... your teacher is trying to get the tee symbol in the main timeline. It will work as long as you don't change the number of instances in the main timeline or the z index of the ball and the tee. I recommend you to use MovieClip(root).nameOfYourInstance like I did in the sample above. Because it doesn't depend on the number of children or z index.

    - Your teacher just didn't follow a pattern. But the name of the Event variables don't really need to be the same. You can call them e and/or ev and/or event. Just try to be consistent.

    Please let us know if this clarifies things for you.

    Regards,

    JC

    jamesp67108574
    Participant
    March 12, 2019

    Thank you so much! I started using MovieClip(root).nameOfYourInstance rather than the parents. That made it a lot easier and I was able to add 4 more working symbol animations!

    JoãoCésar17023019
    Community Expert
    Community Expert
    March 12, 2019

    Amazing! You're welcome!

    I'm really glad you've managed to understand the logic and to continue the exercise!

    Have a nice learning and coding.

    Legend
    March 11, 2019

    jamesp67108574  wrote

    I got some of the code from my professor but I'm not sure what the 2 represents in "gotoAndPlay(2);".

    Just so we're all understanding correctly, your class has been assigned an ActionScript programming project, with apparently absolute zero instruction given on the language itself?

    Also I never knew Rube Goldberg had a wife who also made machines.

    jamesp67108574
    Participant
    March 11, 2019

    So can you help or not?