New array for music symbols - actionscript-3

I have background musics from the library, any ideas how to solve this code by clicking the button to change it?
var BeachBall: CrashBash_BeachBall = new CrashBash_BeachBall();
var BrickDive: LEGOIsland2_BrickDive = new LEGOIsland2_BrickDive();
var BloocheepOcean: MarioHoops_BloocheepOcean = new MarioHoops_BloocheepOcean();
var Underwater: MarioTeachesTyping2_Underwater = new MarioTeachesTyping2_Underwater();
var UnderPressure: CrashBandicootNST_UnderPressure = new CrashBandicootNST_UnderPressure();
var CalmWaters: DireDireDocks_CalmWaters = new DireDireDocks_CalmWaters();
var Level1: TreasureCove_Level1 = new TreasureCove_Level1();
var Level2: TreasureCove_Level2 = new TreasureCove_Level2();
var Level3: TreasureCove_Level3 = new TreasureCove_Level3();
var music: Array = new Array(CalmWaters, BloocheepOcean, UnderPressure, Underwater, BeachBall, BrickDive, Level1, Level2, Level3);
// Changing the Background Music
music_btn.addEventListener(MouseEvent.CLICK, on_pressMusic);
music[Math.floor(Math.random()*music.length)].play();
if(!sound)
{
sound_channel = CalmWaters.play(0, int.MAX_VALUE);
sound_channel.soundTransform = new SoundTransform(0.4);
}
function on_pressMusic(event: MouseEvent): void
{
/*
var random: int = int(Math.random() * sounds[0].length);
play_music(sounds[0][random]);
var sMusic:Button = new Button();
sMusic.play();
*/
if (is_Playing == true) //# if music is already playing
{
music_player.stop();
}
musicNum = Math.floor(Math.random() * (musicList.length-1));
music_object = new (musicList[musicNum]);
audioTime = 0; //# seconds to start from within track
music_player = music_object.play(audioTime);
is_Playing = true;
}
Error:
TypeError: Error #1007: Instantiation attempted on a non-constructor.
at BlowfishPong_fla::MainTimeline/on_pressMusic()[BlowfishPong_fla.MainTimeline::frame27:205]
All of the codes are updated. any ideas how to solve and fix this for their future coding.

Try something like this:
var BeachBall: CrashBash_BeachBall = new CrashBash_BeachBall();
var BrickDive: LEGOIsland2_BrickDive = new LEGOIsland2_BrickDive();
var BloocheepOcean: MarioHoops_BloocheepOcean = new MarioHoops_BloocheepOcean();
var CalmWaters: DireDireDocks_CalmWaters = new DireDireDocks_CalmWaters();
var Level1: TreasureCove_Level1 = new TreasureCove_Level1();
var Level2: TreasureCove_Level2 = new TreasureCove_Level2();
var Level3: TreasureCove_Level3 = new TreasureCove_Level3();
var Underwater: MarioTeachesTyping2_Underwater = new MarioTeachesTyping2_Underwater();
var UnderPressure: CrashBandicootNST_UnderPressure = new CrashBandicootNST_UnderPressure();
var myNum :uint = 0;
var audioTime :uint = 0;
var music_object :Sound;
var music_player :SoundChannel = new SoundChannel();
var is_Playing :Boolean = false;
var music_list :Array = new Array();
music_list = [ BeachBall, BrickDive, BloocheepOcean, CalmWaters,
Level1, Level2, Level3, Underwater, UnderPressure
];
//# Changing the Background Music
music_btn.addEventListener(MouseEvent.CLICK, on_pressMusic);
function on_pressMusic (event:MouseEvent) : void
{
if ( is_Playing == true ) //# if music is already playing
{ music_player.stop(); }
myNum = Math.floor( Math.random() * (music_list.length-1) );
music_object = new ( music_list[ myNum ] );
audioTime = 0; //# seconds to start from within track
music_player = music_object.play( audioTime );
is_Playing = true;
}

