Help me to understand this part of code

Hi :slight_smile:
new to the forum
hope we grow together in our world

these days i am reading this book:

it is really good for beginners, but still need help from the pro :slight_smile:

i am stuck in some part of this code, i do not want to move to the next part until i understand this one, please help me to read it:

THE CODE:

[AS]//stop main timeline from playing on its own through to second scene
stop();

//stop movie clip from playing on its own
pages.stop();

//button event listeners
onePlus.addEventListener(MouseEvent.CLICK, onOnePlusClick, false, 0, true);
output.addEventListener(MouseEvent.CLICK, onOutputClick, false, 0, true);
labelCheck.addEventListener(MouseEvent.CLICK, onLabelCheckClick, false, 0, true);

/*
i did not understand this part, what i know is this talking to a movie clip “pages”, and move the play head to label called "page1), also this part of code is connected to “getFrame” function at the bottom of the code. my question here is: why the name of the movie is there again after the label “page1”.
*/
//jump to a frame relative to the location of a label
function onOnePlusClick(evt:MouseEvent):void {
pages.gotoAndStop(getFrame(“page1”,pages) + 4);
}

//output simple variants of movie, scene, label, and frame information
function onOutputClick(evt:MouseEvent):void {
trace(“The main movie has " + scenes.length + " scenes.”);
trace(“The current scene is '” + currentScene.name + “’.”);
trace(“It has " + currentScene.numFrames + " frame(s),”);
trace(" and " + currentScene.labels.length + " label(s). “);
trace(“The second scene’s first label is '” + scenes[1].labels[0].name + “’,”);
trace(” which is in frame " + scenes[1].labels[0].frame + “.”);
trace(“Movie clip ‘pages’ has " + pages.currentLabels.length + " labels.”);
trace(“Its last label is '” + pages.currentLabels.pop().name + “’.”);
}

// i did not understand this part
//check to see if a frame label exists
function onLabelCheckClick(evt:MouseEvent):void {
trace(isFrameLabel(“page3”, pages));
}

// i did not understand this part
//accept mc and frame label and return frame number in which label resides
function getFrame(frLabel:String, mc:MovieClip):int {
for (var i:int = 0; i<mc.currentLabels.length; i++) {
if (mc.currentLabels*.name == frLabel) {
return mc.currentLabels*.frame;
}
}
return -1;
}

// i did not understand this part
//accept mc and frame label and return true if the label exists, false if not
function isFrameLabel(frLabel:String, mc:Object):Boolean {
for (var i:int = 0; i<mc.currentLabels.length; i++) {
if (mc.currentLabels*.name == frLabel) {
return true;
}
}
return false;
}[/AS]

i tried many times to understand but i could not, also is it good way to stop until i understand the difficult part of AS3 until i understand or go read on then come back?

Thank all for your time

Hey String3

Basically it does the following:
If you click on the object “onePlus” it moves to the frame with the label “page1”, which you have already understood.

the function has been written really short, so maybe this helps you to understand it:


function onOnePlusClick(evt:MouseEvent):void {
//declare a integer called frameNumb
    var frameNumb:int;
//get the frame number of the label "page1" from the Object "pages" and store it in frameNumb
    frameNumb = getFrame("page1",pages);
//add 4 (frames) to the number you received in the line before
    frameNumb += 4;
//go to the frame you calculated in the two rows before
    pages.gotoAndStop(frameNumb);
}

in order to receive the frameNumber of a label they use this function:


//function needs to receive a string (frame label) and a movieclip, the function returns an integer
function getFrame(frLabel:String, mc:MovieClip):int {
//go through every frame label the movieclip has
    for (var i:int = 0; i<mc.currentLabels.length; i++) {
//if the label correspond...
        if (mc.currentLabels*.name == frLabel) {
//...return the frame of the label (the function exits after it returns a value)
            return mc.currentLabels*.frame;
        }
    }
//if the for loop has finished without finding the label, it returns -1
    return -1;
}

the function onLabelCheckClick calls the function “isFrameLabel”.
If you have a question here, please ask again!

the function “isFrameLabel” basically does the exact same thing like the function “getFrame” but it returns true if the frame label is found and false if not.

Hope that clears things a little up.

I think it’s a good idea to figure out things you didn’t understand. But sometimes you can get worked up in a topic and you just don’t see any progress, in that case i would recommend to let it go for a couple of days, focus on something else and then get back to it when your mind is clear again.

If there are any further questions don’t hesitate to post

Bye
Carlo