flashgameblog.at

Currently the posts are filtered by: libs
Reset this filter to see all posts.

11.08.2010
10:24

Layout management for display objects

 

I am working hard at the next update for www.kineticarm.com. So I didn't have much time for the blog in the last month, sorry for that.

 

At my latest custom project I had a complex GUI system and the whole Movie had to be resizeable. So a layout manager seemed to be a proper solution. I just wanted to have a system where I define positions and sizes of the GUI objects once and without having to care about it afterwards.

I builded a static Layoutmanager class where I register all objects and a helper class that allows me to define position and size of each object.

Values can be defined absolute and relative as in CSS and referenced to top left and bottom right border.

  • all values ending with "px" are absolute
  • all values ending width "%" are relative
  • all values starting width "-" are referenced to bottom/right border.
  • all values starting with "+" or without any sign are referenced to top/left border.

 

On top of that all properties can be referenced to the stage or other objects as well. So if layout gets really complex it's possible to build unvisible dummy objects to align other objects to it.Here are some Examples:

 

  1.  
  2. // Created an instance from an object from the lib.
  3. // and adds it to the stage
  4. var obj1:MovieClip = new Item();
  5. addChild(obj1);   
  6.  
  7.  
  8. // creates a layout object for the added Displayobject
  9. // The constructor takes the display object
  10. lit = new LayoutItem(obj1);
  11. lit.scale = false              // the size isn't touched
  12. lit.x = "90%";                 // X pos. is 90% of stage width   
  13. lit.y = "50%";                 // Y pos. is 50% of stage height
  14. Layouter.registerObject(lit)// registers the object to the layoutManager
  15.  
  16.  
  17. // Created an instance from an object from the lib.
  18. // and adds it to the stage
  19. var obj2:MovieClip = new Item();
  20. addChild(obj2);   
  21.  
  22.  
  23. // creates a layout object for the added Displayobject
  24. // The constructor takes the display object
  25. lit = new LayoutItem(obj2);
  26. lit.reference = obj1;          // values references to obj1
  27. lit.scale = false              // the size isn't touched
  28. lit.x = "-100px";              // X pos. is 100 pixels left to obj1
  29. lit.y = "0px";                 // Y pos. is obj1.y
  30. Layouter.registerObject(lit);
  31.  

 

The only thing you need to do is to call the static method "arrange" of the LayoutManager everytime you want to rearrange all registered objects. This is mostly done in a function called at the stage's resize event.


Here you can download the source code with some examples.

20.03.2010
13:06

Depth of Field in flash (DOF)

For my latest game I wanted to simulate DOF in flash. In flash we can apply filters to objects and flash provides a blur filter, this is the base of the work. What we need is a mechanism to coordinate the depths of the objects.

First We build a class that manages everything about DOF. This class needs an array which holds references to all the objects we want to use with DOF. We build a virtual space with the depths of 1 where 0 is the position of the camera and 1 is the point most faraway from the camera. Our class has a function addObject which takes the display object and a depths in our virtual space. Next our class has got a property maxBlur that defines how strong the maximum blur effect will be. And of course there is a property focus which defines the current focus.

In my game I needed the effect animated, so I added a main function which manipulates the focus and recalculates the blur effect for each object.

  1. package at.lueftenegger.dof {
  2.  
  3.   import at.lueftenegger.dof.operator.Operator;
  4.   import flash.display.DisplayObject;
  5.   import flash.filters.BlurFilter;
  6.  
  7.   public class Dof{
  8.    
  9.     public var maxBlur:Number   = 5;
  10.     private var _focus:Number   = 0; // 0 -> 1
  11.     private var last_focus:Number;
  12.     public var operator:Operator;
  13.    
  14.     public function Dof(){
  15.       objects = new Array();
  16.       operator = new Operator();
  17.     }
  18.    
  19.     protected var objects:Array;
  20.    
  21.     public function set focus(value:Number):void {
  22.       _focus = value;
  23.       render();
  24.     }
  25.    
  26.     public function addLayer( object:DisplayObject, position:Number ):void {
  27.      
  28.       if (position < 0) position = 0;
  29.       if (position > 1) position = 1;
  30.      
  31.       var l:Layer = new Layer();
  32.       l.dispObj = object;
  33.       l.position = position;
  34.       objects.push(l);
  35.     }
  36.    
  37.     public function main():void {
  38.      
  39.       _focus = operator.giveFocusValue();
  40.      
  41.       if(_focus == last_focus) return;
  42.      
  43.       render();
  44.       last_focus = _focus;
  45.     }
  46.    
  47.     private function render():void {
  48.       for ( var i:String in objects) {
  49.         var h:Number( (objects[i].position - _focus) < 0 ) ? -(objects[i].position - _focus) : (objects[i].position - _focus);
  50.         var blur:Number = h * maxBlur;
  51.         var filter:BlurFilter = new BlurFilter( blur, blur, 1 );
  52.         var filterss:Array = [];
  53.         filterss.push(filter);
  54.         objects[i].dispObj.filters = filterss;
  55.       }
  56.     }
  57.   }
  58. }
  59.  
  60. package at.lueftenegger.dof{
  61.   import flash.display.DisplayObject;
  62.   public class Layer{
  63.     public var position:Number;
  64.     public var dispObj:DisplayObject;
  65.   }
  66.  
  67. }
  68.  
  69. package at.lueftenegger.dof.operator{
  70.  
  71.   public class Operator{
  72.    
  73.     public function Operator(speed:Number = 0.15){
  74.      
  75.     }
  76.    
  77.     public function giveFocusValue():Number {
  78.       return 0.5;
  79.     }
  80.    
  81.   }
  82.  
  83. }
  84.  
  85. package at.lueftenegger.dof.operator{
  86.  
  87.   public class Sinus extends Operator{
  88.    
  89.     private var speed:Number;
  90.     private var run:Number = 0;
  91.    
  92.     public function Sinus(speed:Number = 0.15){
  93.       this.speed = speed;
  94.     }
  95.    
  96.     override public function giveFocusValue():Number {
  97.       run += speed;
  98.       return  0.5 + Math.sin(run) * 0.5;
  99.     }
  100.   }
  101.  
  102. }
  103.  

