Incorrect calling of external mp3 file - actionscript-3

I am getting Error:#2044, so I assumed that my code is wrong in calling the Sound functions but I can't seem to find where I am making the error.
package {
import flash.display.MovieClip;
import flash.media.SoundChannel;
import flash.media.Sound;
import flash.net.URLRequest;
public class Tile extends MovieClip{
public function GetAndSwitchKey():String {
//Some Code//
//Create sounds
//------------> Start here
var bSound:Sound = new Sound();
var bReq:URLRequest = new URLRequest("B.mp3");
var oSound:Sound = new Sound();
var oReq:URLRequest = new URLRequest("O.mp3");
var mSound:Sound = new Sound();
var mReq:URLRequest = new URLRequest("M.mp3");
//Load sounds
bSound.load(bReq);
oSound.load(oReq);
mSound.load(mReq);
//<-------- End here
//Some code//
switch (temp) {
case "B": bSound.play(); break;
case "O": oSound.play(); break;
case "M": mSound.play(); break;
}
//Some code//
}
}
}
The way I added the files is by placing them inside the same file as the Action script 3 file. I also then changed them in the propperties to be exportable for action script. But as far as I am aware I don't have to specify the directory since a copy is made in the SWF.

Replacing the code indicated between the "start here" and "end here". This will resolve the error. And will call the sound file properly.
var mySoundHolder = new mySound();

Related

How can I call a function in my as file from my fla without linking it?

I've got an .as file in the same folder of my FLA file.
I'd like to call trigger a function from an action frame of my as file.
My as file is like that :
package {
import flash.display.MovieClip;
import flash.text.TextField;
import flash.events.MouseEvent;
public class Game extends MovieClip {
var words:Array = new Array; //a mini database to hold the words
var rand1:int;var rand2:int; //variables used for randomization
var scramble_Array:Array = new Array; //array used to scramble the word
var unscramble_Array:Array = new Array; //array to hold the unscrambled word
var temp:String; //temporary variable
var pauser:Boolean; //used to pause the game
public function Game() {
getword();
checker=new button_chk;addChild(checker);
checker.x=150;checker.y=400;
checker.addEventListener(MouseEvent.CLICK, check_answer);
}
...
...
How can I call the function "Game" in my FLA file using action frame ?
I've tried to do
Game();
But it says that one argument is missing.
It is a class constructor, you don't just call it, you instantiate (create a new instance of) the class with the new operator:
import Game;
var aGame:Game = new Game;

How to successfully unload a swf, and go to parent swf, after pressing the exit button

Using a code snippet in as3 flash, but struggling for it to actually work:
I would like to unload the child SWF (which is a video) and go back into the parent SWF (a video selection page). Tried so many different ways and need a quick and easy solution. Thanks.
Below does not seem to work...
exit_btn.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF);
import fl.display.ProLoader;
var fl_ProLoader:ProLoader;
//This variable keeps track of whether you want to load or unload the SWF
var fl_ToLoad:Boolean = true;
function fl_ClickToLoadUnloadSWF(event:MouseEvent):void
{
fl_ProLoader.unload();
removeChild(fl_ProLoader);
fl_ProLoader = null;
}
I use this code it's tested
import flash.filesystem.*;
import flash.events.MouseEvent;
import flash.net.*;
import flash.events.*;
var _file: File;
_file = File.documentsDirectory.resolvePath("./Directory to swf file/mySwf.swf");
if (_file.exists) {
trace("SWF loading ...........");
displayingSWF(_file.nativePath)
} else {
trace("the file doesn't exit");
}
//////////////// DIsplay the SWF story file //////////////////
var mLoader: Loader ;
function displayingSWF(lnk) {
var inFileStream: FileStream = new FileStream();
inFileStream.open(_file, FileMode.READ);
var swfBytes: ByteArray = new ByteArray();
inFileStream.readBytes(swfBytes);
inFileStream.close();
mLoader = new Loader();
var loaderContext: LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
loaderContext.allowCodeImport = true;
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onSwfLoadComplete);
mLoader.loadBytes(swfBytes, loaderContext);
}
function onSwfLoadComplete(e: Event): void {
mLoader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onSwfLoadComplete);
addChild(mLoader);
}
exit_btn.addEventListener(MouseEvent.CLICK, dimising);
function dimising(e: MouseEvent): void {
mLoader.unloadAndStop();
removeChild(mLoader);
}

