views:

320

answers:

1

I am building your standard slideshow flash header for a web page.

There are three main parts:

  • The Slideshow class
  • A controller class that is used as the projects Document Class
  • Some linking timeline code.

The slideshow class has all the functionality, so I used the Document class to create a new instance of the slideshow and keep a property variable called slideshow that keeps a reference the Slideshow instance.

import flash.display.MovieClip;
import flash.events.Event;

public class Header extends MovieClip
{
    public var slideshow:Slideshow;
    public function CSYC_Header()
        {
            var picturesURL:String = "images/pictures.xml";
            var picturesURLFVar:String = root.loaderInfo.parameters.pictures;
            picturesURL = picturesURLFVar ? picturesURLFVar : picturesURL;
            slideshow = new Slideshow(picturesURL, Slideshow.FADE);             
            slideshow.init();
            addChild(slideshow);
        }

    public function hello():void{trace("Hello");}
}

My next step now is to use Adobe Flash Professional to draw some play and stop buttons, and then link thier click events to call slidshow.play()/.pause(). This code is just place in the timeline:

import flash.events.MouseEvent;

pause_control_btn.addEventListener(MouseEvent.CLICK, pauseClicked);
play_control_btn.addEventListener(MouseEvent.CLICK, playClicked);
addChild(pause_control_btn);
addChild(play_control_btn);

function pauseClicked(e:MouseEvent):void 
{
    //the play and pause buttons are on the stage and have the following names as
    // thier instance names: pause_control_btn, play_control_btn
    pause_control_btn.alpha = 0;
    play_control_btn.alpha = 0.37;
    slideshow.pause();
}

function playClicked(e:MouseEvent):void 
{
    pause_control_btn.alpha = 0.37;
    play_control_btn.alpha = 0;
    slideshow.play();
}

Despite me being able to call regular methods that are in the Doc Class from the timeline, I can not call properties without the following error, for example when i say slideshow.play():

1061: Call to a possibly undefined method play through a reference with static type com.example.test:Slideshow.

So am I missing something obvious, or will I have to make a method on my document class every time I want to wire an event to call an object in my Document Class?

+1  A: 

There's no need to put that code for the buttons on the timeline; you can reference those objects by their instance names from within the Document Class. That'd be the easiest solution, avoiding the timeline altogether, I think.

Otherwise possibly a call to parent.slideshow, or root.slideshow (though I think root is AS2, I don't quite remember) would give you access to that instance from the timeline. The former option is probably still the better option, and keeps your code in one place.

Hope that helps.

debu
Great point! I slept on it last night, and now I feel kinda stupid, since it ended up being an error in the Slideshow class. In conclusion: you can access anything in the document class from the timeline and any thing from your time line in the document class (anything as in properties and methods.) For this example: from the timeline i can call slideshow.play() (if you don't forget to save your changes of adding the the needed methods :) ) and from the document class I can call pause_control_btn.addEventListener(MouseEvent.CLICK, pauseClicked);as well. So everything works!!
Slickt10
Ahh, so that was the problem ;) I've had the 'not saved the file' problem far more than I should! Glad you've fixed it though, and good to know that accessing stuff in the Document Class is possible from the timeline. Thanks for the info!
debu