Why flash is not dead

flashI keep reading “Flash is dead” or “Flash is no use anymore”. Do they actually know what they’re talking about? Are they Flash experts, have they worked with real clients? I don’t think they do, are or have. How to bury the most innovative technology, which has inspired the current developments and uses of javascript, although it is currently the only way to offer a true rich web user experience.
Continue reading

Easy Fullscreen on a specific DisplayObject, deal with overlay and use hardware acceleration

There is several ways to show a specific zone/object in a Flash application in full-screen mode. The problems are to use hardware acceleration (interesting for video), to deal with possible overlay (display object above the target we want to show in full-screen mode) or to avoid with UI pixelization. And we have to consider various player versions and hardwares…
Continue reading

Simple component invalidation in AS3

When we develop components, it quickly becomes necessary not to update the display every time we change a parameter.

For example, this code calls three times my draw method :

1
2
3
myComps.width = 100;
myComps.height = 100;
myComps.color = 0xFF0000;

Mmmm ? Stupid ?

The solution is to wait for the next enterFrame event to update the display.
The UIComponent that is the base of Flash components offers all the methods to do this, but the last thing I want is to use them.

Here’s how to set up a simple invalidation:

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package
{
	import flash.display.Sprite;
	import flash.events.Event;
 
	public class InvalidateSprite extends Sprite 
	{
		private var _width:Number = 150;
		private var _height:Number = 100;
		private var _color:Number = 0xCCCCCC;
 
		public function InvalidateSprite(){
			_invalidate();
		}
 
		public override function set width(value:Number):void {
			_width = value;
			_invalidate();
		}
 
		public override function set height(value:Number):void {
			_height = value;
			_invalidate();
		}
 
		public function set color(value:Number):void {
			_color = value;
			_invalidate();
		}
 
		private function _draw():void {
			graphics.clear();
			graphics.beginFill(_color, 1);
			graphics.drawRect(0, 0, _width, _height);
		}
 
		private function _invalidate():void {
			if(!hasEventListener(Event.ENTER_FRAME))
				addEventListener(Event.ENTER_FRAME, _invalidateCallback);
		}
 
		private function _invalidateCallback(e:Event):void {
			removeEventListener(Event.ENTER_FRAME, _invalidateCallback);
			_draw();
		}
	}
}

Generate xsd from xml with trang

An xsd (XML Schema definition) is a .xsd file that describes the structure of an XML document and allows us to validate and to have autocompletion on our xml file, for example with a xml editor like XMLNotePad.

An easy way do this is to generate your xsd from xml with trang command line tool.
Trang is written in java and packaged for use with a Java runtime (>=version5).

1. Download zip http://www.thaiopensource.com/download/ and extract it where you want
2. You only need to execute the following command (with your pathes)

cd C:\xml
java -jar C:\trang.jar tutorials.xml tutorials.xsd

3. Oh yeah it’s done

Our xml :

<?xml version="1.0" encoding="UTF-8"?>
<tutorials>
    <tutorial id="1" blogged="true">
        <date>2009-09-28</date>
        <author>Jonas</author>
		<copyright>Jonas Monnier</copyright>
		<title>Display JSON strings in AMFPHP 1.9 brwoser</title>
    </tutorial>
    <tutorial id="2" blogged="true">
        <date>2010-07-03</date>
        <author>Jonas</author>
		<title>Generate xsd from xml with trang</title>
    </tutorial>
</tutorials>

Our xsd :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="tutorials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="tutorial"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="tutorial">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="date"/>
        <xs:element ref="author"/>
        <xs:element minOccurs="0" ref="copyright"/>
        <xs:element ref="title"/>
      </xs:sequence>
      <xs:attribute name="blogged" use="required" type="xs:boolean"/>
      <xs:attribute name="id" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="date" type="xs:NMTOKEN"/>
  <xs:element name="author" type="xs:NCName"/>
  <xs:element name="copyright" type="xs:string"/>
  <xs:element name="title" type="xs:string"/>
</xs:schema>

We tell the XML parser that this document should be validated against a schema :

<?xml version="1.0" encoding="UTF-8"?>
<tutorials
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="tutorials.xsd">
    <tutorial id="1" blogged="true">
        <date>2009-09-28</date>
        <author>Jonas</author>
		<copyright>Jonas Monnier</copyright>
		<title>Display JSON strings in AMFPHP 1.9 brwoser</title>
    </tutorial>
    <tutorial id="2" blogged="true">
        <date>2010-07-03</date>
        <author>Jonas</author>
		<title>Generate xsd from xml with trang</title>
    </tutorial>
</tutorials>

And we change the date and author definition (nobody is perfect) :

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="tutorials">
    <xs:complexType>
      <xs:sequence>
        <xs:element maxOccurs="unbounded" ref="tutorial"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="tutorial">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="date"/>
        <xs:element ref="author"/>
        <xs:element minOccurs="0" ref="copyright"/>
        <xs:element ref="title"/>
      </xs:sequence>
      <xs:attribute name="blogged" use="required" type="xs:boolean"/>
      <xs:attribute name="id" use="required" type="xs:integer"/>
    </xs:complexType>
  </xs:element>
  <xs:element name="date" type="xs:date"/>
  <xs:element name="author" type="xs:string"/>
  <xs:element name="copyright" type="xs:string"/>
  <xs:element name="title" type="xs:string"/>
</xs:schema>

That’s all !

You can create a cmd alias for an easiest use :

Windows :

doskey trang=java -jar C:\trang\trang.jar $*

See my previous post to make it working Create cmd aliases in Windows

Linux :

alias trang='java -jar ~/trang/trang.jar'

Héhé

cd C:\xml
trang tutorials.xml tutorials.xsd

More options in trang manual.