Related

Volume control after loop as3

I have a problem with my code. The code works great with one exception, if I have pressed the mute-button and the animation loops, it's always back to volume = 1. How can it loop without always going back to volume = 1?
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
var volumeAdjust:SoundTransform = new SoundTransform();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
volumeAdjust.volume = 1;
mySound.load(songURL);
channel1.soundTransform = volumeAdjust;
channel1 = mySound.play();
function muteLyd(e:MouseEvent):void {
if(volumeAdjust.volume == 1){
volumeAdjust.volume = 0;
}
else {
volumeAdjust.volume = 1;
}
channel1.soundTransform = volumeAdjust;
}
Just found out this code solves the problem:
var mySound:Sound = new Sound();
var songURL:URLRequest = new URLRequest("Lyd/jetpass.mp3");
var channel1:SoundChannel = new SoundChannel();
mute1.addEventListener(MouseEvent.CLICK, muteLyd);
unmute1.addEventListener(MouseEvent.CLICK, unMuteLyd);
mySound.load(songURL);
channel1 = mySound.play();
function muteLyd(evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(0);
unmute1.visible = true;
mute1.visible = false;
}
function unMuteLyd (evt:MouseEvent){
SoundMixer.soundTransform = new SoundTransform(1);
unmute1.visible = false;
mute1.visible = true;
}

Dynamically adding a 3 tier movieclip to stage then populate it

