Posts tagged flex
Working With Large Arrays in Action Script
0While working on partly connected system using flex technology I figured out quick enough that working with large data sets (thousands of records) is not applicable. Especially because i held a number of those sets which I needed to cross reference in number of occasions.
My solution was to create an indexed collection which i can benefit both worlds of Dictionary and ArrayCollection , with that in mind I designed and implemented the IndexedArrayCollection.
The collection extends ArrayCollection and adds the capability of indexing a single member of an object in the collection (I assume that each element is an object).
IndexArrayCollection constructor receives two arguments first is a source array and second is the index field name (attribute name).
Pong Applied Box2DFlex Physics Engine
0I decided to learn by example and assigned myself the goal of building pong in box2d.
box2d is an overkill for pong , but what do you know, it’s damn easy.
the gist of it is :
- layout
- world setup
- event handling
Full source code hosted in github Pong in Box2DFlex.
The layout
a SkinnablePhysicsContainer holding two border containers (named p1 and p2) which are the player bars and corelating p1Goal and p2Goal which will act as hit area for the ball graphics element (Ellipse).
Setting Up the World
when the application loads it maps all elements in the physics container to box2d bodies , in woldSetup method i specified different mapping types to components
The key thing you need to know is that you want p1, p2 and the goals controls to be kinematic bodies so
- they are not affected by collations and
- they do not collide with each other
- they affect dynamic bodies colliding into them
and an event handler onBodiesContact that triggers when two bodies collide .
Event Hadnling
onBodiesContact is triggered each time two bodies collide indie it i do a simple test to see which to bodies collided and triggers the respective indicator.
world_mouseMoveHandler is handling the translation of mouse position and movement of p2
Box2D Flex Complex Shapes Mapping
2One of the drawbacks in box2d is that it’s not supporting concave polygon shapes .
for that box2d flex employs several algorithms and converts a complex shape to a set of polygons (I took the idea from 3d modeling)
at this point i would like to give credits to
- sakri.net for he’s shape outline algorithm
- makc.coverthesky.com for he’s polygon2d
both state that their algorithms are far from perfect but for a v0.2 it’s a good start.
in this case I’ll start from the demo and then go into the code so see how the doll bellow is segmented to several triangles.
first thing is to understand how what the doll constructs of , in our case it’s a graphics
our object can be comprised of anything , graphic shapes , ui controls, images etc’ it has only one caveat - It’s edges must be transparent .
The next thing it to take a look at our physics container
you can see that autoStartPhysicsEngine is set to false. By default SkinnablePhysicsContainer maps all it’s flex objects to box2d shapes on creation, this time we want to interfere before hand and give the engine a different directive to try and map the shape not by it’s boundingBox but by it’s actual edges.
This is done by actionscript code, in which we tell our physics container to parse complex shape and pass it with the shape id and a parsing object, afterwards we call the physics engine to start.
you can see the the shape parser receives an argument specifying the quality of it’s mapping , this is done for performance improvement but might impair edges fidelity.
Simple Box2D Flex Example
0following my previous post announcing Box2D Flex framework i want to share some techniques via examples on how to use the library.
First thing we create add a SkinableContentContainer to and drop some contorls inside it
This, still, does not do anything fancy it just initializes box2d and registers & measures all controls inside our physics container.
next step we add some bindable controls that will change our physics gravity and debug
and the bindable properties to our physics container
now we can play with the gravity sliders and see flex controls bounce around
Announcing Box2D Flex framework
2Prefix
In a previous post demonstrating box2d integration with flex I made a promise (mainly to myself) to share the demo code. When I started to review with sharing in mind it looked cumbersome and overly verbose. So I started cranking on it fixing and straightening the code & architecture and step by step I realized that I’m creating a small Integration layer rather then a POC.
Announcing Box2DFlex
- extends SkinnableContainer uses flex component lifecycle model
- uses regular MXML and AS flex components
- supports complex shaped flex components (convex and concave) conversion to b2d shapes.
- allows multiple worlds side by side
Getting Started
In order to play with Box2dFlex you will need flash builder 4 and Box2DFlex source which is hosted on GitHub.
and in four simple steps you should
- download the source code
- inport the Box2DFlex and Demos projects into flash builder 4
- compile
- Play with the eamples in Box2Demos project
Building Your Own b2d World
- download the framework
- create a new project
- add refrence to <Box2DFlex dir>/Box2dFlexLib/bin/Box2DFlexLib.swc
- in your MXML add a new SkinnablePhysicsContainer
- set the container’s yGravity property to 10 and autoStartPhysicsEngine to true (yGravity=”10 autoStartPhysicsEngine=”true”)
- if you want your world to contains walls use setBoundries=”true” attribute
- place a button inside SkinnablePhysicsContainer and set it’s properties (width / height / label)
- run the project
Flex 3 hebrew textbox
0following my last post on flex3 RTL label , next challenge was input text with RTL support , in that case I decided to cheat and use html input overlying flex.
Not so elegant solution, but a solution non the less. the trick is to pass the flex component coordinate and position the HTML input accordingly , and on the HTML side handle keyboard and mouse event to let the flex developer feel @ home with regular focus and key press events.
Add the following MXML component to your flex project
Add the following code to your html page
box2d flex integration
0[kml_flashembed publishmethod="static" fversion="10.0.0" movie="/blog/wp-content/uploads/2010/04/Box2DFlex.swf" width="300" height="300" targetclass="flashmovie"]
[/kml_flashembed]
Besides collapsing the form you can also drag N drop the items to see collusion and gravity. code will follow soon .. I hope ..
Multitouch + 3D flash 10.1
0I’ve compiled a POC of a 3D model controlled by multitouch events in flex (actually AIR 2.0).
In the application I use TransformGestureEvent dispatched from flash 10.1 player. unfortunately this is the only event fired in Mac and only in AIR mode – adobe it’s lame.
TransformGestureEvent event availability
| Event | Mac Air | Mac Web | Windows (Air+Web) |
| GestureEvent.GESTURE_TWO_FINGER_TAP | v | ||
| GestureEvent.GESTURE_PAN | v | ||
| TransformGestureEvent.GESTURE_ROTATE | v | v | |
| TransformGestureEvent.GESTURE_SWIPE | v | v | |
| TransformGestureEvent.GESTURE_ZOOM | v | v |
TouchEvent availability - windows only (and iPhone but i guess it’s not applicable any more)
| Event | Mac Air | Mac Web | Windows (Air+Web) |
| TouchEvent.TOUCH_END | v | ||
| TouchEvent.TOUCH_MOVE | v | ||
| TouchEvent.TOUCH_OUT | v | ||
| TouchEvent.TOUCH_OVER | v | ||
| TouchEvent.TOUCH_ROLL_OUT | v | ||
| TouchEvent.TOUCH_ROLL_OVER | v | ||
| TouchEvent.TOUCH_TAP | v |
the first version was a bit stale so I’ve added KineticMovementManager which adds a drift effect (like when scrolling an iPhone scroll) but it’s still a work in progress so don’t relay on it.
download source
download installer
[kml_flashembed publishmethod="static" fversion="8.0.0" movie="/blog/wp-content/uploads/2010/04/multitouch 3d take 2.swf" width="550" height="430" targetclass="flashmovie"]
[/kml_flashembed]