How to use it

  1. Create a new FLA file.
  2. Put some MovieClips on screen and give them names.
  3. Add the following script.
  1. var dof:Dof = new Dof();
  2. dof.operator = new Sinus(0.1);
  3. dof.maxBlur = 10;
  4. dof.addLayer( getChildByName("object1"),0);
  5. dof.addLayer( getChildByName("object2"),0.2);
  6. dof.addLayer( getChildByName("object3"),0.4);
  7. dof.addLayer( getChildByName("object4"),0.6);
  8. dof.addLayer( getChildByName("object5"),0.8);
  9. dof.focus = 0.4;
02.12.2009
23:03

plasma

You can reload the page to see different effects.

 

Some weeks ago I needed some eye candy for my latest game. The easiest way was to put some plasma in the background. But mostly plasma algorithms generate a repetitive pattern and i wanted a better one.

 

I found this post in david's blog: http://www.gibbongames.com/?p=167

 

Unfortunatley this program is written in blitzmax, a language I don't know at all. But the syntax is easy to understand, so it took just some hours to port it.

I have added one nice feature. You can define how many lines are calculated by render circle. So you can screw down the fps of the plasma effect and use the CPU for what you really want to do.

download: click hear to download a demo project.

 

p.s.: My favourite line of code in this project is

 

if ( ubyte[plasma_id] & 128 )
                            ubyte[plasma_id] =  (~ubyte[plasma_id])&0x7F;

 

It's a very good example of the power of bit operation.

< < September 2010 > >
S M T W T F S
      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    

Blog rolls

Latest Comments

ISO Blender Camera Setup
24.04.2010 06:58
update
21.03.2010 01:15
if-Statement
16.11.2009 08:34
hmm...
16.11.2009 08:00

Archive

  • [-]2010(8)
    • [-]August(1)
    • [-]July(1)
    • [-]June(2)
    • [-]April(1)
    • [-]March(2)
    • [-]February(1)
  • [-]2009(13)
    • [-]December(4)
    • [-]November(8)
    • [-]October(1)

Copy and paste this link into your RSS news reader

RSS 0.91Posts
RSS 2.0Posts

Social Bookmarking

Bookmark bei: Mr. Wong Bookmark bei: Webnews Bookmark bei: Icio Bookmark bei: Oneview Bookmark bei: Linkarena Bookmark bei: Favoriten Bookmark bei: Seekxl Bookmark bei: Favit Bookmark bei: Social Bookmarking Tool Bookmark bei: Power Oldie Bookmark bei: Bookmarks.cc Bookmark bei: Newskick Bookmark bei: Newsider Bookmark bei: Linksilo Bookmark bei: Readster Bookmark bei: Folkd Bookmark bei: Yigg Bookmark bei: Digg Bookmark bei: Del.icio.us Bookmark bei: Reddit Bookmark bei: Simpy Bookmark bei: StumbleUpon Bookmark bei: Slashdot Bookmark bei: Netscape Bookmark bei: Furl Bookmark bei: Yahoo Bookmark bei: Spurl Bookmark bei: Google Bookmark bei: Blinklist Bookmark bei: Blogmarks Bookmark bei: Diigo Bookmark bei: Technorati Bookmark bei: Newsvine Bookmark bei: Blinkbits Bookmark bei: Ma.Gnolia Bookmark bei: Smarking Bookmark bei: Netvouz Information