What I am trying to do is add say 20 jpgs labelled 1-20 individually inside movieClips [ sReel ] , then add the movieClips to movieClip [ aReel ] and duplicate movieClip [ aReel ] into movieClip [ mReel ] so I can have a horizontal scrolling movieClip.
I can do all manually no problem but doing it automatically is not so easy :(
Just need how to stack the mocieClips correctly as know how to do rest oc code Thanks..
var movie_Number:Array = new Array();
var movie_Title: Array = new Array();
var movie_Director: Array = new Array();
var movie_Star: Array = new Array();
var movie_Duration: Array = new Array();
var movie_Genre: Array = new Array();
var movie_Year: Array = new Array();
var movie_Rating: Array = new Array();
var movie_Comment: Array = new Array();
var mReel:MovieClip = new MovieClip;
var aReel:MovieClip = new MovieClip;
var bReel:MovieClip = new MovieClip;
var sReel:MovieClip = new MovieClip;
var ldr:Loader;
newReelMask.cacheAsBitmap = true;
mReel.cacheAsBitmap = true;
mReel.mask = newReelMask;
var xmlLoader:URLLoader = new URLLoader(), xmlData:XML;
var movieNum:Number;
var xReel = 0;
mReel.addEventListener(MouseEvent.MOUSE_OVER, onStop);
mReel.addEventListener(MouseEvent.MOUSE_OUT, onStart);
var myTimer:Timer = new Timer(1);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();
init();
makeSmallReel();
aReel.x = 0;
aReel.y = 0;
mReel.addChild(aReel);
addChild(mReel);
function init():void
{
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("movieInfo.xml"));
}
function LoadXML(e:Event):void
{
xmlData = new XML(e.target.data);
var movieTitle:XMLList = xmlData..title;
var movieDirector:XMLList = xmlData..director;
var movieStar:XMLList = xmlData..star;
var movieDuration:XMLList = xmlData..duration;
var movieGenre:XMLList = xmlData..genre;
var movieYear:XMLList = xmlData..year;
var movieRating:XMLList = xmlData..rating;
var movieComment:XMLList = xmlData..comment;
var len: int = xmlData.movie.length();
var i:int = 0;
for (i; i < len; i++)
{
movie_Number.push(i);
movie_Title.push(movieTitle[i]);
movie_Director.push(movieDirector[i]);
movie_Star.push(movieStar[i]);
movie_Duration.push(movieDuration[i]);
movie_Genre.push(movieGenre[i]);
movie_Year.push(movieYear[i]);
movie_Rating.push(movieRating[i]);
movie_Comment.push(movieComment[i]);
}
}
function makeSmallReel():void
{
var i:int = 1;
for (i; i < 11; i++)
{
ldr = new Loader();
ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, ldr_complete);
ldr.load(new URLRequest("images/thumb/"+i+".jpg"));
}
}
function ldr_complete(e:Event):void
{
sReel.x = xReel;
sReel.addChild(ldr);
xReel = xReel + e.target.width;
aReel.addChild(sReel);
}
function timerListener(e:TimerEvent):void
{
if (mReel.x <= -(aReel.width))
{
mReel.x = 0;
}
mReel.x -= 1;
}
function onStop(e:MouseEvent):void
{
myTimer.stop();
}
function onStart(e:MouseEvent):void
{
myTimer.start();
}
You 're adding always the same loader(ldr is a class-var) to your container.
At loading you have to create a ldr vor each picture. And add this to your container in your complete-handler. (it's e.target in your complete-handler)
BTW: You don't need a MovieCLip. A Sprite is enought, 'cause semms, you never need the timeline of a movieclip, which is the difference between MovieClip and Sprite ( and the overhead).

AS3 Adobe AIR: Return xml string

I'm working in Flash CS6 with Adobe AIR 3.3 (for iOS) and having trouble returning an XML string to a textField.
It is tracing the proper information, and I've tried a few ways to return the trace but can't seem to quite get it... Here is my most recent try. Any suggestions? Thanks in advance.
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("http://www.someURL.php"));
//php file that echos xml
myLoader.addEventListener(Event.COMPLETE, init);
var fadedText:TextField;
var touchList:TouchList;
var textOutput:TextField;
var animateLeft:Tween;
var listArray:Array;
var item:TouchListItemRenderer;
var theXML:XML;
var days:Array = new Array("monday", "tuesday", "wednesday", "thursday", "friday", "saturday", "sunday");
var daySelected;
var businessNameArray:Array = new Array();
var businessLogoArray:Array = new Array();
var businessAddress:Array = new Array();
var distanceArrayDisplay:Array = new Array();
var distanceArrayCount = 0;
function init(e:Event = null):void
{
trace(myLoader.data);
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
myLoader.close();
// add our list and listener
var itemSizeCalculator = stage.stageHeight / 6;
touchList = new TouchList(stage.stageWidth, stage.stageHeight-itemSizeCalculator);
touchList.addEventListener(ListItemEvent.ITEM_SELECTED, handlelistItemSelected);
addChild(touchList);
touchList.x = stage.stageWidth;
// Fill our list with item rendreres that extend ITouchListRenderer.
for(var i:int = 0; i < theXML.food.length(); i++) {
if(theXML.food[i].monday != "un")
{
item = new TouchListItemRenderer();
item.index = i;
item.data = theXML.food[i].business;
item.name = theXML.food[i].business;
item.addEventListener(MouseEvent.CLICK, itemWasClicked);
item.itemHeight = itemSizeCalculator;
businessNameArray[i]= theXML.food[i].business;
businessLogoArray[i]=("http://www.logosURL.com/"+theXML.food[i].logo);
businessAddress[i]= theXML.food[i].address;
var fadedTextFormat:TextFormat = new TextFormat();
fadedTextFormat.bold = true;
fadedTextFormat.color = 0x999999;
fadedTextFormat.size = 14;
fadedTextFormat.font = "Helvetica";
fadedText = new TextField();
fadedText.height = 30;
fadedText.mouseEnabled = false;
fadedText.defaultTextFormat = fadedTextFormat;
item.addChild(fadedText);
fadedText.x = 75;
fadedText.y = 10;
distanceArrayDisplay.push(fadedText);
var distanceLoader:URLLoader = new URLLoader();
distanceLoader.load(new URLRequest("http://maps.googleapis.com&origins=someAddress&destinations="+businessAddress[i]+"&mode=walking&language=en-en&sensor=false"));
distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);
var logoLoader:Loader = new Loader();
item.addChild(logoLoader);
var logoURL:URLRequest = new URLRequest("http://www.myLogos.com/"+theXML.food[i].logo);
logoLoader.load(logoURL);
logoLoader.scaleX = .4;
logoLoader.scaleY = .4;
logoLoader.y = logoLoader.y + 5;
logoLoader.mouseEnabled = false;
var arrowGraphic:rightArrow = new rightArrow();
item.addChild(arrowGraphic);
arrowGraphic.x = stage.stageWidth - 5;
arrowGraphic.y = item.height/2;
touchList.addListItem(item);
}
}
}
function distanceCalculated(e:Event)
{
// trace(e.currentTarget.data);
var distanceXML:XML = new XML(e.target.data);
distanceXML.ignoreWhitespace = true;
var returnVar:String = (distanceXML.row.element.distance.text);
distanceArrayDisplay[distanceArrayCount].text = returnVar;
trace(returnVar);
distanceArrayCount++;
}
I am guessing that you are correctly reading the first XML, and that XML has a list of URLs that you want to load and then display some info from those on TextFields. Without knowing the structure of that XML I can't suggest you any working code, but I can point you on the right direction. For more info on reading/iterating XML on flash as3, please read: http://www.kirupa.com/developer/flashcs3/using_xml_as3_pg1.htm
//iterator var
var xml_read:uint=0;
//array of textfields for reference
var array_textFields:Array;
//config XML complete
function init(e:Event = null):void
{
array_textFields = new Array();
theXML = new XML(e.target.data);
theXML.ignoreWhitespace = true;
//this depends on the XML structure, please look at the article I linked
//for more info on how to iterate an XML
var i:uint=0;
for(someUrl in theXML..url)
{
var fadedText:TextField = new TextField();
//you should place each Textfield on different coord, otherwise
//they will all stack on top of each other and you will only see one
//for example:
fadedText.y = (fadedText.height+10)*i;
item.addChild(fadedText);
array_textFields.push(fadedText);
var distanceLoader:URLLoader = new URLLoader();
distanceLoader.load(new URLRequest(someUrl));
distanceLoader.addEventListener(Event.COMPLETE, distanceCalculated);
i++;
}
}
function distanceCalculated(e:Event):void
{
var distanceXML:XML = new XML(e.target.data);
distanceXML.ignoreWhitespace = true;
//retrieve information
var returnVar:String = (distanceXML.row.element.distance.text);
//set to textfield
TextField(array_textFields[xml_read]) = returnVar;
//increase iterator
xml_read++;
}
Please bear in mind that in ActionScript3, all network I/O is asynchronous. Usually EventListener functions don't return any value because you don't know when the data is ready. What you do is store a reference to where you want the data to go (in your case, a TextField variable) when the EventListener function is called asynchronously.

