By Zevan | December 21, 2008
Actionscript:
-
mc.addEventListener(MouseEvent.ROLL_OVER, onRollOver);
-
function onRollOver(evt:MouseEvent):void {
-
addChild(MovieClip(evt.currentTarget));
-
}
This one is very simple, but it's important to note that using addChild() on something that is already on the display list simply brings it to the top. Back in AS2 we used to do:
By Zevan | December 20, 2008
Actionscript:
-
var currentBtn:MovieClip;
-
-
var nav:Sprite = new Sprite();
-
nav.x = nav.y = 20;
-
addChild(nav);
-
-
createBtns();
-
-
nav.addEventListener(MouseEvent.CLICK, onClickBtn);
-
-
function createBtns():void{
-
for (var i:int = 0; i<10; i++){
-
var btn:MovieClip = new MovieClip();
-
with(btn.graphics) beginFill(0x666666), drawRect(-10,-10,20,20);
-
btn.x = i * (btn.width + 10);
-
btn.buttonMode = true;
-
btn.num = i;
-
btn.alpha = .5;
-
nav.addChild(btn);
-
}
-
}
-
-
function onClickBtn(evt:MouseEvent):void {
-
//
-
// this is the important part
-
//
-
if (currentBtn){
-
currentBtn.scaleX = currentBtn.scaleY = 1;
-
currentBtn.alpha = .5;
-
}
-
currentBtn = MovieClip(evt.target);
-
currentBtn.scaleX = currentBtn.scaleY = 1.3;
-
currentBtn.alpha = 1;
-
trace("current button:", currentBtn.num);
-
//
-
}
This code will create 10 boxes that represent buttons on a navigation. When you click a box it indicates that it is selected by scaling up and changing alpha. Simple enough...
The term Procedural Pattern is just a spin on the idea of Design Patterns. I've come up with lots of small patterns to solve simple recurring problems over the years. Most of these relate to things like drawing programs, ecards and games.
By Zevan | December 19, 2008
Actionscript:
-
var file:FileReference = new FileReference();
-
-
stage.addEventListener(MouseEvent.CLICK, onClick);
-
-
function onClick(evt:MouseEvent):void {
-
file.save("some text. \nsome more text", "actionsnippet.txt");
-
}
This is possibly my favorite feature of flash 10. Save any kind of file to the users computer...
The first argument is for the data to put in the file, this can be a String, ByteArray or XML object. The second argument is the name of the file.
As a test I also created one that saved a BitmapData object as a jpeg. But it doesn't really fall into the category of snippet because it uses the Jpeg Encoder from adobe.
Posted in misc | Tagged actionscript, flash |
By Zevan | December 18, 2008
Actionscript:
-
addEventListener(Event.ENTER_FRAME, onLoop);
-
-
function onLoop(evt:Event):void {
-
var boolA:Boolean = false;
-
var boolB:Boolean = false;
-
-
if (mouseX> 200){
-
boolA = true;
-
}else{
-
boolA = false;
-
}
-
-
if (mouseY <200){
-
boolB = true
-
}else{
-
boolB = false;
-
}
-
-
if (boolA || boolB){
-
trace("regular or");
-
}
-
-
// this is the XOR conditional
-
if (int(boolA) ^ int(boolB)){
-
trace("exclusive or");
-
}
-
-
}
There's an obvious more logical ways to do this. But I thought it was somewhat interesting to see....
So in binary XOR will give us:
0^0 = 0;
0^1 = 1;
1^0 = 1;
1^1 = 0;
What other operator might you use to get the same results?... that's right !=
With regular OR we get:
0|0 = 0;
0|1 = 1;
1|0 = 1;
1|1 = 1;