contact

Posts Tagged ‘AS3’

adding Mac OS X like Application Behaviour to your Air Application

Tuesday, March 31st, 2009

In Mac OS X when you close the window of an application the app usually keeps running in the dock. Wheter this is a good thing or not is totally different story, but for apps like mail and ical it certainly is since I want my mail app to keep checking for new mail without the window cluttering up my screen. So what if I want to add such behaviour to an Adobe Air Application.

 	var appIsPersistant:Boolean=false
 	var app=NativeApplication.nativeApplication
 	var window=stage.nativeWindow;

public function set appPersistant(b:Boolean):void{ // adding a simple property to your app b:Boolean whether its stays on or not
 appIsPersistant=b
 	if(b){
 		if(app!=null){
 			app.autoExit=false // preventing the app to exit if all windwows are closed
 			window.addEventListener(Event.CLOSING,hideWindow) // listening for the closing event of the main window
 			app.addEventListener(InvokeEvent.INVOKE,initWindow) // listening for the invoke event if the user clicks on the dock icon
 			app.addEventListener(Event.EXITING,appClose) // listening if the app should be terminated, to prevent the hiding of the window and allow termination
 		}
 	}else{
 		if(app!=null){
 		app.autoExit=true // if app is not persistant the app can close just as usual
 		}
 	}
 }
 public function appClose(e:Event):void{
 	window.removeEventListener(Event.CLOSING,hideWindow) // if the app should terminate with command + Q we have to remove the listener for the Closing event. Otherwise only the window will close but the app will not terminate
 }
 public function hideWindow(e:Event):void{
 	if(window.visible){ // if the window is visible the event behaviour is canceld and the window is hidden
 		e.preventDefault()
 		window.visible=false
 	}
 }
 function initWindow(e:Event=null):void{
 	window.activate() // if the user clicks on the dock icon the window commes back up
 }
 public function get appPersistant():Boolean{
 	return appIsPersistant // just the getter for the appPersistant propery
 }

by adding these few methods your app will exit just fine with command + Q but if you click the close button only the window will close and the app keeps running and comes back up if you click the dock icon

onReleaseOutside in Actionscript 3

Thursday, January 29th, 2009

actionscript 3 does not nativly support a onReleaseOutside Event. When creating things like a scrollBar or other UI elements this can be quite annoying.

Here is a simple work around that uses a simple movieClip as a button:

import flash.display.MovieClip
 import flash.events.*

var myBtn:MovieClip=new MovieClip()
 myBtn.graphics.beginFill(0x003366,1)
 myBtn.graphics.drawRect(0,0,50,50)
 addChild(myBtn)
 myBtn.addEventListener(MouseEvent.MOUSE_DOWN,downHandler)

function downHandler(event:MouseEvent):void{
 stage.addEventListener(MouseEvent.MOUSE_UP,upHandler)
 trace("mouse went down")
 }
 function upHandler(e:MouseEvent):void{
 if(e.target==stage){ // this checks to see if the event is coming from the stage or not
 stage.removeEventListener(MouseEvent.MOUSE_UP,upHandler)
 trace("mouse went up outside")
 }else{
 trace("mouse went up")
 }
 }

pretty simple :-)

heres the fla for download

Actionscript 3 3D with Flash CS4

Wednesday, November 26th, 2008

I finally got a copy of adobe flash cs4. In addition to the great new user interface flash cs4 can compile flashplayer 10 content which enables the new actionscript 3 3D methods. with only the three simply rotationX, rotationY and rotationZ properties one can quickly create really nice 3D animations. This is my first test.

here are the few lines of code it took to make this thing spin:

</pre>
import flash.display.*
 import flash.geom.*
 import flash.events.*
 import flash.display.*
 var panes:MovieClip=new MovieClip()
 panes.x=300
 panes.y=200

for(var i:Number=0;i<18;i++){
 var pane:MovieClip=new MovieClip()
 pane.graphics.lineStyle(1,0xffffff,0.5)
 pane.graphics.beginFill(0x003366,0.3)
 pane.graphics.drawRect(0,0,100,100)

pane.x=0
 pane.y=-50

pane.rotationY=i*20
 pane.moving=false
 panes.addChild(pane)

}

addChild(panes)
 panes.rotationZ=90
 panes.cacheAsBitmap=true
 panes.addEventListener(Event.ENTER_FRAME,movePanes)

function movePanes(e:Event){
 panes.rotationX+=5
 }
<pre>

migrating from Actionscript 2 to Actionscript 3

Thursday, January 31st, 2008

 I just recently bought the book Essential Actionscript 3  by Collin Mook. I havent gotten really far into the book yet but there are few things that one must understand in order to get even started with actionscript 3. The first thing is AS 3 is pretty much OOP only, which means for you to forget about timeline code which i personally used very often especially for small projects. So where do I put my code now, was the first thing I asked my self. The answer to this question is the document class. When using the flash authoring tool you find this setting in the document preferences.

 

so none of your code will go to the timeline. You will need to follow a few simple steps:

  • create an actionscript file, save it to the same folder where your fla file is at
  • inside this file you have to create a package that will hold the document class
  • inside the package create the document class
  • place the code you would have originally put into the first frame of the timeline in the constructor function of the document class the following code:
  • then place the name of your document class in the designated field in the document preferences as shown above
  • you may use this sample code to start from:
package{
	import flash.display.Sprite //
	public class yourClass extends Sprite{
		public function yourClass(){
		// your first timeline frame code goes here
		}
	}
}

Now this is a very simplified approach to this topic, there is a lot to know about the naming of packages, classes and the scope of objects and methods within them. I highly recommend the book i mentioned above. just note that the document class must either extend the sprite or the movieclip class, and you have to import every class you want to use. In general Actionscript 3 is a lot more strict but thereby a lot more effective. It forces good OOP programing, and won’t let you get by with several hundred lines of code in you first frame.