Actionscript 3: Soundspectrum animation playing but sound muted. Is this possible?

Kinda of an oxy-moron question but I thought I would go ahead and ask anyhow.... I am looking for my soundspectrum animation to play but my sound will be muted...This will be inserted in a banner ad, so their are specific rsestrictrictions with vendors where the sound has to be user initiated...
Any ideas would be appreciated...
var SpectrumLineWidth:Number;
var SpectrumLineColor:Number;
var SpectrumGlowDirection:String;
var SGD:String;
var ss:ByteArray = new ByteArray();
var bmpData:BitmapData;
var bmp:Bitmap;
var lsp:Sprite = new Sprite();
var blur:BlurFilter;
// Main sounds object
var _sound:Sound = new Sound();
var _soundChannel:SoundChannel;
var _soundTransform:SoundTransform=new SoundTransform(1);
var _soundLoaderContext:SoundLoaderContext = new SoundLoaderContext();
bmpData = new BitmapData(10,10,true,0xcccccc);
bmp = new Bitmap(bmpData);
spectrumArea.specArea.addChild(lsp);
spectrumArea.specArea.addChild(bmp);
// filter;
blur = new BlurFilter(8,8,4);
// save configuration
SpectrumLineWidth = 1;
SpectrumLineColor = 0x0066FF;
var s:Sound = new Sound();
//s.setVolume(0);
var sc:SoundChannel;
var ba:ByteArray = new ByteArray();
var array:Array;
s.load(new URLRequest("test.mp3"));
sc = s.play(0,1000);
sc.soundTransform = _soundTransform;
this.addEventListener(Event.ENTER_FRAME, spectrum);
function spectrum(event:Event){
lsp.graphics.clear();
lsp.graphics.lineStyle(SpectrumLineWidth, SpectrumLineColor);
lsp.graphics.moveTo(-1, 50);
SoundMixer.computeSpectrum(ss);
for (var i:uint = 0; i<350; i++)
{
var num:Number = - ss.readFloat() * 50 + 50;
lsp.graphics.lineTo(i, num);
}
bmpData.draw(lsp);
bmpData.applyFilter(bmpData, bmpData.rect,new Point(), blur);
//SGD = SpectrumGlowDirection;
//lsp.graphics.clear();
//lsp.alpha=.5
bmpData.draw(lsp);
}
//var sd:SoundTransform = new SoundTransform();
//sd.volume=0;
//SoundMixer.soundTransform = new SoundTransform(1);
You cant do it with SoundMixer.computeSpectrum(ss);
Look into Sound.extract

