With ExternalInterface function in AS3, we can call javascript functions that are outside flash and vice versa.
code (flash):
//we can set a callback to our function in flash to enable javascript function to call it
if (ExternalInterface.available) {
ExternalInterface.addCallback("send_from_external", send_from_external);
}
function send_from_external(newText) {
trace(newText);
}
//to call a javascript function from flash
if (ExternalInterface.available) {
ExternalInterface.call("updateMsg", "your string");
}
code (html):
<script language="JavaScript">
function getFlashId(idIe, idMoz) {
var isIE = navigator.appName.indexOf("Microsoft") != -1;
return (isIE) ? idIe : idMoz;
}
function send_message() {
$("#txt_area").attr("disabled", "disabled");
getFlashMovie(getFlashId("swfObject", "swfEmbed")).send_from_external("test_string"); //trigger function inside flash
}
function updateMsg(string) {
alert(string); //from flash
}
</script>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
id="swfObject"
width="1"
height="1"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">
<param name="movie" value="test.swf" />
<param name="quality" value="low" />
<param name="allowScriptAccess" value="always" />
<param name="flashVars" value="" />
<embed id="swfEmbed"
src="test.swf"
quality="low"
width="1"
height="1"
name="ExternalInterfaceExample"
align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="always"
type="application/x-shockwave-flash"
pluginspage="http://www.macromedia.com/go/getflashplayer"
flashVars="">
</embed>
</object>
Showing posts with label as3. Show all posts
Showing posts with label as3. Show all posts
Thursday, November 19, 2009
Wednesday, November 11, 2009
AS3 LocalConnection | local connection between flash | flash internal connection
Here're the basic structure to simple LocalConnection for flash as3. LocalConnection is used when you want to connect 2 or more swf in a pc to exchange data.
To receive:
//receive
var receivingLC:LocalConnection;
receivingLC = new LocalConnection();
receivingLC.client = this;
receivingLC.connect('session_name');
function myMethod(textRecieved:String){
//do your things
}
To send:
//sending
var sendingLC:LocalConnection;
sendingLC = new LocalConnection();
sendingLC.send('message_input', 'myMethod', "test");
sendingLC.addEventListener(StatusEvent.STATUS, onStatus);
function onStatus(event:StatusEvent){
switch (event.level) {
case "error":
trace("LocalConnection.send() failed");
break;
case "status":
trace("LocalConnection.send() succeeded");
break;
default:break;
}
}
To receive:
//receive
var receivingLC:LocalConnection;
receivingLC = new LocalConnection();
receivingLC.client = this;
receivingLC.connect('session_name');
function myMethod(textRecieved:String){
//do your things
}
To send:
//sending
var sendingLC:LocalConnection;
sendingLC = new LocalConnection();
sendingLC.send('message_input', 'myMethod', "test");
sendingLC.addEventListener(StatusEvent.STATUS, onStatus);
function onStatus(event:StatusEvent){
switch (event.level) {
case "error":
trace("LocalConnection.send() failed");
break;
case "status":
trace("LocalConnection.send() succeeded");
break;
default:break;
}
}
Wednesday, October 28, 2009
AS3 simple email validation check
function checkMail(pmail:String) {
if (! (pmail.lastIndexOf(".") <= pmail.indexOf("@") || pmail.indexOf("@")==-1)) {
return true;
} else {
return false;
}
}
if (! (pmail.lastIndexOf(".") <= pmail.indexOf("@") || pmail.indexOf("@")==-1)) {
return true;
} else {
return false;
}
}
Monday, September 28, 2009
AS3 Socket Connection
This example shows how to successfully connect flash to your ftp server..
var s:Socket = new Socket("YOUR_SERVER", 21);
s.addEventListener(ProgressEvent.SOCKET_DATA, sData);
function sData(e:ProgressEvent):void
{
var d:String = s.readUTFBytes(s.bytesAvailable);
trace(d);
if(d.indexOf("+OK Hello there") != -1)
s.writeUTFBytes("USERNAME\n");
if(d.indexOf("+OK Password required.") != -1)
s.writeUTFBytes("PASSWORD\n");
if(d.indexOf("+OK logged in.") != -1)
s.writeUTFBytes("STAT\n");
s.flush();
}
var s:Socket = new Socket("YOUR_SERVER", 21);
s.addEventListener(ProgressEvent.SOCKET_DATA, sData);
function sData(e:ProgressEvent):void
{
var d:String = s.readUTFBytes(s.bytesAvailable);
trace(d);
if(d.indexOf("+OK Hello there") != -1)
s.writeUTFBytes("USERNAME\n");
if(d.indexOf("+OK Password required.") != -1)
s.writeUTFBytes("PASSWORD\n");
if(d.indexOf("+OK logged in.") != -1)
s.writeUTFBytes("STAT\n");
s.flush();
}
Thursday, June 11, 2009
AS3 - Load xml, another approach
var xmlloader:URLLoader = new URLLoader();
xmlloader.addEventListener(Event.COMPLETE, handleComplete);
xmlloader.load(new URLRequest("test.xml"));
var theXML:XML = new XML();
theXML.ignoreWhitespace = true;
function handleComplete(e:Event){
theXML = XML(e.target.data);
var itemList:XMLList = theXML.child("*");
for each (var _item:XML in itemList) {
trace(_item.title.toString());
trace(_item.description.toString());
}
}
xmlloader.addEventListener(Event.COMPLETE, handleComplete);
xmlloader.load(new URLRequest("test.xml"));
var theXML:XML = new XML();
theXML.ignoreWhitespace = true;
function handleComplete(e:Event){
theXML = XML(e.target.data);
var itemList:XMLList = theXML.child("*");
for each (var _item:XML in itemList) {
trace(_item.title.toString());
trace(_item.description.toString());
}
}
Thursday, May 21, 2009
Flash AS3 - Detecting key press | identify Keyboard press | keyCode event
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyboardListener);
function keyboardListener(event:KeyboardEvent):void {
trace("event.keyCode: " + event.keyCode);
}
function keyboardListener(event:KeyboardEvent):void {
trace("event.keyCode: " + event.keyCode);
}
Saturday, May 16, 2009
AS3 - crossdomain explanation
In AS3, crossdomain.xml must be placed on the server that provided the service itself, for example, I want to get the rss feed from lifehacker.com, when I do the URLRequest to http://feeds.gawker.com/lifehacker/full flash will request http://feeds.gawker.com/crossdomain.xml.
I guest that should explain how AS3 uses the crossdomain.xml to do crossdomain stuffs :)
I guest that should explain how AS3 uses the crossdomain.xml to do crossdomain stuffs :)
Monday, May 4, 2009
Flash AS3 - Using Caurina tweener
//Download the class here: http://code.google.com/p/tweener/downloads/list
import caurina.transitions.Tweener;
Tweener.addTween(movieClip, { alpha: 1, time: 10, onComplete: function(){}});
//documentation here: http://hosted.zeh.com.br/tweener/docs/en-us/
import caurina.transitions.Tweener;
Tweener.addTween(movieClip, { alpha: 1, time: 10, onComplete: function(){}});
//documentation here: http://hosted.zeh.com.br/tweener/docs/en-us/
Sunday, April 26, 2009
Flash AS3 - Draw lines
var lineDrawing:MovieClip = new MovieClip();
lineDrawing.graphics.lineStyle(1);
lineDrawing.graphics.moveTo(0,0); //start from 0,0
lineDrawing.graphics.lineTo(50, 50); //draw line to 50,50
//to clear the drawings:
lineDrawing.graphics.clear();
lineDrawing.graphics.lineStyle(1);
lineDrawing.graphics.moveTo(0,0); //start from 0,0
lineDrawing.graphics.lineTo(50, 50); //draw line to 50,50
//to clear the drawings:
lineDrawing.graphics.clear();
Thursday, April 23, 2009
Flash AS3 TransitionManager class
//Imports TransitionManager class along with all available transition types and easing functions.
import fl.transitions.*;
import fl.transitions.easing.*;
// Declare global variables
var tMgr:TransitionManager;
// apply the transition to MovieClip object
tMgr = new TransitionManager(MovieClip);
tMgr.startTransition({type:Iris, direction:Transition.OUT, duration:2, easing:Regular.easeOut});
tMgr.addEventListener("allTransitionsOutDone", whenDone); // when the transition done, execute whenDone function
function whenDone(e:Event):void {
//some actions here
}
/*
transition types available:
Wipe
Photo
Blinds
Iris
PixelDissolve
Zoom
*/
import fl.transitions.*;
import fl.transitions.easing.*;
// Declare global variables
var tMgr:TransitionManager;
// apply the transition to MovieClip object
tMgr = new TransitionManager(MovieClip);
tMgr.startTransition({type:Iris, direction:Transition.OUT, duration:2, easing:Regular.easeOut});
tMgr.addEventListener("allTransitionsOutDone", whenDone); // when the transition done, execute whenDone function
function whenDone(e:Event):void {
//some actions here
}
/*
transition types available:
Wipe
Photo
Blinds
Iris
PixelDissolve
Zoom
*/
Flash AS3 Tweening
// Importing classes from fl package must be done explicitly
import fl.transitions.Tween;
import fl.transitions.easing.*;
// Declare variables for the tween movement. These could just as easily be local below.
var moveBackX:Tween;
var moveBackY:Tween;
var moveRound:Tween;
moveBackX = new Tween(dragger, "x", Strong.easeOut, dragger.x, holder.x, 0.5, true);
moveBackY = new Tween(dragger, "y", Strong.easeOut, dragger.y, holder.y, 0.5, true);
moveRound = new Tween(dragger, "rotation", Strong.easeIn, 0, 180, 0.5, true);
import fl.transitions.Tween;
import fl.transitions.easing.*;
// Declare variables for the tween movement. These could just as easily be local below.
var moveBackX:Tween;
var moveBackY:Tween;
var moveRound:Tween;
moveBackX = new Tween(dragger, "x", Strong.easeOut, dragger.x, holder.x, 0.5, true);
moveBackY = new Tween(dragger, "y", Strong.easeOut, dragger.y, holder.y, 0.5, true);
moveRound = new Tween(dragger, "rotation", Strong.easeIn, 0, 180, 0.5, true);
Tuesday, April 21, 2009
AS3 - Set frameRate at runtime
Framerate can now be set at runtime on per-VM instance basis (ie. it will affect all timelines, including loaded SWFs).
stage.frameRate = fps; //0.01 - 1000
stage.frameRate = fps; //0.01 - 1000
AS3 - Use getStackTrace for debugging
You can use the getStackTrace method of an Error instance for general debugging purposes. This will output the execution stack like so:
Test condition @Error
at Untitled_fla::MainTimeline/testingSomething()
at Untitled_fla::MainTimeline/MyThing_fla::frame1()
code:
function testingSomething():void {
if (testCondition) {
//include a stack trace with our debug trace:
trace("Test condition @" + (new Error()).getStackTrace());
}
}
Test condition @Error
at Untitled_fla::MainTimeline/testingSomething()
at Untitled_fla::MainTimeline/MyThing_fla::frame1()
code:
function testingSomething():void {
if (testCondition) {
//include a stack trace with our debug trace:
trace("Test condition @" + (new Error()).getStackTrace());
}
}
Depths in AS3
In AS3 it is easiest to work with depths in relative terms: Which element should be on top of the other?
Most people think this way anyway, the language reflects that now.
// get the depth of an existing sprite:
var depth:uint = getChildIndex(topMC);
//place mySprite under topMC:
addChildAt(mySprite, depth);
// or just:
addChildAt(mySprite, getChildIndex(topMC));
// directly set depth index:
setChildIndex(myMC, depth);
Most people think this way anyway, the language reflects that now.
// get the depth of an existing sprite:
var depth:uint = getChildIndex(topMC);
//place mySprite under topMC:
addChildAt(mySprite, depth);
// or just:
addChildAt(mySprite, getChildIndex(topMC));
// directly set depth index:
setChildIndex(myMC, depth);
AS3 New TextField methods
There are a number of new methods on text fields that allow you to work with text in more interactive manner, including methods to convert character indexes to pixel positions, find lengths of lines and paragraphs, get line metrics (ascent, descent, leading, etc), and more.
//px position of chars & vice versa:
myTF.getCharBoundaries(index)
myTF.getCharIndexAtPoint(x,y)
//and a TON more:
myTF.getParagraphLength(index)
myTF.getLineIndexOfChar(index)
//px position of chars & vice versa:
myTF.getCharBoundaries(index)
myTF.getCharIndexAtPoint(x,y)
//and a TON more:
myTF.getParagraphLength(index)
myTF.getLineIndexOfChar(index)
Flash - Double click event in AS3
AS3 adds a DOUBLE_CLICK event that uses the system setting for double click speed. Note that you will still get a click event before the double click event.
myButton.addEventListener(MouseEvent.DOUBLE_CLICK, handleDoubleClick);
myButton.doubleClickEnabled = true;
myButton.addEventListener(MouseEvent.DOUBLE_CLICK, handleDoubleClick);
myButton.doubleClickEnabled = true;
Flash - Create text link in AS3
HTML text fields no longer support "asfunction:" links. Instead they support "event:" links, which will generate a TextEvent.
myTextField.htmlText = "<a href="event:text">test</a>";
myTextField.addEventListener(TextEvent.LINK, handleLink);
function handleLink(evt:TextEvent):void {
trace(evt.text);
}
myTextField.htmlText = "<a href="event:text">test</a>";
myTextField.addEventListener(TextEvent.LINK, handleLink);
function handleLink(evt:TextEvent):void {
trace(evt.text);
}
Monday, February 16, 2009
getURL in as3
As we all know, there's no more getURL in as3. Below is the replacement of getURL,
navigateToURL(new URLRequest("http://google.com"), "_blank");
cheers!
navigateToURL(new URLRequest("http://google.com"), "_blank");
cheers!
How to set cookies in flash - as3 (Flash SharedObject)
the codes:
var sharedObj:SharedObject = SharedObject.getLocal("anyname");
sharedObj.data.object1 = "whateverYouWantToStore";
sharedObj.data.object2 = "whateverYouWantToStore";
sharedObj.flush();
sharedObj.close();
var sharedObj:SharedObject = SharedObject.getLocal("anyname");
sharedObj.data.object1 = "whateverYouWantToStore";
sharedObj.data.object2 = "whateverYouWantToStore";
sharedObj.flush();
sharedObj.close();
Monday, January 19, 2009
AS3 - How to call a function in loaded external swf
At the parent code:
at the child code:
package
{
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.Event;
import flash.net.URLRequest;
public class ExampleA extends Sprite
{
private var loader:Loader = new Loader();
public function ExampleA()
{
loader.contentLoaderInfo.addEventListener( Event.INIT, onLoaderInit );
loader.load( new URLRequest( "ExampleB.swf" ) );
}
private function onLoaderInit( e:Event ):void
{
Object( loader.content ).init( "hello" );
}
}
}
at the child code:
package
{
import flash.display.Sprite;
public class ExampleB extends Sprite
{
public function ExampleB()
{}
public function init( param:String ):void
{
trace( param ); // hello
}
}
}
Subscribe to:
Posts (Atom)