Monday, July 2, 2012

Why a/b*c is not equal to a*c/b

For some it may be obvious, but it took me a while to understand a problem. So I thought that multiplication and division are alternating. Maybe for Mathematics but not for computer. Let's have a look at example:



var a:Number = 1400;
var b:Number = 10000;
var c:Number = 100;


trace(a/b*c);
trace(a*c/b);

From mathematical point of view those results should be equal. But here what flash returns:


14.000000000000002
14

Of course simple round, toFixed() or other operation will mask this problem but it's not a good solution.
Type Number is double-precision with 64bits representation. Division with result different then integer is represents by number with 16 digits of precision.


Monday, January 30, 2012

unable to open 'C:\... libs\player\9.0\playerglobal.swc' - Solution

If you ever see an error unable to open 'C:\... libs\player\9.0\playerglobal.swc' in your Problems tab maybe below is a solution for you.
I found this situation while importing some projects. I'm still using flex builder 3 while project has been imported from flash builder 4 where was SDK 4.1. I'm not sure why, but somehow during import some old sdk was used as default. Of course I switched it to the good one but then this error occurred.

The fastest way is to set, and later unset requiring a flash version in compiler tab in project properties.
All you have to do is to check 'Generate HTML wrapper file.
Check Require Flash Player version ans set at least major number to 10.
Press Apply.
Uncheck 'Generate ...' so it will turn off.
It's done!




Friday, November 11, 2011

Problem with updating SDK in Flash builder - Flash player version

I've added another version of  SDK to my Flash builder. It's easy as CTRL+C, CTRL+V. But afterwards my projects were complaining that they don't see flash player and throws error with playerglobal.swc in it's title.

I have to mention that I was switching from sdk 3.5 to 4.1 so automatically I had to upgrade player form version 9 to 10.

In my project properties I had unchecked generating html site with embedded flash like on screen below. I thought if it is disabled IDE is not taking this into consideration. WRONG!
When I:

  1.  selected "Generate HTML wrapper file"
  2. put new version of flash player (10.0.0 instead of 9.0.124)
  3. pressed "Apply"
  4. unselected "Generate HTML wrapper file"
Errors dissapeared.

One more thing. I'm not using html wraper file. I'm not sure how it works when you have it selected. Maybe it'll update automatically. If not do it manually but with ommiting points 1 and 4 from my list.

Monday, October 3, 2011

HTML tags in textfield

If you want to format text in field using old html tag here is simple list of possible tags:


  • <a> hyperlink. Has two parameters:
    • target - target window
    • href - url  (or event!)
  • <b> - text betwen those tags will be bold
  • <br> - breaks line
  • <font> - describes font:
    • color - color of the text. Must be in hex (with # at the begining)
    • face - font name
    • size - font size
  • <img>  allows to embed image (or swf) like in html site. Here is a list of supported properties:
    • src - source of media
    • width - width of rectangle where media is placed (always in px)
    • height - height of rectangle where media is placed (always in px)
    • align - horizontal alignment. Left or right. Specifies on which side text will flow around the image
    • hspace - horizontal margin around media. By default it is 8px
    • vspace - vertical margin around media. By default it is 8px
    • id - identifier for embedded media. Used to controll it
    • checkPolicyFile - connected to the security policy. Used with flash swf file embedded
  • <i> - text between those tags will be italic. 
  • <li>, <ul>, <ol> - lists. ordered and dotted
  • <p> - paragraph. Supports only two attributes:
    • align - specifies if text block is aligned to left/right/center or justify
    • class - connects with CSS class
  • <span> - inline part of text that allows only one attribute:
    • class - classname from CSS
  • <u> - text between those tags will be underlined

Thursday, September 1, 2011

Disabling Textfield scrolling


This tip is probably only for my own memory.

When you want to disable scrolling multiline textfield by mouse wheel use this:

myTextField.mouseWheelEnabled = false;

that's all folks!

Wednesday, August 17, 2011

One image, three states of button.


When we want to create custom button, usually three states considered: mouse over (roll over), mouse out (roll out) and press (click, release). If it is advanced, with multiple animations, and differences between those free states, theres no other way to create each of them separately. But if You need quite simple button which changes its color, look at this example.

Firstly some declarations:

private var _loader:Loader;


private var _colorTransform:ColorTransform;
private var _rollOverColorTransform:ColorTransform;
private var _clickColorTransform:ColorTransform;
        
private var _color:uint;
private var _rollOverColor:uint;
private var _clickColor:uint;

We also have to define values:

_color = 0xFFFFFF;
_rollOverColor = 0xFF6600;

We load image:

_loader = new Loader();
_loader.load(new URLRequest(url));
addChild(_loader);
buttonMode = true;

Prepare transformations for switching states:

_colorTransform = _loader.transform.colorTransform;
_colorTransform.color = _color;
_loader.transform.colorTransform = _colorTransform;
            
_rollOverColorTransform = _loader.transform.colorTransform;
_rollOverColorTransform.color = _rollOverColor;


_clickColorTransform = _loader.transform.colorTransform;
_clickColorTransform.color = _clickColor;

Then we set listeners:

addEventListener(MouseEvent.ROLL_OVER, handleRollOver);
addEventListener(MouseEvent.ROLL_OUT, handleRollOut);
addEventListener(MouseEvent.MOUSE_DOWN, handleMouseDown);

And handlers for events:


private function handleRollOver(e:MouseEvent):void
{
    _loader.transform.colorTransform = _rollOverColorTransform;
}
    
private function handleRollOut(e:MouseEvent):void
{
    _loader.transform.colorTransform = _colorTransform;
}
        
private function handleMouseDown(e:MouseEvent):void
{
    _loader.transform.colorTransform = _clickColorTransform;
}

And it's done!

Check it:




Saturday, August 6, 2011

Letter Spacing - TextFormat or css StyleSheet

If you;re looking for example of setting letter spacing - here it is.

The first solution is TextFormat. The property is named same in both options - letterSpacing. Let's see the code:

var txtfmodified:TextField = new TextField();
addChild(txtfmodified);
txtfmodified.y =20;
txtfmodified.text = 'hello world';
var tf:TextFormat = txtfmodified.getTextFormat();
tf.letterSpacing = 3; /* I tried lots of different values here. */
txtfmodified.setTextFormat(tf);

We can also use stylesheet to adjust part of text (in previous solution you can also adjust part of text but selecting chars is more complicated than using styles for classes of html tags). Here is the example:

var txtfmodified2:TextField = new TextField();
addChild(txtfmodified2);
txtfmodified2.y =40;
txtfmodified2.text = 'hello world';
var s:StyleSheet = new StyleSheet();
s.setStyle("p", {letterSpacing:3});
txtfmodified2.styleSheet = s;
txtfmodified2.htmlText = "<p>hello world</p>";

Remember that there are two solutions but you cannot mix them. Either way you'll receive error like this:
Error: Error #2009: This method cannot be used on a text field with a style sheet


And a proof at the end. First line is simple not modified field. Second line is modified by TextFormat properties. Third line is adjusted by stylesheet.