Archive for the ‘ActionScript’ Category

Unit Circle Demonstration

This demo is kinda fun so I thought I would share it. Below is the visualization of my Unit Circle class I constructed in Flash ActionScript 3. I have a great need in the delicious:designer to do trigonometric calculations to find points in rotated coordinate spaces. So, hearkening back to my high-school trig days I created a class that defines a unit circle with a few unique properties. Perhaps the most convenient feature is the ability to set an artificial zero. In effect this rotates the horizon, causing all data points in the unit circle to be calculated with this angle taken into consideration. It also allows for the radius to be set to any arbitrary value (so it is not technically a “unit” circle), but this makes distance calculations between points in a rotated space quite convenient.

The sample here shows all the data points available in the Unit Circle class represented as labeled dots. To learn more about what each of these actually mean visit this article at wikipedia.

Using the unit circle class is ever so easy. Simply initialize it with three pieces of information: the center point, the radius, and either an arbitrary angle or an arbitrary point (that will cause the angle to be calculated). In this example the unit circle is animated by using a timer to continually set the angle to an ever-increasing number.


You can interact with it by clicking and dragging your mouse in any of the white space outside of the main circle. When dragging your mouse you are setting the arbitrary point to the current mouse position. Use the slider on the bottom to set the artificial zero to any angle between 0 & 360.



The class is still in production so I am not releasing its code, but if there is enough interest I will release it when I have fleshed out its features and completely debugged it. Drop us a line if you’re interested in seeing the code.

Multiple–Key Combination Detection

I have the need to detect multiple-key combinations from the user in the delicious:designer. While porting the designer to ActionScript 3 I decided to refine this behavior and came across this post with an ActionScript 2 class for detecting multi-key combos. I ported this class to AS3, borrowed this other class to replace the missing Key.isDown() function in AS3, and wrote a custom KeyComboEvent event class to create a rather nice multi-key detection system that will allow for far greater flexibility than the current (build 38) key detector.This is all that is needed to create and listen for multiple-key combinations in ActionScript 3

import com.Keyboard.*;

import flash.events.*;// Key is used so we can test for a key being down, like we used to do in AS2Key.initialize(stage);

// Create your key detector and set up a few combinations to test for

var keyDet = new KeyDetection(stage);keyDet.addCombination(“undo”, Keyboard.CONTROL, 90);

// Control + ZkeyDet.addCombination(“redo”, Keyboard.CONTROL, 89);

// Control + YkeyDet.addCombination(“three keys”, Keyboard.CONTROL, Keyboard.SHIFT, 89);

// Control + Shift + Y

// Add a listener so we can respond to key combinations when they are pressed

keyDet.addEventListener(KeyComboEvent.KEY_COMBINATION, keyCombo);

function keyCombo(event:KeyComboEvent):void

{

    switch (event.label)

    {

        case “undo”:

 		trace(“Undo Combination Pressed”);

 		textArea.appendText(“Undo Combination Pressedn”);

 		break;

 	case “redo”:

 		trace(“Redo Combination Pressed”);

 		textArea.appendText(“Redo Combination Pressedn”);

 		break;

 	case “three keys”:

 		trace(“Our three-key Combination was Pressed”);

 		textArea.appendText(“Our three-key Combination Pressedn”);

 		break;

 	}

 }

}

Click here to download the Flash 9 sample with source code, and click here to view the package documentation.

ActionScript 2 double interval

Within the delicious:suite I often have the need of calling a function at an interval. But I need a two step timer to call that function at a slower interval the first few times, and then call it at a faster interval.

I wrote a custom class to mimic the Flash ActionScript 2 built in setInterval() and clearInterval() functions, but with the addition of this two-step timer. The sample swf demoed here shows my stepper that increments a number up and down. When you press and hold either arrow button the number increments slowly for the first 10, and then increments faster until you release the button. Another example of this class in action is the nudge function in the delicious:designer — the user can nudge art objects using the arrow keys. The first nudge occurs at a long interval, and then, if the user continues holding the key down, the nudge continues at a faster interval.Here is sample ActionScript 2 code showing how simple it can be to create a double interval function call. This example will call the function myFunction 10 times every 200 milliseconds. Then it will call the function myFunction every 50 milliseconds until the user clears it.

import mx.utils.Delegate;
import delicious.doubleInterval;
intervalNumber = doubleInterval.setDoubleInterval( Delegate.create( this, myFunction ), 200, 10, 50);


For more advanced uses you can also pass parameters to the function called on the interval. The class can be downloaded here, and the documentation can be viewed here.