Having had difficulties to find a rather complete description on this very common subject, I thought I might as well compile a selection of techniques necessary to accomplish this.
first, let's go through the requirements within the HTML page and the swfObject javascript, which comes into the head of the HTML page embedding the swf:
HTML:
-
<script type="text/javascript" src="js/swfobject.js"></script>
-
-
//vars and parameters for embedded swf
-
var flashvars = { };
-
var params = { menu: "false", allowScriptAccess:"sameDomain", allowfullscreen :"true"};
-
var attributes = { id: "main", name: "main" };
-
swfobject.embedSWF("flash/main.swf", "mainFlash", "100%", "100%", "9.0.0","expressInstall.swf", flashvars, params, attributes);
-
</script>
-
-
<!--
-
html, body, #mainFlash{ height:100%; width:100%;}
-
body { margin:0; padding:0; overflow:hidden; background-color:#A31724; text-align:center; }
-
-->
-
</style>
Set both the width and height of your SWF to 100% in your SWFObject definition.
Include CSS to get rid of any default margins/padding and set the height of the html element, the body element and the entire chain of block level HTML elements that your SWF will be nested in to 100%, because Firefox (or: any Gecko based browser) in standards compliant mode (or: using a valid DOCTYPE) interprets percentages in a very strict way (to be precise: the percentage of the height of its parent container, which has to be set explicitly)
Now go to your flash movie, and manage the scaling and alignment of your SWF and the positioning of your SWF's elements, within your ActionScript code, e.g.:
Actionscript:
-
stage.scaleMode = StageScaleMode.NO_SCALE;
-
stage.align = StageAlign.TOP_LEFT;
-
-
stage.addEventListener(Event.RESIZE, resizeHandler);
-
-
function resizeHandler(event:Event):void {
-
trace(stage.stageWidth + " x " + stage.stageHeight);
-
// center stuff
-
}
For one of my projects that let's the user record a profile video through his webcam, I need to publish a JPEG from the recorded video to later display in an html video list.
After turning the web upside down, I found that byteArrays blog's approach seemed the most suitable one. But he works with AMFPHP, which I did not want to add to the other frameworks already involved with this project, and therefor reconstructed it using a simple PHP solution.
I will continue posting my progress here, and offer src files ASAP.
Another source I found, but haven't evaluated yet, because it is built in AS2, works with PHP's GD image library.
For more information on AMFPHP (aka flash remoting for PHP), which is a remoting service for flash applications, that basically allows flash movies to call remote server side applications. Here is an introduction, but I think that it deserves it's own topic in flashcrobat. I hopefully will soon have time to look deeper into it.
<update >
Ok I have come closer :
http://labs.findsubstance.com/2008/04/03/as3-upload-encode-images/
and :
http://www.hackszine.com/blog/archive/2008/04/encoding_jpegs_clientside_in_a.html
http://henryjones.us/articles/using-the-as3-jpeg-encoder
I am going to try to combine the infos found on several resources, and hope that as an outcome we will have a nice reusable application with which you can make a thumbnail of a clip recorded through your webcam. So let's spit in our hands:
The System.setClipboard() method allows a SWF file to replace the contents of the clipboard with a plain-text string of characters. This poses no security risk. To protect against the risk posed by passwords and other sensitive data being cut or copied to clipboards, there is no corresponding "getClipboard" (read) method.
I will here collect a series of links of useful information on the interaction between flash and JavaScript, Until I have evaluated all relevant infos, to write a good summary on common practices.
- SWFObject has grown up! version 2.0
The famous SWFObject to easily embed flash content in html, that was previously hosted on deconcept.com has now moved to google.code basically there are 2 ways to embed your swf into your HTML site:
more to come...
her a basic code sample for the integration using swf object, including parameters, flashvars and attributes. (for many external interface interactions, you will need to give your swf an ID and a name)
here the javascipt to embed:
JavaScript:
-
<script src="http://flashcrobat.noesi.co.uk/js/swfobject.js" type="text/javascript"><!--mce:0--></script>
-
<script type="text/javascript"><!--mce:1--></script>
-
<!-- later in the html page comes the div tag where you want your swf to be embedded where you can put in your alternative text, in case someone has javascript disabled, or no flash plugin installed.-->
-
<div id="swf-div">
-
You need Javascript activated and the flash player plugin installed to be able to view this content.
-
The newest Flash Player can be downloaded here:
-
-
<a href="http://www.adobe.com/go/getflashplayer"><img src="http://www.adobe.com/images/shared/download_buttons/get_flash_player.gif" alt="Get Adobe Flash player" /></a></div>
Attention!!:
giving the id or the name identical name to the swf file causes javascript errors on IE!! took me ages to find the reason for these. So just call your id and name differently than your swf is called.
- external interface approach
this seems to be till now the best and easiest reference I found on smooth communicatin between Javascript and the swf in both directions. unfortunatley this only seems to work in AS2. The old fscommand now has been replaced with the externalInterface class. more to read in the Adobe.liveDocs. found on: http://flexion.wordpress.com/ source files: onBoxNet
- AS3 Script Injection
Complete and unmodified JavaScript and/or VBScript functions, class objects and applications are stored inside AS3 files using XML, and are then parsed, sent to the browser, and executed. This is an ideal solution for Flash/Flex developers who need JavaScript to interact with the user's browser, but might not have full access to the webpage or server that their SWF application is actually hosted on. Flash Ads, YouTube-style video players, and games that may be hosted across multiple (and possibly unforeseen) webpages are the first things that come to mind. on: actionscript.org
- Passing variable from JavaScript To AS3
A brief and basic recollection about passing vars from JS to AS3. found on: metah.ch
- Local Connection Actionscript - Communicate between seperate Flash files
Communication between two seperate flash files placed on the same page (or even running simultaneously on one machine) is a nice way to spread a project out. You can send variable, call functions, pretty much do anything in one swf from another. found on: blog.circlecube.com
- Flash and Google analytics
This also fits in this cathegory, as most functionalities are handled by Javascript. I will update this entry after have=ing implemented the code on my current Project. found on: flashenabledblog.com
sometimes, you want to know the instances class name or the superclass's name. AS3 offers a new function called getQualifiedClassName (flash.utils.getQualifiedClassName) to do just that:
Actionscript:
-
var sprite:Sprite = new Sprite();
-
trace(getQualifiedClassName(sprite)); // "flash.display::Sprite"
-
-
//or to retrieve the name of the super class, use:
-
trace(getQualifiedSuperclassName(sprite)); // "flash.display::DisplayObjectContainer"
If you actually want to sieve through plenty of info about your instance, try describeType() (flash.utils.describeType).
Actionscript:
-
var sprite:Sprite = new Sprite();
-
var spriteDescription:XML = describeType(sprite);
-
trace (spriteDescription);
try to see....(loads of data coming out here...)
what also fits in here is the use ofgetDefinitionByName()(flash.utils.getDefinitionByName).
How annoying! You have your flash application, make a normal external link (a good old www basic), and then the browsers block your external link!, well there seems to be a work around:
Actionscript:
-
function getURL(url:String,window:String="_blank"):void{
-
var broswer:String=ExternalInterface.call("function getBrowser(){return navigator.userAgent}") as String;
-
if(broswer.indexOf("Firefox")!=-1 || broswer.indexOf("MSIE 7.0")!=-1){
-
ExternalInterface.call('window.open("'+url+'","'+window+'")');
-
}else{
-
navigateToURL(new URLRequest(url),window);
-
}
-
}
and the most important is set the wmode property is opaque or transparent.
courtesy of:
http://www.riaidea.com/article.asp?id=27(chinese Blog)
Somehow getURL() survived the AS3 code cull, an interesting approach to make it reusable can be found here on Steven Sacks blog. cheers for that one. Would be interesting to combine them to one. TODO:)
Posted by sortofme on February 29th, 2008
In AS 3.0, if you do want to pass params in your Events you can create your own custom events:
Actionscript:
-
package{
-
-
import flash.events.Event;
-
-
class CustomEvent extends Event{
-
-
public static const MOUSE_DOWN:String = "onMouseDown";
-
-
public var myString:String;
-
-
public function CustomEvent( type:String, str:String ){
-
myString = str;
-
super( type );
-
}//...
-
-
/*_______you then dispatch this like this______*/
-
-
//in hboxvar
-
//you would have to import your custom event
-
dispatchEvent( new CustomEvent( CustomEvent.MOUSE_DOWN, "1,2,3") )
-
-
//again you would need to import your custom event
-
hboxvar.addEventListener( CustomEvent.MOUSE_DOWN, dosomething );
-
-
private function dosomething( event:CustomEvent ):Void{
-
trace( event.myString ) // output: 1,2,3
-
}
another interesting approach can be found here, where flep assigns multiple event types to one custom event, to have more variety in eventHandling.
But probably the most flexible of solutions is to add to the custom Event an Array, as Parameter, alllowing multiple parameters to be passed on when dispatched.
comment: display CustomEvent classÂ
Posted by sortofme on February 26th, 2008
TextFields are created in AS3 basically like most other objects, by first defining and then instantiating them:
Actionscript:
-
</code>
-
-
package {
-
import flash.display.Sprite;
-
import flash.text.*;
-
public class whynot extends Sprite{
-
private var display_txt:TextField;
-
-
public function whynot(){
-
display_txt = new TextField();
-
display_txt.text = "a text";
-
addChild(display_txt);
-
}
-
}
-
}
Here the code of a function I use for creating my textfields. still basic, but displays the most common settings needed for your dynamic textField. assumingyou have a font added in the library with the class instance name "_tahoma":
Actionscript:
-
private function makeTextField():void {
-
-
var tahoma:Font = new _tahoma();
-
-
TFFormat = new TextFormat();
-
-
TFFormat.font =tahoma.fontName;
-
TFFormat.color = "0xFFFFFF";
-
TFFormat.size = 11;
-
-
TextField = new TextField();
-
TextField.width = 200;
-
TextField.height = height;
-
TextField.x = 42;
-
TextField.y = 10;
-
TextField.wordWrap = true;
-
TextField.selectable = false;
-
TextField.border = true;
-
TextField.multiline = false;
-
TextField.embedFonts = true;
-
TextField.autoSize = TextFieldAutoSize.LEFT;
-
TextField.antiAliasType = AntiAliasType.ADVANCED;
-
-
addChild(alertTextField);
-
-
alertTextField.setTextFormat(alertTFFormat);
-
-
};
One issue I discovered that I can't really explain, is that I often have to reassign the textFormat to the TextField when changing the fields content:
Actionscript:
-
alertTextField.text = myString;
-
alertTextField.setTextFormat(alertTFFormat);
setting the focus on an input field:
http://livedocs.adobe.com/flex/201/langref/flash/display/Stage.html#focus
in AS3, it's now a property rather than a function that you call. Also, you don't use the text name value of the field, but a reference to the actual field itself...
would look something like:
Actionscript:
-
myObj.stage.focus = myTextField;
Posted by sortofme on February 22nd, 2008
first off the link to the class documentation on Adobe Livedocs/MouseEvents:
Actionscript:
-
//simple way of creating a default Button
-
package{
-
import flash.display.MovieClip;
-
import flash.events.MouseEvent;
-
public class NavButton extends MovieClip {
-
public function NavButton() {
-
this.mouseEnabled = true;//to display the hand cursor on Rollover
-
this.buttonMode = true;
-
this.addEventListener(MouseEvent.ROLL_OVER, dim);
-
this.addEventListener(MouseEvent.ROLL_OUT, bright);
-
}
-
private function dim ( event:MouseEvent ) : void{
-
this.alpha= .75;
-
}
-
private function bright(event:MouseEvent):void{
-
this.alpha = 1;
-
}
-
}
-
}
Recent Comments