A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Play random sounds from an array. Error:1007

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    15

    Play random sounds from an array. Error:1007

    Hello!

    So I've been messing around with some code to see if I can get a button to play a random sound from an array by using a function. However, I keep getting the error

    TypeError: Error #1007: Instantiation attempted on a non-constructor.
    at Untitled_fla::MainTimeline/ClickHandler()


    Here's my code:

    Code:
    //-----------------------------------------------------------------
    import flash.media.Sound;
    import flash.media.SoundChannel;
    
    var sndc1:click1;
    var sndc1channel:SoundChannel;
    var sndc2:click2;
    var sndc2channel:SoundChannel;
    var sndc3:click3;
    var sndc3channel:SoundChannel;
    var sndc4:click4;
    var sndc4channel:SoundChannel;
    var sndc5:click5;
    var sndc5channel:SoundChannel;
    
    var soundbase: Array = [sndc1,sndc2,sndc3,sndc4,sndc5];
    
    
    
    
    myBtn.addEventListener(MouseEvent.CLICK, ClickHandler);
    function ClickHandler(event:MouseEvent):void 
    
    {
    	
    	var rand:Number = Math.floor(Math.random()*(soundbase.length));
    	trace ("Random Number: "+rand);
    	var ChosenSound = soundbase[rand]
    	trace ("Chosen sound: " +ChosenSound);
    	
    	var playing:Sound = new ChosenSound()
    	
    	playing.play();
    
    }
    //-----------------------------------------------------------------
    The traces in there work.
    Random number is of course a random number no higher than the array length.
    However, Chosen sound is always "null". Should I worry here?

    Anything I'm doing wrong? Not really sure how to understand these error messages yet.

    Thanks in advance.

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Yes, ChosenSound being null will of course prevent it from working. You cannot create a new null.

    What you need to do is put your classes in the array instead of the uninitialized instances of those classes. In fact, you don't need the sndc variables at all.

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    15
    Thanks for the tip. Trouble is, I'm still a bit of a rookie with actionscript.

    When you say "put my classes in the array", which part of the code is the class exactly?

    Also, is there a resource you know of that could help explain classes?

    Thanks again.

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Are you familiar with class-based object oriented programming at all? A Class is somewhat like the type of a thing. It is not the thing itself. An instance of a class is that thing, and its type is the class. That might have been confusing. Metaphor time.

    A class is a recipe. An instance is the food you make from that recipe. You can't eat a recipe, you have to eat the actual food. But you can still do stuff with your recipes, like pass them around, put them in various recipe books, and check whether some food was made with that recipe.

    So you need to put your classes(recipes) in the array so that later you can pull a class(recipe) out of the array and create a new sound(food) from it.

    In your case, your classes to put in the array are click1 through click5. If you were going to give the ChosenSound variable a type (and you should ALWAYS type your variables) it would be of type Class.
    Code:
    //-----------------------------------------------------------------
    import flash.media.Sound;
    import flash.media.SoundChannel;
    
    var soundbase:Array = [click1, click2, click3, click4, click5];
    
    myBtn.addEventListener(MouseEvent.CLICK, ClickHandler);
    function ClickHandler(event:MouseEvent):void {
    	var rand:Number = Math.floor(Math.random()*(soundbase.length));
    	trace ("Random Number: "+rand);
    	var ChosenSound:Class = soundbase[rand]
    	trace ("Chosen sound: " +ChosenSound);
    	
    	var playing:Sound = Sound(new ChosenSound());
    	
    	playing.play();
    
    }
    //-----------------------------------------------------------------

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    15
    Brilliant! Thanks for that, code works perfectly now. I owe you an e-beer.

    I did computing as a subject in college years ago but since then, a lot of my (Visual Basic 6) programming know-how has leaked out (classes, inheritance, instantiation etc). Another reason I've been trying to learn Flash and AS is to rebuild on that knowledge.

    I do remember that classes were useful for great flexibility and this is something I'm looking to build on.

    Cheers!

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