Flash AS3: How to create one function to play sounds from different buttons? - actionscript-3

I am making a soundboard and I'm using actionscript. I would like to have one function with the code to play a different sound depending on which button is pressed. I will also be playing from the library rather than a URL. Here is some real code mixed with pseudo for what I want to do:
var soundEffect:SoundEffect = new SoundEffect();
sound1_btn.addEventListener(MouseEvent:CLICK, buttonName, playSoundEffect); //possible?
function playSoundEffect(e:Event, buttonName):void {
soundEffect.attachSound = buttonName + ".mp3" //pseudo code
soundEffect.play();
}
The SoundEffect class is just the name I used in Linkage. I don't know the best way to change the sound that a class represents, or the best way to do this in general. Ideally I'd like to not create 50 different classes with 50 different sound variables and 50 functions. I'd rather each button had some sort of identifier and within the function I can use the button name or identifier to assign the appropriate sound.

If you are using a Button symbol, you could use a naming convention that encodes the class name in your button name.
So if your sound effect class name was sfx_jump , you would name your instance :
sfx_jump_btn
You then set your event listener like this :
sfx_jump_btn.addEventListener(MouseEvent.CLICK, clickHandler);
What you want to do in the clickHandler function is to first generate your className String based on the buttons name property. Then you get the Class Definition via using getDefinitionByName so that you can create an instance of the sound, the following code is how you do that :
public function clickHandler(e:MouseEvent):void
{
var button:SimpleButton = e.target as SimpleButton;
// use replace to clip off the _btn suffix
var className:String = button.name.replace("_btn","");
var SoundClass:Class = getDefinitionByName(className) as Class;
var newSound:Sound = new SoundClass();
newSound.play();
}
You also need to add this import :
import flash.utils.getDefinitionByName;

Yes you can do it. Since you have multiple buttons attach a property to each button like below I attached 'soundToPlay' property to sound1_btn.
sound1_btn.soundToPlay = "1.mp3";
sound1_btn.addEventListener( MouseEvent.CLICK, handleBtnClick);
function handleBtnClick( e:Event ):void{
soundEffect.attachSound = e.target.soundToPlay;
soundEffect.play();
}

#hrehman have the answer for you, but if you button class don't have the property soundToPlay you could use the name property as an ID. and get back with the currentTargetproperty of the event.
sound1_btn.name = "Sound1";
sound1_btn.addEventListener(MouseEvent:CLICK, playSoundEffect);
function playSoundEffect(e:Event):void {
soundEffect.attachSound = e.currentTarget.name + ".mp3";
soundEffect.play();
}

Related

ActionScript 3 - use [brackets] instead of getChildByName

I have a MovieClip inside library, linkaged to MyObject and it contains a textField.
I don't know how I can access this textField without using the getChildByName method.
Apparently, the 3rd section works when object is on stage (without using addChild). But when using addChild I think there has to be some kind of casting; which I don't know how.
var childElement: MyObject = new MyObject();
childElement.name = "theChildElement";
container.addChild(childElement);
btn.addEventListener(MouseEvent.CLICK, changeText);
function changeText(event: MouseEvent): void
{
var targetBox:MovieClip = container.getChildByName(childElement.name) as MovieClip;
targetBox.textField.text = "hello"; // THIS WORKS
// This works too:
// MovieClip(container.getChildByName("theChildElement"))["textField"].text = "hello"; // THIS WORKS TOO.
// THIS DOESN'T WORK. why?
// container["theChildElement"]["textField"].text = "hello";
}
As confusing as it may seem, instance name, and name are not the same. From your code you should always be able to get to your MC by it's variable name. To get your last like to work you could just use this.
childElement["textField"].text = "hello";
There is a difference between Symbols created by the Flash IDE, which aggregate other DisplayObjects and programmatically created DisplayObjects.
When a DisplayObject is created in the Flash IDE, it's instance name can be used to resolve the instance as a property - which means it can be accessed via []. The [] can be used to access properties or keys of dynamic declared classes - like MovieClip. This necessary because you'll most likely down cast to MovieClip instead of using the symbol class created by Flash. That is not possible when simply using addChild, addChildAt or setChildAt from the DisplayObjectContainer API.
It is always the save way to access it via getChildByNameand check for null because otherwise your app, website or whatever is doomed for 1009 errors as soon as someone is changing the symbols.
I'd create a bunch of helper methods, like
// not tested
function getChildIn(parent:DisplayObjectContainer, names:Array):DisplayObject {
var child:DisplayObject, name:String;
while (names.length > 0) {
name = names.shift();
child = parent.getChildByName(name);
if (!child) {
// log it
return null;
}
if (names.length == 0) {
return child;
}
}
// log it
return null;
}
function getTextFieldIn(parent:DisplayObjectContainer, names:Array):TextField {
return getChildIn(parent, names) as TextField;
}
function getMovieClipIn(parent:DisplayObjectContainer, names:Array):MovieClip {
return getChildIn(parent, names) as MovieClip;
}
Your third method doesn't work because you are trying to call the ChildElement by it's name
without using getChildByName method. On the other hand, you shouldn't call your textField textField, because that's already an actionScript property.
Your should rather call it 'displayText' for example.
For a textField called 'displayText' contained in childElement :
function changeText(event:MouseEvent): void
{
childElement.displayText.text = "hello";
}

