Search code examples
actionscript-3flash-cs5google-search

How to implement custom google search for ActionScript 3.0


I was trying to implement custom google search in my course-ware that developed in flash. I define a class named 'Main' (Main.as) and put my search code there. But the problem is, that Main class has a conflict with the other codes containing in my course-ware (i've combo box & other basic navigation in course-ware). i have no idea how to resolve it. is there any way to put that code in timeline layer? help please.. thanks. here is my Main class:

package 
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.KeyboardEvent;
    import flash.ui.Keyboard;
    import flash.net.navigateToURL;
    import flash.net.URLRequest;

    public class Main extends Sprite
    {
        public function Main():void
        {
            searchButton.addEventListener(MouseEvent.MOUSE_UP, google);
            addEventListener(KeyboardEvent.KEY_DOWN, google);
            searchTerms.addEventListener(MouseEvent.MOUSE_DOWN, selectText);
        }

        private function google(e:*):void
        {
            if(e.type == "mouseUp")
            {
                navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
            }
            else if(e.keyCode == Keyboard.ENTER)
            {
                navigateToURL(new URLRequest("http://www.google.com/search?q=" + searchTerms.text));
            }
        }

        private function selectText(e:MouseEvent):void
        {
            searchTerms.setSelection(0, searchTerms.length);
        }
    }
}

Solution

  • From what you shared & the messages here, I assume you are trying to add code through the flash IDE while at the same time having a document class called Main, for your application.

    There are lots of ways you may get around this.

    Assuming you want keep your timeline code unaltered while adding an instance of the Main class :

    • Add an empty movie clip to the library, say SearchClass.

    • Go to properties of the movie clip & click export for actionscript.

    • Set the class to Main. Do make sure where Main.as lies outside with respect to the fla.

    • Add this movieclip onto the stage, on any frame or layer.

    • Do remember to clear the document class field.


    As a side note, You should also rename the class Main to something meaningful, like SearchClass.

    If you wonder about setting the class vs base class,

    We use base class only when you wish to extend the features of the class (by adding UI elements for eg).


    You could also simply call the class directly from the timeline code as :

    var main:Main = new Main();
    
    addChild(main);
    

    Just make sure the Main.as file lies next to the fla... ie make sure the path is available to compiler.