swf code, and file name is test.swf:
function test() {
trace('this is a test');
}
test();
I visit it by http://localhost/test.swf
but I get nothing in window.
chrome version: 46.0.2490.86 m
chrome:plugin
Adobe Flash Player - version: 19.0.0.245
Shockwave Flash 19.0 r0
It would seem you are unclear on what trace does in ActionScript.
While some languages have a print or echo type command, AS does not.
trace() is for debugging, to send a message to the output panel when your code is running in the debug flash player.
So with the code you've shown, the expected result is a blank screen as the code doesn't do anything that affects the screen.
If you want to put text on the screen, you'd have to do the following:
function test():void {
var tf:TextField = new TextField();
tf.text = "this is a test";
addChild(tf);
}
test();
If you want to use trace statements in the browser for debugging, you'll have to install the flash debug player for your browser.
Once installed, you can start a remote debug session in FlashPro.
Please follow this tutorial for making textFields to hold visible text within your SWF.
Textfield tutorial
Alternatively if you want something like a browser alert/pop-up then try
Javascript Alert from AS3 tutorial
Related
I know that we can not record/Access anything out of flash player, because of it's security sandbox.
Instead We can record the video while streaming it to server.
like
netStream.publish("YourFileName.flv","record");
But in recorde file, I will get only a video file Published by webcam to server and i want to record entire session.
Is there any way to record it locally, or Can i record the window ??
p.s. : I am not trying to access anything outside of flash player.
Thanks in advance...
ok, so you can record the entire contents of the swf like this:
first, create a container object (anything that extends DisplayObject which implements IBitmapDrawable) then place everything that you want to capture (video view and everything else in your "session") then using an ENTER_FRAME event or Timer (preferable to control capture fps), draw the container to a BitmapData using BitmapData.draw(container). Pass that BitmapData to the FLV encode library found here using it's addFrame() method (docs and examples come with that library... super easy) and that's it! When you are done, you will have an flv video containing a frame by frame "screen capture" of what was going on in your swf! Just make sure EVERYTHING is in the container! That lib also accepts captured audio too if you want.
private function startCapturing():void{
flvEncoder.start(); // see the libs docs and examples
var timer:Timer = new Timer(1000/15); //capture at 15 fps
timer.addEventListener(TimerEvent.Timer, onTimer);
timer.start();
}
private function onTimer(event:TimerEvent):void{
var screenCap:BitmapData = new BitmapData(container.width, container.height);
screenCap.draw(container);
flvEncoder.addFrame(screenCap); // see the libs docs and examples
}
As I understand, ActionScript is used mostly to control graphical output on Flash websites: such as Flash based games.
However, I would like ActionScript to perform tasks not related to graphical output. Tasks, which for browser compatibility reasons, are more suitable for ActionScript: such as file upload.
Is it therefore possible to use ActionScript instead of JavaScript or to accomplish tasks that are not possible with JavaScript, such as file upload?
Are the following possible?
Run ActionScript on HTML Button Press?
Send information to ActionScript from HTML/JavaScript?
Process information without any graphical output in ActionScript?
Ouptut information from ActionScript to HTML/JavasScript?
I wish to know, if ActionScript can do what I wish.
I will have a picture of the right functions to call.
ExternalInterface is your friend:
http://help.adobe.com/nl_NL/Flash/CS5/AS3LR/flash/external/ExternalInterface.html
Some tips when using ExternalInterface:
set allowScriptAccess to "always" in the html embed code
make sure the flash has an id in your html code
Some simple examples:
1. Grab a value from javascript to flash
// actionscript 3 code
if (ExternalInterface.available)
{
var url:String = ExternalInterface.call("document.location");
// output to textfield
var t:TextField = new TextField();
addChild(t);
t.text = url;
}
2. Call a function with parameters from flash
// actionscript 3 code
if (ExternalInterface.available)
{
var result:String = "Flash rocks"
ExternalInterface.call("alert", result);
}
3. Call from javascript a function with parameters to Flash:
// javascript
window.onLoad = function()
{
document.getElementById('flashId').doSomething("javascript rocks");
}
.. and
// actionscript 3
if (ExternalInterface.available)
{
ExternalInterface.addCallback("doSomething", handleSomethingFromJavascript);// links js function to as3 function
function handleSomethingFromJavascript(value:String):void
{
// output to textfield
var t:TextField = new TextField();
addChild(t);
t.text = value;
}
}
You can do lots of stuff between flash and javascript, as you can see the intergration is almost painless! The only note is that within flash ExternalInterface is not available, so you have to test in browser. You can make a transparent Flash object using wmode="transparent". You cannot use display:none or visibility (css) because then the flash isnt executed or acts slower. To make sure it keeps running, place it position:fixed (css) on the page in a corner or something. Browsers make flash object run in a sort of sleep mode (slower) when out of screen or when inactive (ie in an inactive tab)
You can't really replace javascript with actionscript, but you can interact with it.
"Run ActionScript on HTML Button Press?" - Yes, this is possible through ExternalInterface.registerCallback. However, many actions (iirc, opening the file browser) can only be done on user interaction in flash, so you would need a flash button for that.
"Send information to ActionScript from HTML/JavaScript?" Also through externalInterface, or flashvars (but only at startup).
"Process information without any graphical output in ActionScript?" - It's a programming language, so sure. What did you have in mind?
"Ouptut information from ActionScript to HTML/JavasScript?" - yes, also through ExternalInterface.
i feel stupid asking that question, but it seems like i'm unable to find out.
i have a SWF that works perfectly inside flash. once i export as SWF, it doesn't work anymore.
i can't use "trace"
i have tryed that function but it doesn't trigger it for some reason. :
function quikTrace(string:String){
//INIT VARIABLES
var tmpTxtField:TextField;
//TEXTFIELD PROPERTIES
tmpTxtField = new TextField();
tmpTxtField.text = string;
tmpTxtField.x = stage.width / 2;
tmpTxtField.textColor = 0xFFFFFF; //white (black background)
tmpTxtField.y = stage.height / 2;
addChild(tmpTxtField);
}
is there any "outside the box" way to find errors in a SWF?
You can view trace statements in the browser by doing a few quick steps:
Get a debug flash player for your browser. link
Set up mm.cfg to enable flash logging to a file. link
Get a text editor / "tail" viewer and read the flashlog.txt file that is generated. This is where FireBug & Flashbug come in handy, if you're in firefox. You can also try baretail for PC, or regular old "tail -f" on a mac/linux machine.
Good luck.
It's not a stupid question at all! You're just learning how to debug Flash for the first time. You can use "trace" with an exported SWF by doing a debug/test build from Flash IDE or Flash Develop. You can then read these traces with Firefox by using the extentions Firebug combined with the Flashbug extension. Now you're all set for getting feedback using traces.
For this particular problem, it's hard to say. Is this function inside of a class or timeline code? If it's a class, it probably because Stage is not ready yet. You can use the event Event.ADDED_TO_STAGE to know when the Stage reference is ready to be used.
Hope this helps!
Install a debug version of flash player for your browser. It will tell you exactly which line of code is throwing error.
http://www.adobe.com/support/flashplayer/downloads.html
I have a textField on my stage named 'adBuy', which when clicked I want to open up my browser with the defined in URL request. However when I click on the 'adBuy' textField on my SWF it opens Coda, the piece of software I'm using to write this small piece of code?
I am puzzled. Here is my code:
adBuy.defaultTextFormat = adFormat;
adBuy.textColor = 0xFF65CB;
adBuy.x = 640;
adBuy.y = 455;
adBuy.text = "Buy Now";
parent.addChild(adBuy);
adBuy.addEventListener(MouseEvent.CLICK, buyAdvert);
var request:URLRequest = new URLRequest("http://www.google.co.uk");
function buyAdvert(event:MouseEvent):void {
navigateToURL(request, "_blank");
trace("link clicked");
}
Is there an error in my code, or is this a common problem for which there is an answer?
Sorry, I have solved my problem.
It appears the reason it was not opening a web browser with the URL it because I was running the SWF through 'Test Movie' in Flash. This appears to have been stopping the code working.
It did however work fine when I ran it in Flash Player.
I'm a complete newbie to this, I've been watching tons of tutorials, reading tutorials, reading the Adobe site, etc... The last time I programmed was BASIC on Apple IIe...
I'm trying to create a preloader that my html page will link to, and when the main .swf file (Portfolio.swf) is loaded, it will display and stop (it's a print swf, so I don't want it to "Play" at 24 frames/sec.)
I have errors in my AS3 of 1120, which I will copy the code here:
stop();
var loader : Loader = new Loader ();
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, progressHandler);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.load(new URLRequest ("Portfolio.swf"));
function progressHandler(e:ProgressEvent) {
var percent:Number=Math.round(e.bytesLoaded*100/e.bytesTotal);
preloader.gotoAndStop(percent);
}
function completeHandler(e:Event) {
this.addChild(loader);
this.removeChild(preloader);
}
And here's what I uploaded to my site so far to test:
http://krisbunda.com/templates/portfolio/Portfolio.html
as you can see, it just loops the progress bar, and doesn't load the "Portfolio.swf" file afterward. The actual "Portfolio.swf" can be viewed on this page:
[actually, I'm new, can't post more than 1 hyperlink... you can find the Portfolio on the root of my site.]
it takes a while to download the 6MB+ file, which is why I would like a preloader.
Thanks for your help!
I finally figured it out, albeit the code has evolved (devolved?) to something different.
I'm not at home to copy the AS3 from my workstation, but here's a link to see the code working as I intended it:
http://krisbunda.com/images/portfolio/Portfolio.html
send me a message if you are having a similar problem and would like the AS3 code pasted here.