embedded font flash.text.engine

I've completely run out of ideas on this. It follows, and is part of my previous question:
embedding a font in a swf using as3
I just don't seem to be able to get the flash.text.engine to use my embedded font. NB the font has to be loaded into the application (as embedded in swf) after the user has chosen the two languages (for translation to and from). There seems to be a little info implying that it is now necessary to use the fontswf application which is in the sdk. I have tried this and produced loadable swf files but I can't find any info on how these are then loaded (i.e. the getDefinition and registerFont bits below don't work as there are no classes in these swf) and applied to text.engine objects. The source for the embedding is in my answer to my question above. This is a test as3 which demonstrates how it doesn't work!
package
{
import flash.display.Loader;
import flash.display.Shape;
import flash.display.Sprite;
import flash.events.Event;
import flash.text.engine.ElementFormat;
import flash.text.engine.FontDescription;
import flash.text.engine.TextBlock;
import flash.text.engine.TextLine;
import flash.text.engine.TextElement;
import flash.net.URLRequest;
import flash.text.Font;
public class Main extends Sprite
{
private var loader:Loader;
private var tl:TextLine;
public function Main():void
{
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,fontLoaded);
loader.load(new URLRequest("EnglishF.swf"));
}
private function fontLoaded(evt:Event):void {
var FontClass:Class
FontClass = evt.target.applicationDomain.getDefinition("EnglishF") as Class;
try {
Font.registerFont(FontClass.myFont);
trace("successfully loaded " + FontClass);
// gives 'successfully loaded EnglishF'
} catch (err:Error) {}
var fontList:Array = Font.enumerateFonts();
for (var i:int = 0; i < fontList.length; i++) {
trace(fontList[i].fontName, fontList[i].fontType);
// gives 'EnglishF embeddedCFF'
}
var block:TextBlock = new TextBlock();
var font:FontDescription = new FontDescription("EnglishF");
var formt:ElementFormat = new ElementFormat(font, 30);
trace(FontDescription.isFontCompatible("EnglishF","normal","normal"), formt.fontDescription.fontName);
// gives 'true EnglishF'
formt.color = 0x882233;
var span:TextElement = new TextElement("Hello World. This is certainly NOT in the Font provided!", formt);
block.content = span;
tl = block.createTextLine();
tl.x = 10;
tl.y = tl.ascent + 10;
addChild(tl);
}
}
}
Am I doing anything wrong, or is this impossible?
Hopefully this will help. Under this line:
var font:FontDescription = new FontDescription("EnglishF");
add the following line:
font.fontLookup = FontLookup.EMBEDDED_CFF;
This will let the text framework know that you're using a CFF font (used in the new text framework), instead of a regular embedded font (used in TextFields).
Hope this helps.
Jordan

Flash AS3 FileReference - Select and Upload Multiple Files one at a time