Actionscript 3 - DataGridColumn and having a specific column automatically sorted

I am using the DataGrid component in Flash, that is loaded with data via a external XML file. I have a column, A (Serial), which once loaded, I'd like for the information to be sorted Ascendingly, automatically. Does anyone have any idea on how to do this?
Here is my code:
import fl.controls.DataGrid;
import fl.data.DataProvider;
import fl.controls.dataGridClasses.DataGridColumn;
import fl.controls.ScrollPolicy;
import fl.events.DataGridEvent;
var dp:DataProvider;
var A:DataGridColumn = new DataGridColumn("Serial");
A.headerText = "Serial No.";
A.width = 100;
A.resizable = false;
var B:DataGridColumn = new DataGridColumn("Mold");
B.headerText = "Mold No.";
B.width = 150;
B.resizable = false;
var C:DataGridColumn = new DataGridColumn("Type");
C.headerText = "Grid Type: ";
C.width = 350;
C.resizable = false;
var myDataGrid:DataGrid = new DataGrid();
myDataGrid.addColumn(A);
myDataGrid.addColumn(B);
myDataGrid.addColumn(C);
myDataGrid.verticalScrollPolicy = ScrollPolicy.ON;
myDataGrid.setSize(600, 800);
myDataGrid.move(0, 0);
myDataGrid.addEventListener(DataGridEvent.HEADER_RELEASE, headerReleaseHandler);
addChild(myDataGrid);
var url:String = "xml/TEST.xml";
var req:URLRequest = new URLRequest(url);
var uLdr:URLLoader = new URLLoader();
uLdr.addEventListener(Event.COMPLETE, completeHandler);
uLdr.load(req);
function completeHandler(event:Event):void {
var ldr:URLLoader = event.currentTarget as URLLoader;
var xmlDP:XML = new XML(ldr.data);
dp = new DataProvider(xmlDP);
myDataGrid.dataProvider = dp;
}
function headerReleaseHandler(event:DataGridEvent):void {
var dg:DataGrid = event.currentTarget as DataGrid;
trace("column: " + String(event.dataField));
trace("descending: " + String(dg.sortDescending));
}
Thanx
Try the sortItemsOn() method on the DataGrid.
See http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/controls/SelectableList.html#sortItemsOn() for an example how to use it.