ActionScript 3 - How to target textbox inside movieclip

I have a movie clip called radio and flashlight. Inside the movieclip, there is a text box with words inside. The instance name of the textbox inside radio is called 'radioText' and the instance name of the textbox inside flashlight is called 'flashlightText'. I want the color of the text to turn white when hovering over the movieclip.
The code below works for changing radioText but not flashlightText:
var containers = [radio, flashlight];
for (var i:int = 0; i<containers.length; i++) {
containers[i].addEventListener(MouseEvent.MOUSE_OVER, hOver);
}
var whiteFont:TextFormat = new TextFormat();
whiteFont.color = 0xFFFFFF;
function hOver(evt:Event):void {
evt.currentTarget.radioText.setTextFormat(whiteFont); //change radioText's color
}
what I want to do is instead of
evt.currentTarget.radioText.setTextFormat(whiteFont);
, I want to do somthing like
evt.currentTarget.(currentTarget.name + 'Text').setTextFormat(whiteFont);
but that doesn't work for obvious reasons. Is there a way to do what I want to do?
There are quite a few good ways to implement what you are asking, what you do really depends on how you are setting up your objects.
For example, your pseudo code:
evt.currentTarget.(currentTarget.name + 'Text').setTextFormat(whiteFont);
...could be implemented if you named all your child text fields identically OR if you named them accordingly to some convention (for this example I set a name for the parent and a similar name for the child). So let's assume your container has the name "container" and has a TextField child named "containerText". It would look something like this:
private function hOver(evt:MouseEvent):void {
var n:String = evt.currentTarget.name;
evt.currentTarget.getChildByName( n + "Text" ).setTextFormat( whiteFont );
}
This is now dependent on following a naming convention, which may or may not be ideal. So what might be an even better and more generic way to handle this? You can setup a class for your container object and have a TextField property there. For Example:
class myContainer extends Sprite {
public var textField:TextField;
public function myContainer():void {
textField = new TextField();
addChild(textField);
}
}
Pretty basic, but now if you added all the listeners to types of myContainer your event function could look like this:
private function hOver(evt:MouseEvent):void {
evt.currentTarget[ "textField" ].setTextFormat( whiteFont );
}
Which is probably more what you are looking for. This version allows you to grab the "textField" property of the object you applied the listener to, in this case that property would essentially be your radioText/flashlightText.
Ok so lastly, and the least favorable way to do this, assumes all you care about is that the object has a child of type TextField, it has no logical naming convention, and we are not sure what index it is at in the display list. You would have to do it something like this:
private function hOver(e:MouseEvent):void {
var i:int;
var displayObj:DisplayObjectContainer = e.currentTarget as DisplayObjectContainer;
for ( ; i < displayObj.numChildren; i++ ) {
var obj:* = displayObj.getChildAt( i );
if ( obj is TextField ) {
obj.setTextFormat( whiteFont );
}
}
}
That is also the slowest way of reaching your child object. This should cover all the bases, good luck!
What's wrong with?
DisplayObjectContainer(evt.currentTarget)[evt.currentTarget.name + 'Text'].setTextFormat(whiteFont);

AS3 dynamic variable creation in DC

