Flash Builder converting number to string - apache-flex

I'm trying to calculate a BMI and set a label to the number, but it keeps returning "NaN" rather than the number.
Full code:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark" title="BMI Calculator">
<fx:Script>
<![CDATA[
public var weightnum:Number;
public var heightnum:Number;
public var resultvar:Number;
protected function button1_clickHandler():void
{
weightnum = Number(weightvar);
heightnum = Number(heightvar);
resultvar = weightnum * 4.4 / (heightnum * heightnum);
resultstr.text = resultvar.toString();
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Label left="30" top="30" text="Your Weight (lbs)"/>
<s:TextInput id="weightvar" restrict="0-9" x="30" y="53" width="80%"/>
<s:Label x="30" y="104" text="Your Height (feet)"/>
<s:TextInput id="heightvar" restrict="0-9" x="30" y="127" width="80%"/>
<s:Button x="30" y="202" label="Calculate" click="button1_clickHandler()"/>
<s:Label id="resultstr" x="30" y="253" text="" />

You're trying to cast the TextInput's to a Number (Number(weightvar);).
That obviously won't work.
What you need to do is to cast the TextInput's text property to a Number, just like this:
weightnum = Number(weightvar.text);

Instead of casting using String() use the number.toString()

Related

Display data properties of ItemRenderer

I have this ItemRenderer
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
autoDrawBackground="false">
<s:HGroup verticalAlign="middle">
<s:Button label="{data.Nome} ({data.Rating})" width="150" height="35"/>
<s:Button label="{data.Estado}" width="150" height="30"/>
</s:HGroup>
</s:ItemRenderer>
I'd like to see the properties of data object when typing . since its a custom object. How can I see them?
data is suppose to be a User class object.
Try this
<fx:Script>
<![CDATA[
import yourPackage.User;
[Bindable]
private var user:User;
override public function set data(value:Object):void{
super.data = value;
user = data as User;
}
]]>
</fx:Script>
<s:HGroup verticalAlign="middle">
<s:Button label="{user.Nome} ({user.Rating})" width="150" height="35"/>
<s:Button label="{user.Estado}" width="150" height="30"/>
</s:HGroup>
Either as Юрий Борыс said or you could also cast data as User:
<s:Button label="{User(data).Nome} ({User(data).Rating})" width="150" height="35"/>
HIH

Bind to textarea.text in a separate component file

I have a create characters component with a tabnavigator and textarea. In my main app I want to save the textarea id - mainchar text to the tag. I tried dotnotation, nothing, I tried import components.CreateCharacter; and still nothing, and tried some other options found on the net but can't get this to work.
Note, the code works fine if I call components in the main app, so the code works fine it is just the calling of the component (CreateCharacters) in the components folder. The components folder is in the src folder. Here is the code:
Main.mxml
<fx:Script>
<![CDATA[
import components.CreateCharacters
[Bindable]
public var xmlData:XML=<ROOTS></ROOTS>;
public function sav_clickHandler(event:MouseEvent):void
{
var fr:FileReference = new FileReference();
var ba:ByteArray = new ByteArray();
var newXmlRow:XML=<ROOTS>
<TXT>{components.CreateCharacters.mainchar.text}</TXT>// The problem lies with this line
<TXTA>{txt2.text}</TXTA>
<DTF>{txt3.text}</DTF>
</ROOTS>;
ba.writeMultiByte(newXmlRow, 'utf-8');
fr.save(ba);
}
private var openedFile:File;
private function open_clickHandler(event:MouseEvent):void {
openedFile = new File();
openedFile.addEventListener(Event.SELECT, file_select);
openedFile.browseForOpen("Please select a file...");
}
private function file_select(event:Event):void {
if(openedFile != null && openedFile.exists){
var fileStream:FileStream = new FileStream();
fileStream.open(openedFile, FileMode.READ);
var readXML:XML = XML(fileStream.readUTFBytes(fileStream.bytesAvailable));
fileStream.close();
trace(readXML.toString());
CreateCaracters.maichar.text = readXML.TXT;
txt2.text = readXML.TXTA;
txt3.text = readXML.DTF;
}
trace(event);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
<net:FileReference id="fileReference" />
</fx:Declarations>
<s:Image x="0" y="0" width="100%" height="50" scaleMode="stretch"
source="assets/imaginationFly.png"/>
<s:Image x="12" y="1" source="assets/ECWDove.png"/>
<mx:TabNavigator x="1" y="49" width="100%" height="100%" backgroundColor="#7EB7C5"
chromeColor="#85B5BF">
<s:NavigatorContent width="100%" height="100%" label="Start">
<s:TextArea id="txt2" x="57" y="29"/>
</s:NavigatorContent>
<s:NavigatorContent id="Characters" width="100%" height="100%" label="Characters">
<components:CreateCharacters id="creatchr" width="100%" height="100%"/>
</s:NavigatorContent>
<s:NavigatorContent width="100%" height="100%" label="Worlds">
<s:TextArea id="txt3" x="55" y="10"
text="hello
I am testing this shit
hope it works"/>
<s:Button id="sav" x="285" y="138" label="Save" click="sav_clickHandler(event)"/>
<s:Button id="open" x="381" y="138" label="Open" click="open"/>
etc...
CreateCharaters.mxml (component)
<?xml version="1.0" encoding="utf-8"?>
<s:HGroup xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
chromeColor="#0106BD" paddingLeft="10" paddingRight="0">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<mx:ViewStack id="Characters" x="172" y="10" width="81" height="100%" backgroundColor="#030BB3"
chromeColor="#D7D7D8" paddingLeft="5" paddingRight="15">
<s:NavigatorContent id="Hero" width="100%" height="95%" label="Main Charater">
<s:HGroup width="100%" height="100%">
<s:TextArea id="mainchar" bottom="0" width="80%" height="100%"
chromeColor="#7070FD">//this is the textarea I want to use
<s:text><![CDATA[
Name:
Surname:
Nickname:
Hair Color:
etc...
Any help please
Why would you need binding in that case?
Your XML node is created and the text property is evaluated inside the same function so it should not be a problem. In other words, when the event handler is called, the current value of text will be copied inside your XML node but any future changes won't be reflected (you probably don't need that anyway since you save the XML file when clicking 'Save').
As for the chain in between curly braces, {components.CreateCharacters.mainchar.text} does not seem correct because components.CreateCharacters is a class reference, and not the reference to the component directly. Instead you should use creatchr;
I am not sure if the variable replacement inside XML supports .. Have you tried saving the text value inside a local or a class variable like this ?
var currentText:String = creatchr.mainchar.text;
<TXT>{currentText}</TXT>

How can I do simple text input math calculations in flash-builder?

Can someone show me how should I correctly implement mathematical calculations like (+,-,/,*) using text inputs in 2 cases:
1 - more states
2 - input in a view an the result in other
Here is a part of my code:
<?xml version="1.0" encoding="utf-8"?>
<s:View xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
creationComplete="init()" currentState="State1" overlayControls="false" title="Calculator">
<s:states>
<s:State name="State1"/>
<s:State name="RezultatCalculator"/>
</s:states>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import spark.events.DropDownEvent;
protected function init():void
{
addEventListener(TransformGestureEvent.GESTURE_SWIPE, swipeHandler);
}
protected function swipeHandler(event:TransformGestureEvent):void
{
if (event.offsetX == 1)
{
navigator.pushView(CalculatorView);
}
}
protected function primulcalcul_openHandler(event:DropDownEvent):void
{
// TODO Auto-generated method stub
}
]]>
</fx:Script>
<s:Scroller includeIn="State1" left="3" right="3" top="0" height="547">
<s:VGroup width="100%" height="100%" gap="24" horizontalAlign="justify" paddingTop="8"
verticalAlign="top">
<s:Label id="actot" width="237" height="60" text="Active Totale" textAlign="center"
verticalAlign="middle"/>
<s:TextInput id="actot_val" width="183" height="60" fontFamily="_sans" fontSize="28"
textAlign="center" softKeyboardType="number" />
<s:Label id="disp" width="159" height="60" text="Disponibilitati" textAlign="center"
verticalAlign="middle"/>
<s:TextInput id="disp_val" width="164" height="60" fontFamily="_sans" fontSize="28"
textAlign="center" softKeyboardType="number"/>
<s:Label id="datot" width="159" height="60" text="Datorii Totale" textAlign="center"
verticalAlign="middle"/>
<s:TextInput id="datot_val" width="164" height="60" fontFamily="_sans"
fontSize="28" textAlign="center" softKeyboardType="number"/>
<s:Label id="caprop" width="159" height="60" fontSize="24" text="Capitaluri Proprii"
textAlign="center" verticalAlign="middle"/>
<s:TextInput id="caprop_val" width="164" height="60" fontFamily="_sans" fontSize="28"
textAlign="center" softKeyboardType="number"/>
<s:Button id="butstart0" width="401" height="70" label="START"
click="currentState='RezultatCalculator'" enabled="true"/>
</s:VGroup>
</s:Scroller>
<s:CalloutButton id="primulcalcul" includeIn="RezultatCalculator" x="22" y="28" width="145"
height="63" label="primulcalcul" enabled="true"
open="primulcalcul_openHandler(event)"/>
<s:TextArea id="Primul_val" includeIn="RezultatCalculator" x="203" y="27" width="267"
editable="false" prompt="result"/>
<fx:Script>
<![CDATA[
import flash.globalization.NumberFormatter;
import flashx.textLayout.formats.Float;
import flashx.textLayout.formats.Float;
import learnmath.mathml.components.MathMLFormula;
import learnmath.mathml.formula.layout.MathBox;
public function MathMLFormula():void
{
var Primul_val:Number=new Number
var datot:Number=new Number
var disp:Number=new Number
Primul_val=0
NumberFormatter(TextArea(Primul_val))==NumberFormatter(TextInput(datot))+NumberFormatter(TextInput(disp)); /* this is one of the examples, i tied some different values like valueOf */
}
]]>
</fx:Script>
</s:View>
I have a few suggestions, but I'm not sure exactly what your problem is.
First, use the restrict attribute on your TextArea. The restrict attribute will prevent people from entering non-numeric keys. Sort of like this:
<s:TextInput id="datot_val" width="164" height="60" fontFamily="_sans"
fontSize="28" textAlign="center" softKeyboardType="number" restrict="0-9"/>
It's entirely possible that the use of the softKeyboardType makes this a non-issue if you're using a mobile application on a device that supports it.
The line of code you mentioned in the comments looks to be error prone:
NumberFormatter(TextArea(Primul_val))==NumberFormatter(TextInput(datot))+NumberFormatter(TextInput(disp));
First, datot and disp are Labels; not TextInputs. They have hard coded non-numeric values. I think you want to use datot_val and disp_val. which are TextInputs. Since they are already inputs you do not need to cast them as such.
Second you do not need to cast the TextInput as a NumberFormatter. You would not usually use a NumberFormatter as a static class, but rather create an instance of it. More info on NumberFormatter here.
So, I believe your line would be rewritten like this:
Primul_val.text== String(
int(datot.text) + int(disp.text)
);
I added some extra lines so the parenthesis wouldn't get confused.
I take the input of datot (AKA datot.text) and cast it as an int. I take the input of disp (disp.text) and cast that as an int. I add the two together and convert them both to a String. The string is stored in the text value of Primul_val.
Does that help clear anything up?

Using [Bindable] in flex 4

I just don't get it with [Bindable] and updating a label
So here are my three pages, please tell me what I am doing wrong because when the listner sends the updated var to button2.mxml the var updates but the label does not redraw it.
application.mxml
<s:WindowedApplication
xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
xmlns:comps="components.*"
creationComplete="init();">
<fx:Script>
<![CDATA[
import mx.controls.Alert;
public function init(){
btn1.addEventListener(MouseEvent.MOUSE_OVER, myFunction);
}
public function myFunction(e:MouseEvent){
var myPage:button2 = new button2();
var ranNum = Math.floor(Math.random() * 40) + 10;
myPage.myValue("ABC "+ranNum);
}
]]>
</fx:Script>
<comps:button1 y="0" id="btn1" width="100"/>
<comps:button2 y="100" id="btn2" width="100"/>
button1.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="128" height="72">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button x="27" y="19" label="Button1" id="btn1" enabled="true"/>
</s:Group>
button2.mxml
<?xml version="1.0" encoding="utf-8"?>
<s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" width="400" height="300">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
[Bindable]
public var myVal:String = "Button2";
public function myValue(mV:String)
{
myVal = mV;
}
]]>
</fx:Script>
<s:Button x="10" y="32" label="{myVal}" id="btn2" enabled="true"/>
</s:Group>
Your function should be:
public function myFunction(e:MouseEvent){
var ranNum = Math.floor(Math.random() * 40) + 10;
btn2.myValue("ABC " + String(ranNum));
}
In the function that you have, you are creating a new button (without adding it as a child to anything) and setting the label on that button rather than the one you have already defined in your application.
You also do not necessarily need a [Bindable] variable for the label in button2.mxml but this depends on what you are accomplishing. You could just do btn2.label = mV; in the myValue() function definition alternatively.
The simplest way to do this is to remove your setter for myVal in button2.mxml and just set the value in myFunction like you would any other public variable:
myPage.myVal = "ABC " + ranNum;
The reason your code is not currently working is that you have implicitly overridden the myVal setter and are not dispatching a data change event, which is what makes binding work. When you add the [Bindable] metadata tag to a variable, the compiler automatically generates a setter for that variable with the proper event dispatching for you.
Hope that helps.

Is it possible to pause a playing SWF file in Adobe Flex? How?

I just wanna know if it is possible to pause a playing SWF file in adobe flex?
I have an SWF Loader and it plays my SWF file however, it does not have any capability (or built in function) that pauses the file.
Can anyone help me with this please? I'll appreciate some code to start with. :) Thanks in advance.
You can use the stop();
The following is an example of a swf playing and control is given to the play and pause and gotoandstop buttons.
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
private function playHandler():void {
var file_mc:MovieClip = fileswf.content as MovieClip;
file_mc.play();
}
private function pauseHandler():void {
var file_mc:MovieClip = fileswf.content as MovieClip;
file_mc.stop();
}
private function pauseat(frame:Number):void {
var file_mc:MovieClip = fileswf.content as MovieClip;
file_mc.gotoAndStop(frame);
}
]]>
</fx:Script>
<mx:SWFLoader x="0" y="0" source="abc.swf" id="fileswf"/>
<s:Button x="0" y="200" label="Play" id="playbtn" click="playHandler()"/>
<s:Button x="100" y="200" label="Pause" id="pausebtn" click="pauseHandler()"/>
<s:Button x="100" y="250" label="Pause at A" id="pauseAbtn" click="pauseat(1)"/>
<s:Button x="200" y="250" label="Pause at B" id="pauseBbtn" click="pauseat(2)"/>
<s:Button x="300" y="250" label="Pause at C" id="pauseCbtn" click="pauseat(3)"/>
<s:Button x="400" y="250" label="Pause at D" id="pauseDbtn" click="pauseat(4)"/>
<s:Button x="500" y="250" label="Pause at E" id="pauseEbtn" click="pauseat(5)"/>
</s:Application>
For completeness I have placed in the method for #Embed (it is not possible to get the original swf directly via SWFLoader [runtime vs compile time] but you can you can load the Bytes from a Class)
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
minWidth="955" minHeight="600" addedToStage="init()">
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<fx:Script>
<![CDATA[
[Embed(source="abc.swf", mimeType="application/octet-stream") ]
public var abc_cls:Class;
public var ldr:Loader = new Loader();
private var file_mc:MovieClip;
protected function init():void
{
ldr.loadBytes( new abc_cls() as ByteArray );
ldr.contentLoaderInfo.addEventListener(Event.INIT, onSwfLoaded);
swfcontainer.addChild(ldr);
}
private function onSwfLoaded(e:Event):void {
file_mc = ldr.content as MovieClip;
}
private function playHandler():void {
file_mc.play();
}
private function pauseHandler():void {
file_mc.stop();
}
private function pauseat(frame:Number):void {
file_mc.gotoAndStop(frame);
}
]]>
</fx:Script>
<mx:Image id="swfcontainer" />
<s:Button x="0" y="200" label="Play" id="playbtn" click="playHandler()"/>
<s:Button x="100" y="200" label="Pause" id="pausebtn" click="pauseHandler()"/>
<s:Button x="100" y="250" label="Pause at A" id="pauseAbtn" click="pauseat(1)"/>
<s:Button x="200" y="250" label="Pause at B" id="pauseBbtn" click="pauseat(2)"/>
<s:Button x="300" y="250" label="Pause at C" id="pauseCbtn" click="pauseat(3)"/>
<s:Button x="400" y="250" label="Pause at D" id="pauseDbtn" click="pauseat(4)"/>
<s:Button x="500" y="250" label="Pause at E" id="pauseEbtn" click="pauseat(5)"/>
</s:Application>
I am sure now that it is the embed that was giving the problem sorry for any confusions. So if you need your swf as part of your file below should the trick.
The last way you can achieve getting assets into your flex workspace is to use SWC Assets Method.
Good Luck ! :D

Resources