I currently have an swf that allows you to select and display a file and upload it to the server using FileReference. This works great but i need to be able to select and display multiple (up to 25 in some cases) and then upload them all at the end.
I know you can use the FileReferenceList to select multiple files at the same time in the dialog popup but my issue is that the user needs to select one at a time, do stuff to that image, then select another, do stuff and so on... then at the end when they press upload it upload them all onto the server.
Is it possible or is there a way so i can maybe add every new selected file into an array, then at the end it uploads all the files in the array either in one go or loops through each until all objects in the array are complete?
Can anyone help? I'll post the full code for my working single file upload below.
Please, please help, i'm limited with flash and have only been learning for 3-4 months and i've been stuck for over a week now :(
Lauren
as3 Code:
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
import fl.controls.ProgressBarMode;
import flash.display.Bitmap;
import flash.display.BitmapData;
progressBar.visible=false;
UploadprogressBar.visible=false;
// Create FileReference.
var imageFile:FileReference;
// Create Loader to hold image content
var image_loader:Loader = new Loader();
var image_content:Sprite = new Sprite();
// Get Extension Function.
var imageExtension;
function getExtension($url:String):String {
var extension:String = $url.substring($url.lastIndexOf(".")+1, $url.length);
return extension;
}
// Random Number Function to create new filename on server.
function randomNum(low:Number=0, high:Number=1):Number {
return Math.floor(Math.random() * (1+high-low)) + low;
}
var myNumber:Number= randomNum(1000,9999);
var myString:String= String(myNumber);
var RandomNumbers = myString;
var imageFilePath = (RandomNumbers);
// Select Button Function.
select_btn.addEventListener(MouseEvent.CLICK, onselect_btnClicked);
function onselect_btnClicked(event:MouseEvent):void {
imageFile=new FileReference();
imageFile.addEventListener(Event.SELECT, onFileSelected);
var imageTypeFilter:FileFilter = new FileFilter("JPG/PNG Files","*.jpeg; *.jpg;*.gif;*.png");
imageFile.browse([imageTypeFilter]);
}
// File Selected Function.
function onFileSelected(event:Event):void {
imageFile.addEventListener(Event.COMPLETE, onFileLoaded);
imageFile.addEventListener(ProgressEvent.PROGRESS, onProgress);
var imageFileName = imageFile.name;
imageExtension = getExtension(imageFileName);
imageFile.load();
progressBar.visible=true;
progressBar.mode=ProgressBarMode.MANUAL;
progressBar.minimum=0;
progressBar.maximum=100;
UploadprogressBar.mode=ProgressBarMode.MANUAL;
UploadprogressBar.minimum=0;
UploadprogressBar.maximum=100;
}
// File Progress Function.
function onProgress(event:ProgressEvent):void {
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
progressBar.setProgress(percentLoaded, 100);
}
// File Loaded Function.
function onFileLoaded(event:Event):void {
var fileReference:FileReference=event.target as FileReference;
var data:ByteArray=fileReference["data"];
var movieClipLoader:Loader=new Loader();
movieClipLoader.loadBytes(data);
movieClipLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onMovieClipLoaderComplete);
imageFile.removeEventListener(Event.COMPLETE, onFileLoaded);
imageFile.removeEventListener(ProgressEvent.PROGRESS, onProgress);
}
// Load Image onto Stage Function.
function onMovieClipLoaderComplete(event:Event):void {
var loadedContent:DisplayObject=event.target.content;
image_loader =event.target.loader as Loader;
var scaleWidth:Number=345/image_loader.width;
image_loader.scaleX=image_loader.scaleY=scaleWidth;
image_loader.height=200;
image_loader.scaleX=image_loader.scaleY;
image_loader.x=10;
image_loader.y=10;
image_content.buttonMode=true;
image_content.addChild(image_loader);
addChild(image_content);
}
// Upload Button Function.
upload_btn.addEventListener(MouseEvent.CLICK, onupload_btnClicked);
function onupload_btnClicked(event:MouseEvent):void {
var filename:String=imageFile.name;
var urlRequest:URLRequest = new URLRequest("http://www.xxxxx.com/xxxxx/file-reference.php");
var variables:URLVariables = new URLVariables();
urlRequest.method = URLRequestMethod.POST;
imageFile.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
imageFile.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadComplete);
variables.ID = (RandomNumbers);
urlRequest.data = variables;
imageFile.upload(urlRequest);
}
// File Upload Progress Function.
function onUploadProgress(event:ProgressEvent):void {
var percentLoaded:Number=event.bytesLoaded/event.bytesTotal*100;
UploadprogressBar.setProgress(percentLoaded, 100);
trace("loaded: "+percentLoaded+"%");
upload_status_txt.text='upload in progress...';
}
// Upload File Completed Function.
function onUploadComplete(event:Event):void {
upload_status_txt.text='upload complete';
imageFile.removeEventListener(ProgressEvent.PROGRESS, onProgress);
imageFile.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUploadComplete);
UploadprogressBar.visible=false;
}
php Server Code:
<?php
function getExtension($str) {
$i = strrpos($str,".");
if (!$i) { return ""; }
$l = strlen($str) - $i;
$ext = substr($str,$i+1,$l);
return $ext;
}
$fileID = $_POST['ID'];
$filename = basename( $_FILES['Filedata']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
$uploadID = $fileID.'.'.$extension;
if (move_uploaded_file($_FILES['Filedata']['tmp_name'], "images/".$uploadID))
{
echo "OK";
}
else
{
echo "ERROR";
}
?>
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html
"When you call the FileReferenceList.browse() method, which creates an array of FileReference objects."
You need to use FileReferenceList instead of FileReference.

Error 1119 when creating "Load Text" button

A section of a Flash animation I'm creating involves an area where people can write on a notepad, save their work and update it at a later time. The file will be downloaded by users before they run it, rather than from a webpage. Here is the code I have so far:
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.display.MovieClip;
import flash.events.Event;
stop();
var MyNotes:FileReference = new FileReference()
Save_btn.addEventListener (MouseEvent.CLICK, SaveText);
function SaveText(Event:MouseEvent):void {
MyNotes.save(TypeOwn_txt.text, "MyNotes.txt");
}
Load_btn.addEventListener (MouseEvent.CLICK, LoadText);
function LoadText(Event:MouseEvent):void {
MyNotes.addEventListener(Event.SELECT, onFileSelected);
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; .html;*.htm;*.php");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
MyNotes.browse([swfTypeFilter, allTypeFilter]);
}
function onFileSelected(event:Event):void
{
trace("onFileSelected");
MyNotes.addEventListener(Event.COMPLETE, onFileLoaded);
MyNotes.load();
}
function onFileLoaded(event:Event):void
{
var fileReference:FileReference=event.target as FileReference;
var data:ByteArray=fileReference["data"];
TypeOwn_txt.text=data.toString();
}
The problem is I receive a "Symbol 'Structure Summary', Layer 'Actions', Frame 29, Line 19 1119: Access of possibly undefined property SELECT through a reference with static type flash.events:MouseEvent.
" in regards to the line "MyNotes.addEventListener(Event.SELECT, onFileSelected);". I've done some research and understand this is something to do with the parent not being identified as a MovieClip, or something along those lines. I'm still not sure, however I don't have a clue how to proceed! Thanks.
Just to let you know the problem is solved, although I really don't know how. I used this site for a template and worked backwards. I'll put in the working Code below in case it's of use to somebody else.
import flash.events.MouseEvent;
import flash.net.FileReference;
import flash.net.FileFilter;
import flash.utils.ByteArray;
import flash.events.MouseEvent;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.MovieClip;
var myNotes:FileReference;
Load_btn.addEventListener(MouseEvent.CLICK, onLoadClicked);
function onLoadClicked(event:MouseEvent):void
{
trace("onBrowse");
myNotes=new FileReference();
myNotes.addEventListener(Event.SELECT, onFileSelected);
var swfTypeFilter:FileFilter = new FileFilter("Text Files","*.txt; *.html;*.htm;*.php");
var allTypeFilter:FileFilter = new FileFilter("All Files (*.*)","*.*");
myNotes.browse([swfTypeFilter, allTypeFilter]);
}
function onFileSelected(event:Event):void
{
trace("onFileSelected");
myNotes.addEventListener(Event.COMPLETE, onFileLoaded);
myNotes.addEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
myNotes.load();
}
function onFileLoaded(event:Event):void
{
var fileReference:FileReference=event.target as FileReference;
var data:ByteArray=fileReference["data"];
textArea.text=data.toString();
myNotes.removeEventListener(Event.COMPLETE, onFileLoaded);
myNotes.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
}
function onFileLoadError(event:Event):void
{
myNotes.removeEventListener(Event.COMPLETE, onFileLoaded);
myNotes.removeEventListener(IOErrorEvent.IO_ERROR, onFileLoadError);
trace("File load error");
}
Save_btn.addEventListener (MouseEvent.CLICK, SaveText);
function SaveText(Event:MouseEvent):void {
myNotes=new FileReference();
myNotes.save(textArea.text, "MyNotes.txt");
}
Thanks to everyone who contributed.