gridBox.addEventListener(ListEvent.ITEM_CLICK, ItemClicked); function ItemClicked(e:ListEvent){ //heres the bit you want var o:Object = e.item; trace(o.Name); trace(o.Age);
Listing object properties / names
If you need to get the property names from an object: var object:Object = {name:"lawrence", age:"26"}; for( var o : * in object){ trace( o + " = " + object[o] ); } source: http://stackoverflow.com/questions/372317/how-can-i-get-list-of-properties-in-an-object-in-actionscript
TweenMax tween curves
import com.greensock.TweenMax; import com.greensock.plugins.BezierPlugin; import com.greensock.plugins.BezierThroughPlugin; TweenMax.to(sp,5,{bezierThrough:[{x:250,y:100},{x:50,y:200},{x:500,y:200}]}); Man TweenLite/TweenMax is awesome.
Stop stage rescalling AS3
stage.scaleMode = StageScaleMode.NO_SCALE; stage.align = StageAlign.TOP_LEFT;
if else questionmark operators
A neat wee time saving way to write an if () {} else {} statement in flash or javascript: var a = true; var b = false; var c; if(a==b?c = true: c = false); trace(c); The statements between the ? and the : are if the if evaluated to true, and after the :... Continue Reading →
Call AS3 functions from javascript and vice versa
To communicate between JavaScript and Actionscript it isn't so hard. FLASH: import flash.external.ExternalInterface; ExternalInterface.addCallback("sendTextToFlash", getTextFromJavaScript); function getTextFromJavaScript(str):void { trace(str); } JAVASCRIPT: var currentPage="Home"; function setCurrentPage(newPage) { currentPage = newPage; SendDataToFlashMovie(newPage); } So then on the page, this code is run when an item is clicked (javascript function sending to flash) <a href="javascript:void(0);" onClick="setCurrentPage('Home')">Home</a> This javascript... Continue Reading →
Radians to Degrees, Degrees to radians, AS3
// Degrees to Radians radians = degrees * Math.PI / 180 // Radians to Degrees degree = radians * 180 / Math.PI
Adding inline function and removing it AS3
Saves you making real functions for tiny little functions - handy. timer.addEventListener(TimerEvent.TIMER, function(e:TimerEvent){trace("inline function")}); To remove it, it can only be done when it is called (see the dudes link below for the source for this!) myButton_btn.addEventListener(MouseEvent.CLICK, function(e:Event) { trace(e.currentTarget.name+" was just clicked!"); //removing anonymous listener e.currentTarget.removeEventListener(e.type, arguments.callee); }); src: (ty!): http://sierakowski.eu/list-of-tips/61-using-an-anonymous-inline-function-when-adding-an-event-listener.html
Turning a clip to a point AS3
Use Math.atan2. So handy but forgetable. var targetAngle:Number = Math.atan2(mouseY - this.y, mouseX - this.x); var angle:Number = targetAngle * (180/Math.PI) - this.rotation = angle;
Saving files with and without prompts using AIR AS3
Couple of bits of code for saving files using AIR: This way does not open the save dialogue, just saves it automatically. var stream:FileStream = new FileStream(); var saveFile:File = new File(); saveFile = saveFile.resolvePath(String(courseStructureURL)); var fileStream:FileStream = new FileStream(); fileStream.openAsync(saveFile, FileMode.WRITE); fileStream.writeUTFBytes(String(Structure.xml)); fileStream.addEventListener(Event.CLOSE, fileClosed); fileStream.close(); function fileClosed(e:Event):void { Utils.alert("The course has been updated successfully.");... Continue Reading →
Stopping an AIR app window closing with Flash IDE AS3
If you're doing document manipulation or something you might need to confirm with the user of the app that they want to quit the app before letting them quit. To do this, there's plenty of help online if using Flex, but if writing AIR using the Flash IDE, it's slightly different. 1. Import the NativeWindow... Continue Reading →
Cancel bubbling of an event
If you've set 'cancelable' to true in your event dispatch call, you can call e.stopPropagation(); To stop the event in its bubbling track at the first listener. Good if you've got a CLOSE event for example, and want the first parent who can handle closing things to close it rather than higher great-grandaddy clips.