Archive for November, 2007

Tweener Tips

We’ve been using Tweener here for a few of our examples and while looking for info on Bezier Movement like the old MC_Tween used to support I came across one of Tweener’s co-creator’s blogs: labs.zeh.com.br
It has a really good post about how he decided on the syntax for the bezier Tween. It also includes a terrific example that applies it to a Papervision3D camera object.

Also:

  • For those not already familiar with Tweener there are some decent flas here.
  • And some pretty neat tricks (especially tip 3) for the more experienced Tweenerers here.

Happy Tweenering.

E4X & XMLList fails when defined in a switch statement.

This one had me REALLY confused, for a good bit of time, luckily a co-worker helped be go through a rather vigorous debugging session to pinpoint the problem. Apparently if you are attempting to access a piece of a larger XML object using the new E4X capabilities and you attempt to do it with a single line of code, it will return a null object instead of the expected XMLList object it should. After looking into it further, I discovered that if you define the variable, but don’t set it’s value on the same line, then it works fine… See wonky huh?

Example (Based on Adobes example documentation):

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package
{
import flash.display.Sprite;
 
public class XMLListExample extends Sprite
{
	private var books:XML;
 
	public function XMLListExample()
	{
		books =		<books>
			<book publisher="Addison-Wesley" name="Design Patterns" />
			<book publisher="Addison-Wesley" name="The Pragmatic Programmer" />
			<book publisher="Addison-Wesley" name="Test Driven Development" />
			<book publisher="Addison-Wesley" name="Refactoring to Patterns" />
			<book publisher="O'Reilly Media" name="The Cathedral & the Bazaar" />
			<book publisher="O'Reilly Media" name="Unit Test Frameworks" />
		</books>;
 
		showBooksByPublisher();
	}
 
	private function showBooksByPublisher():void
	{
		var ner = "ren";
		switch(ner){
			case "ren":
			// the working code
			var results:XMLList;// = new XMLList();
			results = books.book.(@publisher == "Addison-Wesley");
			trace("results:\n"+results); // returns a the first 4 items (which contain "Adison...")
 
			// now for the fun!!!
			var otherResults:XMLList = books.book.(@publisher == "O'Reilly Media");
			trace("other results:\n"+otherResults); // returns null? (actually a null object)
 
			// just to see if oneline variable definitions work
			var test:String = 'ner';
			trace("test string: "+test);
 
			// seeing what else is a problem to do this way
			var objTest:Object = {ner:"ren"};
			trace('test object: '+objTest.ner); // yup works fine.
			//
			var xmlTest:XML = <ners>
			<ner>ren</ner>
			</ners>;
			trace('test xml:\n'+xmlTest); // no problem here either.
			//
			var numTest:Number = 45;
			trace('test number: '+numTest); // this is fine too.
			break;
		}
		if(ner == "ren")
		{
			var otherOtherResults:XMLList = books.book.(@publisher == "O'Reilly Media");
			trace("other other results:\n"+otherOtherResults); // works fine as well.
		}
	}
}// end of class
}

Download the code.

Tell your friends, I’m letting Adobe know. And please let us know if your testing shows differently.

As3 event management

Recently while working on an as3 project the project manager of the project came over to me and asked that something happen automatically that was currently happening only on a mouse click. Normally I would have said fuck you that is impossible and get the hell out of my face. I was feeling confident today though with the event model of as3 so I said yeah I think we can make that happen. The logic was that at worst I would have to make a separate function based on the one I already had. So in a quest to solve this problem I offer you the most awesome kick ass thing you could ask for.

With as3 you quite literally fake an event. And you can do so very easily. All you need to do is import the events classes:

?View Code ACTIONSCRIPT
1
import flash.events.*;

Then any time you have a button you can fake whatever event you want. You can fake all sorts of events not just button. But faking a click is often the most useful. The code for doing such a thing is as follows.

?View Code ACTIONSCRIPT
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// note you must make a symbol and link it in order to make an instance.
function init(){
	mybutton = new customSymbolIHaveLinked();
	mybutton.mouseChildren = false;
	mybutton.mouseEnabled = true;
 
	// this makes the button listen for the click event
	mybutton.addEventListener(MouseEvent.CLICK, functiontorun);
	// now if you would like to automatially run this event on start up to perhaps ensure the user is at a default
	// position you simply need to dispatch an event
 
	mybutton.dispatchEvent(new Event(MouseEvent.CLICK));
}
function functiontorun(evt:Event){
	trace("recieved event from "+evt.target);
}

That’s all there is to it really. I hope this has been helpful. It has been a lifesaver for me. Thanks again as3 for making life a hell of a lot easier…