AS3 text to file - actionscript-3

I'm making a game in cs3 using as3. I've seen dozens of tutorials, but none of them are working for me.
I found the simplest code I could, and it still gives me an error "1061: Call to a possibly undefined method save through a reference with static type flash.net:FileReference."
here's the code I'm using:
var file:FileReference = new FileReference();
file.save("this is a text", "file.txt")

Are you importing FileReference? If not, you'll need:
import flash.net.FileReference;
..inside your package declaration (or at the top of your block of code if you're coding on the time line)
With that import included, your code works for me.

Related

ActionScript 3 - sound mp3 is not played without error

Help me, I use Adobe Flash CS3 Professional.
// i have try using this import, but still not work
/*import flash.events.Event;
import flash.media.Sound;
import flash.net.URLRequest;
*/
// here is the code
var req:URLRequest=new URLRequest("music.mp3");
var snd:Sound=new Sound();
snd.load(req);
var chan:SoundChannel=snd.play();// not work using 'snd.play(1);' too
chan.addEventListener(Event.SOUND_COMPLETE, onPlaybackComplete);
function onPlaybackComplete(event:Event){
trace("Musik Complete");
}
When i run the swf or ctrl+enter, the output Musik Complete without any sound played, but the animation still played. For the animation i just create simple twin motion.
i work on this directory
C:/flash/example.fla
C:/flash/example.swf
C:/flash/music.mp3
I have try to import music.mp3 into the library too.
Note: I get the example from my friend(.swf, .fla, .mp3). I play the .swf that shared by my friend and work, but when i try to create by my self, is not work. So, i think music.mp3 was fine to use for Flash. Here is my friend code:
var Audio:musik = new musik();
var chan:SoundChannel = new SoundChannel();
// play
chan=Audio.play();
// stop
chan.stop();
// i am so confuse i run my friend project and work fine (sound and other), if i write from scrap even i write the same code, there is an error, and this the error:
// 1046: Type was not found or was not a compile-time constant: musik.
// 1180: Call to a possibly undefined method musik.
I just watch on youtube, i missing one step.
In library, open properties of music.mp3 and give it class name.
So, we can call the class using this var Audio:musik=new musik();.
Then, create the Sound Channel.
At least, function to play and stop using this:
//play
chan=Audio.play();
// stop
chan.stop();

Embedding png throws Error #1065: Variable FlexVersion is not defined

I've found several articles relating to variable FlexVersion and error 1065, but nothing seems to help.
EDIT:I've attempted to implement SEVERAL guides on embedding images into flashDevelop with the same result. Everything works correctly until I try to add the embedded image, then I get the above error.
Has no one here seen this error?
My Class (which I've stripped down to nothing in order to pinpoint the issue):
package {
import flash.display.Sprite;
import flash.display.Bitmap;
public class JMouse extends Sprite {
[Embed(source = "../lib/jacobsLadder.png")]
private var Picture:Class;
//private var pic:Bitmap = new Picture(); // THIS LINE
public function JMouse() {
init();
}
private function init():void {
}
}
throws the error when the line "THIS LINE" is not commented out. I've tried adding "as Bitmap" to the end of this line without luck.
I am calling this class from my document class:
jMouse = new JMouse();
the file, jacobsLadder.png, is in my lib folder, and I used FlashDevelop to "Generate Embed Code."
I am using FlashDevelop 5.0.1.3 for .NET 3.5
Any Ideas?
EDIT: I also tried this (and similar variations), as per the suggestion:
"The type of code you can run at variable declaration scope is limited. Creating an instance of Picture requires decoding and so will fail at that point. Instead create your instance of Picture within an instance methods or the constructor."
[Embed(source = "../lib/jacobsLadder.png")]
public static var Picture:Class;
public function JMouse() {
var pic:Bitmap = new Picture();
init();
}
But I get the same error.
The type of code you can run at variable declaration scope is limited. Creating an instance of Picture requires decoding and so will fail at that point. Instead create your instance of Picture within an instance methods or the constructor.
ANSWER: Load the bitmapdata with a loader.
Although the OP (me) was asking how to embed the file, the mysterious problem of "Error #1065: Variable FlexVersion is not defined" issue is apparently an insurmountable one, and I hope that anyone else who may come across this problem may find solace in the following (courtesy of John Mark Isaac Madison et al):
How do you load a bitmap file into a BitmapData object?
While not the an answer, per se, it at least allows the OP to continue on in his work as an alternative to lighting his house on fire.

What does the message "Test Movie Launch Failed" mean? (Flash Professional CC)

The Problem
When I was creating a test program using Flash Professional CC, I got the message "Test Movie Launch Failed" while compiling, and when I tried to open it, nothing happened and it didn't open. However, I got no compiler errors whatsoever, it just said "Test Movie Terminated." in the output.
What Might be Going on
I was thinking it might have been an error in my code, but I can't tell since the error message is extremely vague and no runtime errors appear.
So Here's my code (just in case if it's an error of the code)
Info:
Target: Air 3.6 for Desktop
Script: ActionScript 3.0
Objects:
Input Text Box with instance name of "writeT"
Dynamic Text Box with instance name of "readT"
MovieClip with instance name of "Write" (Too lazy to make buttons)
MovieClip with instance name of "Read"
import flash.filesystem.*;
import flash.events.MouseEvent;
Write.addEventListener(MouseEvent.CLICK, writeToFile);
Read.addEventListener(MouseEvent.CLICK, readFile);
var file:File = File.desktopDirectory.resolvePath("stuff.txt");
var fs:FileStream = new FileStream();
function writeToFile(e:MouseEvent):void{
fs.open(file, FileMode.APPEND);
var text:String = writeT.text;
fs.writeUTF(text);
writeT.text = "";
fs.close();
}
function readFile(e:MouseEvent):void{
fs.open(file, FileMode.READ);
var text:String = fs.readUTF();
readT.text = text;
fs.close();
}
EDIT: I commented all of my code and the error still showed up, so ignore the code
I typically see this if I use a library (.swc or .ane) that the desktop debugger cannot use.
If you using any libraries, perhaps uncouple them to see if the error disappears.
Difficult to say what the exact problem could be. From what I read, seems like a version issue. This thread maybe of some help : https://forums.adobe.com/thread/1007928
On a side note, I noticed that you have your instance named as Write and Read. I would typically avoid such names as they might conflict with reserved keywords, and while naming instances, it's best to name them using a camel case convention.
So, I would name them something Iike writeMc and readMc.
Hope this helps.
if you're publishing a mobile app, test on a mobile. you will get that error when using certain mobile-only code in the test environment. ie, test on an appropriate (ios/android) mobile.

Loading Dynamic Image from Library

In a Flash (Actionscript 3.0) project I'm working on, I'm trying to load a PNG file from the library, however which icon should load is not established until run-time.
The project is a weather bar that loads an icon associated to the data feed that's returned as a JSON object. I have tried the following:
var iconData:Class = getDefinitionByName("i"+image);
var i:* = new iconData(130, 130);
var bitmap:Bitmap = new Bitmap(i);
if(p==0){
today_mc.today_icon.addChild(bitmap);
}
There is a bunch of evaluation that happens to determine "image".
All of the imported files are included as Classes for use in Actionscript, as seen here:
The error I'm getting is:
Implicit coercion of a value with static type Object to a possibly
unrelated type Class.
So if the JSON response says the weather code is '33', and I determine in the script that code 33 = icon 4 (i4), I need to load i4.png to the stage.
In the example I read on the Adobe Site for the function getDefinitionByName, located here, they use the as operator to evaluate the expression before assigning the value to a variable.
So I would recommend trying this first:
var iconData:Class = getDefinitionByName("i"+image); as Class;
Also be sure that you have the proper import statements specified. In the linked example, they list the following; however, the first two I'm unsure if you need based on your application's needs:
import flash.display.DisplayObject;
import flash.display.Sprite;
import flash.utils.getDefinitionByName;
I would suggest keeping your images external to your Flash file. Otherwise, all 30 pngs will be be downloaded every time, even though the user only needs to see one. Then use the AS3 Loader class to load only the png needed at runtime.

Flash AS3 - SoundChannel error #1009

I'm a bit of a Flash newb so apologies if this is a simple issue or fix.
Anyways I'm having problems with some functions. I've noticed I'm getting error 1009 only on functions that attempt to stop a sound channel. Here's some of the code:
This is one the first frame of the first scene, just setting up my channels. All my sounds are done here too but I don't think those are a problem.
import flash.media.Sound;
import flash.media.SoundChannel;
var musicChannel:SoundChannel = new SoundChannel();
var speechChannel:SoundChannel = new SoundChannel();
var sfxChannel:SoundChannel = new SoundChannel();
And here's an example of one of the buttons that causes the 1009 error:
function goHome(e:MouseEvent):void {
speechChannel.stop();
sfxChannel.stop();
musicChannel.stop();
gotoAndPlay(2, "TitleScreen")
}
home.addEventListener(MouseEvent.CLICK, goHome);
Now what really confuses me is that this same code ran perfectly last night on my computer. I went to test my .swf on the computers at my college and my animation froze, so I checked in Flash and this is what I got.
I'm also getting the same 1009 error on some TweenMax stuff. And yes I did import everything necessary and all files that are required for these functions to run are in the root directory of the .fla file. I know this because, as with the other error, this stuff worked on my computer at home perfectly. I am running the exact same .fla file and code file I saved on my computer and I do have the 'com' folder and 'greenstock' swc files in the same directory as my .fla, just like on my PC, yet I'm stilling seeing 1009 with this:
import com.greensock.TweenLite;
import com.greensock.TweenMax;
TweenMax.to(musicChannel, 2, {volume:0});
Again this line is referencing a sound channel, so I'm 99% sure it's the root of these problems. I can swap out 'musicChannel' in this piece of code with 'Music1' (a sound declared in the first frame with the channels) and it stops the music fine, but whenever I try to stop sound channels it craps out on me.
Again sorry if this is really simple to fix but I'm quite confused because it was working fine last night. The Flash Player could be outdated but it's Adobe Flash Player 11 so I don't know if that would be the problem. Right now I'm at college so I don't know what version I have at home.
Thanks for your time.
SoundCahnnel can be null and when they are you can't try to tween their property. An easy way to deal with that is to check whether they are null or not:
if(musicChannel)
{
TweenMax.to(musicChannel, 2, {volume:0});
}
Also SoundChannel objects are generated only when a Sound object play method is called so there's no need to instantiate them:
var musicChannel:SoundChannel = new SoundChannel();//irrelevant piece of code
var musicChannel:SoundChannel;//correct