Can variables be created dynamically without declaration when we write as Document class in AS3?
For example, from a library I'm importing sound files. Some 20 sound files.
If the code is in fla itself, we can assign in for loop like:
this["SOUND"+increasingNumber]
But in documentClass this is not working , since this refers the class here not the stage.
Any method to create variables?
When imported into your library, right click the sound file and go to its properties. Click the actionscript tab and check 'export for actionscript'. Give it a class name which you can then use in your document class to instantiate that sound.
If you named it Sound1:
var sound:Sound = new Sound1();
sound.play();
more detailed info here
[Edit to loxxy's reply] above shows how to create the variables in the document class.
To dynamically create all the sound variables, I'd recommend using an array, like so:
Suppose you named all your sounds in your library Sound1 to Sound20
import flash.utils.getDefinitionByName;
var sounds:Array = [];
var soundClass:Class;
for(var i:int = 1; i<21; i++){
soundClass = getDefinitionByName("Sound" + i) as Class;
sounds.push(new soundClass());
}
In fla when you add code, you add it into a framescript.
A framescript is a block of code repeated at a regular interval (framerate).
You can achieve that using addFrameScript like this.
However a better approach would be to not mix up framescript & the regular class methods.
You can access the 'stage' from the code but only after the added_to_stage event to be sure.
addEventListener(Event.ADDED_TO_STAGE, init);
function init(e:Event):void{
// Access 'stage' here
}

How can I give flash stage instances unique properties in Flash Professional to pass to AS3 script?

I've started building a rough game engine framework in Flash Professional and I'm curious how I can create objects in the Flash library that I'm able to drag onto the stage and assign properties that are accessible from AS3.
Example:
I want to create a switch object (e.g. a light switch), so that when the player interactes with it, it triggers something specific in code such as a light in the room turns on.
I understand that Flash has built in UI components that you can define properties within the Flash Professional environment (see image below), and I'm wondering if there's a way to create my own custom style components so that I can essentially have my level file open in flash (.fla) and then drag a switch component from my library, and type in some information such as what light it is controlling, and any other information I want.
(above is an example of the type of parameter control I'm looking for)
I've read a bit about extending the flash UIComponent class but I feel that that's not the right approach because it's overkill for what I want. All I want is to pass some basic parameters from a library stage instance into AS3. I do not want to pass data via the instance name because this seems very messy if I want to have more complex interaction.
Thanks!
I would create a "switch" movie clip and export it to actionscrip, same with a "light" movie clip. The in the main class .as file I would inset them into the stage, using addChild (clips) and then add a click listener to the "switch" movie clip to control the "light".
This can be easily done.
Component(s) are wrong approach in my opinion.
Firstly you would want to setup Actionscript linkage / label your Library item.
In Library Panel.
- Right Click on "yourMC" >> click "Properties".
- In Properties dialog Tick "Export for Action Script"
- Then Name your Class eg "yourMC_Class"
now MC is ready to be referenced in your code.
next you would want to Dynamically add your "yourMC" from library to stage.
which can be done like such.
// first reference library item
var yourMC_ref:yourMC_Class = new yourMC_Class();
// Then load dynamic mc item into var
var your_MC_OBJ = yourMC_ref;
// then add your MC to stage.
this.addChild(your_MC_OBJ);
your_MC_OBJ.x = 200;
your_MC_OBJ.y = 100;
in a nutshell that's how I add library items to stage.
Obviously thats the basic function / code.
In a project I would have all code in an external class, in which case you would just set vars as public vars
public var yourMC_ref:yourMC_Class = new yourMC_Class();
public var your_MC_OBJ = yourMC_ref;
and the last 3 lines of code into a public function
public function ADD_First_MC()
{
this.addChild(your_MC_OBJ);
your_MC_OBJ.x = 200;
your_MC_OBJ.y = 100;
}
Now 'your_MC_OBJ' can be used in more complex ways.
eg. to create a light switch there are many options depending on how you need to approch functionality.
eg. Apply a different MC library item to "your_MC_OBJ"
play specific frame within MCs.
However If it was me I would just use mouse function to switch light on or off using addChild removeChild.
eg.
public var LightON = 0;
public var yourMC_ref:yourMC_Class = new yourMC_Class();
public var your_MC_OBJ = yourMC_ref;
then create a public function that handles on / off events
public function LightON_OFF()
{
if(LightON == 1)
{
this.addChild(your_MC_OBJ);
your_MC_OBJ.x = 200;
your_MC_OBJ.y = 100;
}
if(LightON == 0)
{
this.removeChild(your_MC_OBJ);
}
}
Hope this helps.
So, for what you want, while it may not be the best way to do what you want, I understand it's your experience you are constructing.
Use components, yes...in the following way (the most simple one):
Create a Movie Clip
Right-click it in library
Click on "Component Definitions"
Add a property, set a name, a variable name (var test, for this matter) and a default value
Click OK
Open your movie clip
Open code for the first frame and declare the variable without an initial value (var test:String;)
Trace it's value ( trace( test ); )
Go back to the stage root
Drag and drop the item from library to stage
Test it (Cmd/Ctrl + Enter) (maybe it will print null, dunno why, it ignores the default value sometimes)
Select your component on stage
Open the properties panel (Windows > Properties)
Go to Component Parameters on this panel and change the property value
You should see the value traced on console
And, I think, like this you can use properties from components for what you want, like using a String and getting the controlled mc by its name.
Good luck
I think what people are trying to say is that you can have the whole thing is data driven, and so you can combine the IDE with the data to come up with your final game.
But consider this ... it might be what you want.
If you have, for instance, a BaseSwitch Class:
public Class BaseSwitch extends MovieClip {
private var _lightName:String;
private var _light:Light;
public function get lightName():String {
return lightName;
}
public function set lightName(value:String):void {
if (value != _lightName) {
_lightnName = value;
//Note I don't advocate having children reach into their parents like this,
//but you sound like you don't want the parent involved in the process, so
//this is one way you could do it.
if (parent.hasOwnProperty(lightName) && parent[lightName] is Light) {
_light = parent[lightName];
} else {
trace('Could not find light', _lightName);
}
}
}
//other code to listen for gestures and operate the light
}
Now, when you want a switch to operate a specific light name, create a library instance and set its base class to BaseSwitch. When you close the dialog where you set the base Class, you'll notice that it gives you a dialogue that it couldn't find the Class in the Class path and one will be generated. You're going to replace it with a Class that sets the lightName. Create a new AS3 Class in the root directory with the same name as your library instance. It should look something like this:
public class SpecificSwitch {
public function SpecificSwitch() {
super();
lightName = 'theSwitch';
}
}
Other possible choices involve having the parent Class match up instances of switch with instances of light based on name, so if it finds a light1 and a light1Switch, it either gives a reference to the light to the switch or it simply sets up a mapping in its own event listening system.

AS3 - How to assign names or tags to loader?

I have a list of images that i’ve loaded with the Loader class, but I’m having a tough time assigning them unique names.
I need unique names because I want to remove certain images after a while.
Is there a way to assign loaders a name or some unique tag so i can remove them later? thanks.
Here's part of my code
for (var i = startnum; i < endnum; i++){
var thumb = panels[i].#THUMB;
var thumb_loader = new Loader();
thumb_loader.load(new URLRequest(thumb));
thumb_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, thumbLoaded);
thumb_loader.name = i;
thumb_loader.x = (thumb_width + 20)*i;
}
I tried to use getChildByName in another function..
var myLoader:Loader = getChildByName( "1" ) as Loader;
myLoader.unload();
But it's giving me
Error #1009: Cannot access a property or method of a null object reference.
I tried to put thumb_loader as global variable, and do this
var myLoader:Loader = thumb_loader.getChildByName( "1" ) as Loader;
But it's still not working.
Thanks.
All display objects in ActionScript 3 have a name property. Whenever you create a Loader object you can assign a name to it like so:
var myLoader:Loader = new Loader();
myLoader.name = "myUniqueName";
myLoader.load( .... );
addChild( myLoader );
If you'd like to refer to the loader by the name you gave it, use the getChildByName() method.
var myLoader:Loader = getChildByName( "myUniqueName" ) as Loader;
myLoader.unload();
Please be mindful that getChildByName() will only work after you've added the Loader(s) to the display list using addChild(). Otherwise, you'll have to create something to store the references to the Loader objects in, such as an Array and refer to the loaders via that Array. For example, outside your loop you could create an Array named loadersArr. In your loop you would do:
loadersArr["uniqueName"] = thumb_loader;
Then you can refer to your loaders with your unique name through the loadersArr Array.
var loaderToUnload:Loader = loadersArr["uniqueName"];
loaderToUnload.unload();
Without seeing more of your code, its difficult to understand the scope in which this code resides and where any other code that may try to reference these Loaders resides.
Not sure I 100% understand your problem, but why not put them in an object map rather than a list and generate unique names for them if you don't have them...
var img:Image;
var img_map:Object = new Object();
var last_added:int = 0;
for each (img in yourListOfImages)
{
img_map["img_"+last_added] = img;
last_added++;
}
Depending on your environment (Flex or Flash) you can use a UID generator instead of my simplistic unique names above.
package
{
import flash.display.Loader;
public dynamic class DynamicLoader extends Loader
{
public function DynamicLoader()
{
super();
}
}
}
I believe the Loader class is a sealed class so you would want to create this class and use it instead of the normal Loader class to give it any attribute you want. I also believe that without using this DynamicLoader instead of the normal Loader, the Loader class does have the name property available to it.