package com.mindfock.blog {

Shuffler class

Posted: June 3rd, 2008 | Author: John del Rosario | Filed under: AS 3.0, Flash | Tags: , , , | 1 Comment »

I’ve been working on my site for a few weeks now, and I have developed a class that will handle how I display assets on my site.

Basically, it “throws” in items into the stage, and you could shuffle them around or you could stack them up. I’ve made it very easy to use, and it’s still under development, but I think it’s ready to be used by others. This is my first class I’ve ever shared, and though it’s not much, I hope someone likes it or maybe even use it. ;-)

You can use any DisplayObject as the ‘items’ of the Shuffler.
There are explanations in the code, but here are some important points

  • shuffle() – shuffles the items
  • stack() – stacks the items
  • setFocusOn(item:DisplayObject) – sets the item param as the focused item
  • addItem(item:DisplayObject) – adds item param to the shuffler
  • deleteItem(index:int) – deletes the item at index
  • has max property for maximum number of boxes allowed. 0 for infinite.

    Here is an example:

    Sample usage in code:

    package{
    	import flash.display.DisplayObject;
    	import flash.display.Sprite;
    	import flash.display.StageAlign;
    	import flash.display.StageScaleMode;
    	import flash.events.*;
    	import flash.text.TextField;
    	import com.mindfock.ui.Shuffler;
    
    	public class TestShuffler extends Sprite{
    		private var boxes:Array;
    		private var shuffler:Shuffler;
    		private var stacker:TextField;
    		private var shuffle:TextField;
    		private var adder:TextField;
    
    		public function TestShuffler(){
    			init();
    		}
    		public function init():void{
    			shuffler = new Shuffler(400, 300, 6);
    			createBoxes(6);
    			createButtons();
    			addChild(shuffler);
    		}
    		private function createBoxes(num:int):void{
    			boxes = new Array();
    			for (var i:int = 0; i < num; i++)
    			{
    				trace("created");
    				var box:Sprite = drawBox(50, 50) as Sprite;
    
    				boxes.push(box);
    
    				shuffler.addItem(box);
    			}
    		}
    		private function drawBox(w:Number, h:Number):DisplayObject{
    			var box:Sprite = new Sprite();
    			box.graphics.lineStyle(2, 0x000000);
    			box.graphics.beginFill(Math.random() * 16000000);
    			box.graphics.drawRect(0, 0, 100, 100);
    			box.graphics.endFill();
    
    			return box;
    		}
    		private function createButtons():void
    		{
    			// I've left out the drawing of the buttons
    			square.addEventListener(MouseEvent.CLICK, shuffleHandler);
    			square2.addEventListener(MouseEvent.CLICK, stackHandler);
    			square3.addEventListener(MouseEvent.CLICK, addHandler);
    		}
    		private function shuffleHandler(event:MouseEvent):void{
    			shuffler.shuffle();
    		}
    		private function stackHandler(event:MouseEvent):void{
    			shuffler.stack();
    		}
    		private function addHandler(event:MouseEvent):void{
    			var newBox:Sprite = drawBox(50, 50) as Sprite;
    			shuffler.addItem(newBox);
    		}
    	}
    }
    

    Download source

    Just save it in your classpath, under com/mindfock/ui

    You need TweenLite for the tweening animations… If you don’t know what it is, you should look into tweening engines. They will save you a lot of hard work.

    Now that I’ve got the main bulk of my site done, I hope I can finish it before classes start. :D


    Flash Player 10 (beta) available at Adobe Labs

    Posted: May 16th, 2008 | Author: John del Rosario | Filed under: Flash | Tags: | No Comments »

    Adobe® Flash® Player 10, code-named “Astro,” introduces new expressive features and visualAstro performance improvements that allow interactive designers and developers to build the richest and most immersive Web experiences. These new capabilities also empower the community to extend Flash Player and to take creativity and interactivity to a new level.

    Wow, this is great news. The features list is very impressive. I am especially excited GPU acceleration support. That means greatly improved performance since on previous versions, everything was handled by the CPU alone.

    • 3D Effects – Easily transform and animate any display object through 3D space while retaining full interactivity. Fast, lightweight, and native 3D effects make motion that was previously reserved for expert users available to everyone. Complex effects are simple with APIs that extend what you already know.
    • Custom Filters and Effects – Create your own portable filters, blend modes, and fills using Adobe® Pixel Bender™, the same technology used for many After Effects CS3 filters. Shaders in Flash Player are about 1KB and can be scripted and animated at runtime.
    • Advanced Text Layout – A new, highly flexible text layout engine, co-existing with TextField, enables innovation in creating new text controls by providing low-level access to text offering right-to-left and vertical text layout, plus support for typographic elements like ligatures.
    • Enhanced Drawing API – Runtime drawing is easier and more powerful with re-styleable properties, 3D APIs, and a new way of drawing sophisticated shapes without having to code them line by line.
    • Visual Performance Improvements – Applications and videos will run smoother and faster with expanded use of hardware acceleration. By moving several visual processing tasks to the video card, the CPU is free to do more.

    And this of course means that Flash CS4/10 will soon follow. And that’s what I will be waiting for like a child on Christmas Eve. :D

    http://labs.adobe.com/technologies/flashplayer10/

    Demos. The Flash demo is not much, but the videos at the bottom of the page are well worth a watch. You need to download and install the beta player, and I have no idea how stable it is.


      The Making of an O’Reilly Animal

      Posted: May 3rd, 2008 | Author: John del Rosario | Filed under: Art and Design, Random | Tags: , , , | No Comments »

      pelican

      I’ll be out for a few days on vacation again, so here’s something that might keep things interesting around here.

      Have you ever wondered how those O’Reilly books got their covers? How did they choose the animal for a specific book? Why did they even choose animals in the first place?

      Their website posted an article answering those questions, with a step-by-step image on creating one of the animals (a pelican).

      Read the article.


      May 1st Rebootion

      Posted: May 2nd, 2008 | Author: John del Rosario | Filed under: Art and Design, Random, Web, Web Design | Tags: , , | No Comments »

      May 1st just went by, and all the participants now have their rebooted sites up. There must be at least a hundred participants for this. I registered on the site last month, just for kicks, but I never really got around to making my site.

      Head on over to the site and have a look at all the new sites that went up at the same time, and cast your votes for your favorite.

      http://www.may1reboot.com/2008/


      [Learn] AS2 to AS3 Basic Liquid Layout

      Posted: April 23rd, 2008 | Author: John del Rosario | Filed under: AS 3.0, Flash, Learn | Tags: , , , , , | 11 Comments »

      I was just getting the hang of creating liquid layouts in AS2.0 a few months ago, and boy, is it quite a concept to grasp. Now I have to relearn everything I learned using AS3.0.Thankfully, once I understand the concept, all I have to do now is do it with unfamiliar syntax.

      In this little tutorial, I’ll show AS2 and AS3 code showing the same end result. This is just very basic, just showing the differences between the two (which isn’t much), and to show how to get started with creating liquid layouts.

      By the way, liquid layouts are layouts that change dynamically according the stage’s size. Not unlike liquid in a container, which takes the container’s size and shape, thus the name. ;-)

      The movie shows 4 boxes I drew on four sides of the stage, changing position according to the stage’s size. Obviously, you could also adjust other properties of the MovieClip, like scale, alpha, rotation, etc.

      First, the AS2.0 code:

      Stage.scaleMode = “noScale”;
      Stage.align = “TL”;
      
      setStage();
      
      var stageListener:Object = new Object();
      
      Stage.addListener(stageListener);
      stageListener.onResize = function() {
      setStage();
      };
      
      function setStage() {
      var WIDTH:Number = Stage.width;
      var HEIGHT:Number = Stage.height;
      
      box1._x = WIDTH-box1._width;
      box1._y = HEIGHT/2-(box1._height/2);
      box2._x = WIDTH/2 - (box2._width/2);
      box2._y = 0;
      box3._x = 0;
      box3._y = box1._y;
      box4._x = box2._x;
      box4._y = HEIGHT - box4._height;
      }

      AS3.0:

      stage.scaleMode = StageScaleMode.NO_SCALE;
      stage.align = StageAlign.TOP_LEFT;
      
      setStage();
      
      stage.addEventListener(Event.RESIZE, stageResize);
      
      function setStage():void{
      var WIDTH:Number = stage.stageWidth;
      var HEIGHT:Number = stage.stageHeight;
      box1.x = WIDTH - box1.width;
      box1.y = HEIGHT/2 - (box1.height/2);
      box2.x = WIDTH / 2 - (box2.width/2);
      box2.y = 0;
      box3.x = 0;
      box3.y = box1.y;
      box4.x = box2.x;
      box4.y = HEIGHT - (box4.height);
      }
      
      function stageResize(e:Event):void{
      setStage();
      }
      

      So, there isn’t really much difference between the two. AS3 is easier to understand because of its better event handling methods.

      A little explanation for the 2 codes above, since they are quite similar. The first 2 lines are very important. Basically, they are telling the player to not do anything to objects on the stage when the stage is resized, because we want to control everything that happens when the stage is resized by code. Without those lines, the movie won’t work as expected.

      The setStage() function is pretty self explanatory, it is where we do all the positioning of the movieclips. I like to put all of the code that does the positioning, resizing, etc. work in a function (preferably rolled into one), so I could call it at least once anytime I want. This is useful when initializing the movie, where all the movieclips might not be in their right place. And it is easier to put into the listener function too. You could also do it in the listener function, but I don’t recommend it.

      We then call the setStage() function at least once, to position our movieclips where they should be, because when I created them in Flash, they are pretty much all over the place. If we don’t call that function, then they’ll only fall into place when we start resizing the stage.

      And now to the part where the two codes are most different. Adding listeners to the stage. In AS3, it is quite straightforward. We add a listener, in this case stageResize() and listen to RESIZE events, to the stage object, which does nothing but call setStage(). You could also add other functions in there if you want to do something else when the stage resizes. In AS2, we have to create a listener object, tell that object to listen for the resizing event and execute the setStage() function. Again, you could add other functions in there.

      So, there you have it. A basic Flash liquid layout. I hope this helps anyone who is having a hard time understanding this concept (I know I did). Now, this tutorial only showed changing the position of movieclips, by assigning values to them. But you could also change their position and/or size by percentages. Like 30% of the stage (I know this tutorial shows applying 50% of the stage), so you could adjust the visual elements of your movie to fit any size of resolution properly, at a limit of course.

      NOTE: Notice the capital ‘S’ in Stage in the AS2 code and the small ‘s’ in the AS3 code. Don’t mess those up!

      Here are the source files of this tutorial.

      AS2 source (Flash 8 )

      AS3 source (Flash CS3)


      FontStruct

      Posted: April 15th, 2008 | Author: John del Rosario | Filed under: Art and Design, Computer Graphics, Random, Web, Web Design | Tags: , , | No Comments »

      Here’s a cool site. It’s main feature is an RIA that allows you to create fonts and share it with the community. That’s right, creating fonts online for free. I am so impressed with what people can do with the web these days.
      You could create any font you could imagine. From simple pixel fonts, to dingbat style fonts.
      The gallery is very impressive. There’s some really nice fonts there, all created with FontStruct and free to download and use. You could even “clone” someone else’s work. To download a font, click on the font name or double-click on the preview.
      FontStruct gallery
      The application itself is a work of art. Its interface is very intuitive, its tools adequate, and it’s just so comfortable playing/working with it. You could save your work, it even has hotkeys for undo, redo, tool selection, etc. Although you are limited to the “bricks” provided, they are more than enough to create a wide variety of fonts.
      Go to http://fontstruct.fontshop.com/ to get started in creating your own fonts. You need to register for free though to use the fontstructor and download fonts.


      [Learn] AS2 to AS3 EnterFrame events

      Posted: April 14th, 2008 | Author: John del Rosario | Filed under: AS 3.0, Flash, Learn | Tags: , , , , , | 4 Comments »

      NOTE: This blog post is inaccurate. I am sorry for the error. I have fixed the error following the comments posted.

      So in this blog, I’ll be posting things I learn each day (hopefully), about programming, life, etc. The things I post here might not be completely correct or true, but they are what I learned and I post them as I understand them. Corrections would be very much appreciated.
      I’ll call it the [Learn] series. :P

      NOTICE: I am telling this as how I understand it from reading other tutorials on the web. This is possibly very inaccurate, but maybe you could understand things you haven’t understood before by reading how I understand it. :-/

      Event handling in AS3.0 is very different from AS2.0. One of my most used events is onEnterFrame events.

      In AS2.0, animating a simple box to move to the right every frame would look like this:
      (please bear with me and the unformatted code)

      box.onEnterFrame = function(){
      this._x += 1;  //execute this code everytime the box movieclip enters a frame.
      }

      but in AS3.0, it is a little longer, and I may still have to see the real benefits of this. But already, I am liking how easy it is to understand.

      function moveBox(e:Event):void{
      e.target.x += 1;
      }
      box.addEventListener(Event.ENTER_FRAME, moveBox);

      the addEventListener method tells the box MovieClip to execute the function moveBox, everytime an ENTER_FRAME event is generated(which is all the time).

      Notice the moveBox function has an Event parameter. When a function has an Event type parameter, it becomes a listener function. Passing a non-listener function to the addEventListener method causes an error. You could also add the moveBox listener to any other MovieClip you might have on stage, already one of the benefits of the new event handling of AS3.0.

      There are a lot more events to discuss, but I hope this helps those who have just started converting to AS3 (like me). I’ll keep on posting more things I learn.