Copy link to clipboard
Copied
Hey guys,
I'm new to AS3, still learning. I just read a little about arrays in the book I was learning from, and after looking at some online stuff, thought I'd try to do something simple with one.
What I'm trying to do is save a bunch of sounds (different dog barks) in an array and then when I press the button, it plays a random sound from that array. Here's what I have so far :
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
package
{
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class Sound2 extends MovieClip
{
var bark1:Bark1;
var bark2:Bark2;
var bark3:Bark3;
var bark4:Bark4;
var bark5:Bark5;
var bark6:Bark6;
var soundChannel:SoundChannel;
var barkArray:Array = [Bark1, Bark2, Bark3, Bark4, Bark5, Bark6];
public function Sound2()
{
barkButton.addEventListener(MouseEvent.CLICK, onBarkButtonClick);
bark1 = new Bark1();
bark2 = new Bark2();
bark3 = new Bark3();
bark4 = new Bark4();
bark5 = new Bark5();
bark6 = new Bark6();
soundChannel = new SoundChannel();
function onBarkButtonClick(event:MouseEvent):void
{
var barkNo = Math.round(Math.random() * 5);
soundChannel = (barkArray[barkNo]).play();
}
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
When I try to press the button, I get this error message :
TypeError: Error #1006: play is not a function.
at MethodInfo-22()
I used trace and saw the numbers from barkNo were fine, I think it's just something about the array. I've also spent a few hours trying to do it different ways, but still nothing. How can I play a random sound from an array? And am I storing the information correctly? I'm not sure what I'm missing, any help would be great.
Thanks
Copy link to clipboard
Copied
as3 is case sensitiv.
change all the upper cases of your array to lower cases:
var barkArray:Array = [bark1... bark6];
also it might be necessary to cast your Sounds
soundChannel = Sound(barkArray[barkNo]).play();
also:
1. In a class its usual a good idea to grant all your functions and variables a scope that are not created on the fly
private var bark1:Bark1 etc.
2.you shouldn`t put another function inside the constructor
put it after the constructors closing bracket and write
public function onBarkButtonClick(event:MouseEvent):void
{
var barkNo = Math.round(Math.random() * 5);
soundChannel = Sound(barkArray[barkNo]).play();
}
Copy link to clipboard
Copied
First, never use nested and anonymous functions - you position yourself for a lot of trouble.
Try this (I did not test it though):
package
{
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.events.MouseEvent;
import flash.display.MovieClip;
public class Sound2 extends MovieClip
{
var sound:Sound;
var soundChannel:SoundChannel = new SoundChannel();
var barkArray:Array = [Bark1, Bark2, Bark3, Bark4, Bark5, Bark6];
public function Sound2()
{
barkButton.addEventListener(MouseEvent.CLICK, onBarkButtonClick);
}
private function onBarkButtonClick(event:MouseEvent):void
{
soundChannel.stop();
sound = new barkArray[int(barkArray.length * Math.random())]();
soundChannel = sound.play();
}
}
}
Copy link to clipboard
Copied
Hey guys,
Thanks for the reply. I didn't mean to have the function nested haha, so I fixed that up. Also, with the sounds in the array, their AS Linkage is Bark1, Bark2 etc. with a capital, so that's what I used when putting them in the array. If I use the lower case, it says I'm accessing an undefined property, EVEN when I define them using variables.
Andrei1, I tried your way and I get the error message :
1067 : Implicit coercion of a value type void to an unrelated type flash.media:SoundChannel
I think I know what that means, I've had it before. It's where the variables type isn't correct. But I can't seem to figure out how it related to the Sound Channel.
Copy link to clipboard
Copied
When you only declare an object you do not yet create it, so it will be null.
var bark1:Bark1; // this object does not yet exist
bark1 = new Bark1(); // now it exists
Copy link to clipboard
Copied
You are pushing classes of Sounds in an array and try to use play() as if it were a static function.
Looke here for the basics how Sound-objects are used.
declare your array in the beginning like so: var barkArray:Array = new Array();
and after you have called the constructor of each class,
add the instances to the array:
....
bark6 = new Bark6();
barkArray.push(bark1);
...
Find more inspiration, events, and resources on the new Adobe Community
Explore Now