This blog has moved to a new location! http://iqandreas.github.com/
Showing posts with label functions. Show all posts
Showing posts with label functions. Show all posts

Thursday, September 17, 2009

Most common Flash Questions (AS3 FAQ)

When you help out beginners as much as possible each day, you tend to hear the same questions again and again (and again, and again).

Ignoring the fact that when I was little, I used to think FAQ was a bad word (not joking), I have here compiled a few links for threads with the most frequently asked questions in the Kirupa.com ActionScript 3.0 Forums.


TypeError: Error #1009: Cannot access a property or method of a null object reference.
Hands down, with no doubt possible in anyone's mind the absolute #1 error. There are 2 possible causes:


Duplicating a MovieClip (or at least creating a new version of whatever you pass in)
http://www.kirupa.com/forum/showpost...80&postcount=3
http://www.kirupa.com/forum/showthread.php?t=333120
http://www.kirupa.com/forum/showthread.php?t=334933 - Didn't realize the "constructor" property was so vulnerable. Nice discussion.


The difference between instance name and library name
http://www.kirupa.com/forum/showthread.php?t=333041


The Dictionary Class (Passing in custom variables to Events)
Personally, I really like using it since it allows you to pass in other variables to events such as telling which URLs go to which buttons, so you can use one function for the onClick event.
http://www.kirupa.com/forum/showthread.php?p=2484903
http://www.kirupa.com/forum/showthread.php?t=331118
http://www.kirupa.com/forum/showthread.php?p=2501990 - Some sample usage


Wow! How can I learn to be a great coder like you?
First of all, I'm good with Logic, but sadly I often use Flash instead, which is often similar, just uglier and meaner.
http://www.kirupa.com/forum/showpost...51&postcount=6 - Some great reading
http://www.kirupa.com/forum/showpost...85&postcount=2 - The Flash and PHP Bible


The advantage of using static properties and functions
I need to dig out an old MSN messenger discussion on this as well. It's on my rediculously long todo list.
http://www.kirupa.com/forum/showthread.php?t=334492


The strange way frames work in Flash
http://www.kirupa.com/forum/showthread.php?t=325891 - How frames really work. Might need some updating as this was posted early on when I was first learning Flash.
http://www.kirupa.com/forum/showthread.php?t=325884 - This guy still owes me a beer


There are plenty more, I just want to avoid digging through all 600+ of my forum posts for the gold nuggets. If anyone finds any more (by me, or anyone else for that matter) comment.

Thursday, August 13, 2009

TIP: Tweening Functions

I often get annoyed by the fact that most tweening engines do not allow the tweening of functions.

Here is how you work around that!

This example will be using TweenLite, but if anyone wants me to write similar code for other Tweening engines (such as GTween or Tweener), just ask.


Here I will be tweening the graphics.lineTo() function to draw a simple line from the top left corner of the stage down to the bottom right, straight through the center of the movie, but tweened!

The key is creating a new object, and then applying the needed properties of that object to the function you want to run.

ActionScript Code:
import gs.TweenLite;

//This sprite is what we will draw the lines on, insead of drawing them directly onto the stage.
var lineSprite:Sprite = new Sprite();
this.addChild(lineSprite);

//The key is to create a new object, and put tweenable properties on it
var drawObj:Object = new Object();
drawObj.targetObject = lineSprite;
drawObj.x = 0;
drawObj.y = 0;
drawObj.endX = this.stage.stageWidth;
drawObj.endY = this.stage.stageHeight;

TweenLite.to(drawObj, 5, { x:drawObj.endX, y:drawObj.endY,
onStart:startDraw, onStartParams:[drawObj],
onUpdate:updateDraw, onUpdateParams:[drawObj] } );

function startDraw(obj:Object):void
{
obj.targetObject.graphics.lineStyle(5, 0x000000);
obj.targetObject.graphics.moveTo(obj.x, obj.y);
}

function updateDraw(obj:Object):void
{
//Here is where all the action happens.
//This code is executed each and every time the tweening engine changes the value of x or y
//Here is where you place the function you want to tween, and use whatever parameters are needed
obj.targetObject.graphics.lineTo(obj.x, obj.y);
}



That code can also be abbreviated down a lot. This code is less clear to read, but will run faster. Also, instead of creating a new function that will call the function to be tweened, the lineTo() function will be called directly.
ActionScript Code:
import gs.TweenLite;

//This sprite is what we will draw the lines on, insead of drawing them directly onto the stage.
var lineSprite:Sprite = new Sprite();
this.addChild(lineSprite);

//The key is to create a new object, and put tweenable properties on it
var drawObj:Object = {targetObject:lineSprite, x:0, y:0, endX:this.stage.stageWidth, endY:this.stage.stageHeight};

//Prepare the sprite for drawing
//This code was originally in a startDraw function
drawObj.targetObject.graphics.lineStyle(5, 0x000000);
drawObj.targetObject.graphics.moveTo(obj.x, obj.y);

TweenLite.to(drawObj, 5, { x:drawObj.endX, y:drawObj.endY,
onUpdate:obj.targetObject.graphics.lineTo,
onUpdateParams:[drawObj.x, drawObj.y]