A Flash Developer Resource Site

Results 1 to 15 of 15

Thread: what is actionscript3 version of script below?

  1. #1
    Senior Member
    Join Date
    Sep 2001
    Posts
    126

    what is actionscript3 version of script below?

    Hi! does anyone know the actionscript 3 version of:

    onClipEvent (load) {
    mySound = new Sound();
    mySound.loadSound("sound2.mp3", false);
    }


    apparently, this is actionscript 2, and for my movie I have to export in 3..

    thanks!

  2. #2
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    Code:
    function startSound():void{
       var mySound:Sound = new Sound(new URLRequest('sound2.mp3');
    }
    addEventListener(Event.ENTER_FRAME, startSound);
    Let's go through it line by line. Line 1: declares a new function, startSound. () means no variables are being passed to it. :void means that it doesn't return anything.

    Line 2. var mySound:Sound declares a new variable which is a Sound. It equals a new instance of the Sound class that excepts the argument of a URLRequest. Here I created a new URLRequest that accepts a url to load, sound2.mp3.

    Line 4: adds an event listener to the application that listens for the ENTER_FRAME event. When it receives the enter_frame event it calls the startSound function.
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  3. #3
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    you really might want to remove that event listener. and you code is going to throw an error. invalid number of parameters for the function.

    also, i wouldn't use the ENTER_FRAME at all since he wants the function to execute when the clip loads... so there are other better ways to go about that.

    And you also would probably want to declare the variable OUTSIDE that function or youll never be able to access it again and it'd be available for garbage collection immediately. Which is a bad thing for that.

    But to fix your code, Kid_Charlie:
    Code:
    var mySound:Sound;
    function startSound($event:Event):void
    {
        removeEventListener(Event.ENTER_FRAME, startSound);
    
        mySound = new Sound(new URLRequest("sound2.mp3");
    }
    addEventListener(Event.ENTER_FRAME, startSound);

  4. #4
    Senior Member
    Join Date
    Sep 2001
    Posts
    126
    so, just curious, are there multiple ways to go about linking to and playing an external MP3? or are your 2 examples above an indication that its not a straight forward script and that it can get tricky?

  5. #5
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    Thanks for the fix MFIAT. I always manage to forget the event and to remove the eventlistener on my first draft of code. I always appreciate the second eye.

    I personally wouldn't use the enter frame object, but I was trying to make it as simple as possible.

    @ nulrick: the code that MFIAT put was the same as mine only he fixed a few errors that I had in mine (Sorry I was typing fast as I was getting ready for bed). There are better ways to do what I put up there, but they depend on the context of the code. I recommend picking up a book and reading tutorials to fully understand what you are doing.

    To help let me explain the changes that MFIAT made:
    var mySound:Sound; he defined the sound outside of the function so that you can access the sound later. IE if you followed my code above you wouldn;t be able to start or stop the sound again because you wouldn;t be able to access the sound variable from outside the function that defined it.

    function startSound($event:Event):void : addEventListener passes a argument which is the actual event itself. If you don't handle that argument the function will throw an error because it was expecting no arguments and it got one.

    removeEventListener(Event.ENTER_FRAME, startSound); This will fix some memory errors as that listener will continue to be listening. Also if you happen to enter that frame again the listener will p[ick up that event again and load the sound again.
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  6. #6
    Senior Member
    Join Date
    Sep 2001
    Posts
    126
    when using the scrip, I get 2 errors:
    The class or interface 'Event' could not be loaded.
    ')' or ',' expected

  7. #7
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    oops, i had a typo in this line:
    mySound = new Sound(new URLRequest("sound2.mp3");

    should be:

    mySound = new Sound(new URLRequest("sound2.mp3"));

    and at the beginning of the script, you should have:

    import flash.events.Event;

  8. #8
    Senior Member
    Join Date
    Sep 2001
    Posts
    126
    so here's the script I'm using:

    import flash.events.Event;
    var mySound:Sound;
    function startSound($event:Event):void
    {
    removeEventListener(Event.ENTER_FRAME, startSound);

    mySound = new Sound(new URLRequest("3.mp3"));
    }
    addEventListener(Event.ENTER_FRAME, startSound);



    it's in the first frame on the main level of my movie..
    other than 1 empty frame with this actionscript in it, there is nothing else in the movie, just so i can isolate and test this script.. so the MP3 (which is in the same folder, same level as the swf and html) should start to play automatically, correct? It's not playing. I'm publishing as FlashPlayer 9, Actionscript 3.

  9. #9
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    you need some more code to make it play.

    Code:
    function playSound(e:Event):void{
       mySound.play();
    }
    mySound.addEventListener(complete, playSound); //To call the playSound when the load of the sound is complete
    you could also listen for an open event or write some code to monitor the download progress of you mp3 file.
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  10. #10
    Senior Member
    Join Date
    Sep 2001
    Posts
    126
    Hi KC.. thnx for your help.. really really appreciated.. I added the code above but get an error when publishing, and the MP3 still doesn't play:

    import flash.events.Event;
    var mySound:Sound;
    function startSound($event:Event):void
    {
    removeEventListener(Event.ENTER_FRAME, startSound);

    mySound = new Sound(new URLRequest("3.mp3"));
    }
    addEventListener(Event.ENTER_FRAME, startSound);

    function playSound(e:Event):void{
    mySound.play();
    }
    mySound.addEventListener(complete, playSound);



    the error is:
    "1120: Access of undefined property complete."

  11. #11
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    I am typing you these answers too quickly I am sorry. It should be Event.COMPLETE

    Sorry
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  12. #12
    Senior Member
    Join Date
    Sep 2001
    Posts
    126
    where? Event is written out 3 times in script.. is the rest ok?

  13. #13
    Senior Member
    Join Date
    Sep 2001
    Posts
    126
    I did this:

    import flash.events.Event;
    var mySound:Sound;
    function startSound($event:Event):void
    {
    removeEventListener(Event.ENTER_FRAME, startSound);

    mySound = new Sound(new URLRequest("3.mp3"));
    }
    addEventListener(Event.ENTER_FRAME, startSound);

    function playSound(e:Event):void{
    mySound.play();
    }
    mySound.addEventListener(Event.COMPLETE, playSound);


    and published it.. in browser, got pop-up:

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

  14. #14
    Senior Member
    Join Date
    Sep 2005
    Location
    Detroit
    Posts
    193
    OK just quickly i stepped back and asked how I would create a sound in the main timeline. This is what I got:

    Code:
    import flash.events.Event;
    
    function createAndPlaySound():Sound
    {
    	function playSound(e:Event):void{
    		s.removeEventListener(Event.COMPLETE, playSound);
    		s.play();
    	}
    
    	var s:Sound = new Sound(new URLRequest("3.mp3"));
    
    	s.addEventListener(Event.COMPLETE, playSound);
    	return s;
    }
    var mySound:Sound = createAndPlaySound();
    While the music played you worked by candlelight
    Those san francisco nights
    You were the best in town
    Just by chance you crossed the diamond with the pearl
    You turned it on the world
    That’s when you turned the world around

  15. #15
    Senior Member
    Join Date
    Sep 2001
    Posts
    126
    yes!!!!! thanks you!!!!

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