Skip to main content
Participating Frequently
April 22, 2014
Question

Flash Develop

  • April 22, 2014
  • 1 reply
  • 654 views

I am currently using flash develop and action script 3.0 to code a converter which will convert cm(centimetres) to inches. The user inputs the cm's and the computer generates the output in inches. I know the calculation is input divided by 2.54 but cant get my code to work. Any ideas anyone?

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
April 22, 2014

What is the code you have that does not work?  How is it set up with respect to capturing the user's input?

Participating Frequently
April 23, 2014

Hi Ned, Thanks for the reply. This is a task i am doing for uni. I only have to code the cm to inches part. I have tried different things to get it to work but currently cant even get the cursor to come up so i can enter the cm!! Below is the current version i was working on. It is tab 4 which contains the cm to inches converter. Any help would be fantastic. Thanks again.

package

{

          import flash.display.MovieClip;

          import flash.display.Sprite;

          import flash.events.Event;

          import flash.events.MouseEvent;

          import flash.net.URLRequest;

          import flash.display.Loader;

          import flash.text.TextField;

          import flash.text.TextFormat;

          import flash.events.KeyboardEvent;

          import flash.ui.Keyboard;

          import flash.text.*;

 

          [SWF(height="240", width="320", backgroundColor="#FFFFFF", frameRate="60")]

          public class Main extends Sprite

          {

 

                    public var tfFormat:TextFormat = new TextFormat();

                    public var inputField:TextField;

                    public var backgdImage:MovieClip;

                    public var title:Sprite, tab1:Sprite, tab2:Sprite, tab3:Sprite, tab4:Sprite, convertBtn:Sprite;

                    public var input:TextField, output:TextField, inputLabel:TextField, outputLabel:TextField;

                    public var converting:String;

                    public var cmGuess:uint;

                    public var randomNumber:uint;

                    //public var currentQuestion:uint;

                    //public var currentWord:String;

 

                    public function Main():void

                    {

                              setUpInterface();

                    }

 

                    public function setUpInterface():void {

                              converting = "Fahrenheit";

                              title = addImage("../images/title.png", "title", 10, 5, false, Sprite);

                              tab1 = addImage("../images/btn1.png", "tab1", 20, 50, true, Sprite);

                              tab2 = addImage("../images/btn2.png", "tab2", 88, 50, true, Sprite);

                              tab3 = addImage("../images/btn3.png", "tab3", 156, 50, true, Sprite);

                              tab4 = addImage("../images/btn4.png", "tab4", 224, 50, true, Sprite);

                              backgdImage = addImage("../images/bk1.png", "back", 5, 70, false, MovieClip);

                              backgdImage.tab2bk = addImage("../images/bk2.png", "back", 5, 70, false, Sprite);

                              backgdImage.tab2bk.visible = false;

                              backgdImage.tab3bk = addImage("../images/bk3.png", "back", 5, 70, false, Sprite);

                              backgdImage.tab3bk.visible = false;

                              backgdImage.tab4bk = addImage("../images/bk4.png", "back", 5, 70, false, Sprite);

                              backgdImage.tab4bk.visible = false;

                              convertBtn = addImage("../images/convert.png", "convertBtn", 155, 125, true, Sprite);

 

                              tab1.addEventListener(MouseEvent.CLICK, tab1Handler);

                              tab2.addEventListener(MouseEvent.CLICK, tab2Handler);

                              tab3.addEventListener(MouseEvent.CLICK, tab3Handler);

                              tab4.addEventListener(MouseEvent.CLICK, tab4Handler);

                              convertBtn.addEventListener(MouseEvent.CLICK, convert);

                              //Add the Enter key

                              //stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressHandler);

                              //input.type = "input";

                              //input.alpha = 0.5;

 

                              setTextFieldFormat();

                              placeTextField("input", 140, 90, 150, 25, "input", true, "0xFFFFFF");

                              placeTextField("output", 30, 180, 150, 25, "dynamic", true, "0xFFFFFF");

                              setLabelFormat();

                              placeTextField("inputLabel", 20, 90, 150, 25, "dynamic", false, "");

                              inputLabel.text = "Enter Fahrenheit";

                              placeTextField("outputLabel", 200, 180, 150, 25, "dynamic", false, "");

                              outputLabel.text = "Centigrade";

                    }

 

                    public function tab1Handler(event:MouseEvent):void {

                              backgdImage.tab2bk.visible = false;

                              backgdImage.tab3bk.visible = false;

                              backgdImage.tab4bk.visible = false;

                              inputLabel.text = "Enter Fahrenheit";

                              outputLabel.text = "Centigrade";

                              input.text = "";

                              output.text = "";

                              converting = "Fahrenheit";

                    }

 

                    public function tab2Handler(event:MouseEvent):void {

                              backgdImage.tab2bk.visible = true;

                              inputLabel.text = "Enter Grams";

                              outputLabel.text = "Pounds";

                              input.text = "";

                              output.text = "";

                              converting = "Grams";

                    }

 

                    public function tab3Handler(event:MouseEvent):void {

                              backgdImage.tab3bk.visible = true;

                              inputLabel.text = "Enter Centimeters";

                              outputLabel.text = "Inches";

                              input.type = "input";

                              input.text = "";

                              input.maxChars = 3;

                              input.restrict = "0-100";

                              output.text = "";

                              converting = "Centimeters";

                    }

 

                    public function tab4Handler(event:MouseEvent):void {

                              backgdImage.tab4bk.visible = true;

                              inputLabel.text = "Enter Kilometres";

                              outputLabel.text = "Miles";

                              input.text = "";

                              output.text = "";

                              converting = "Kilometers";

                    }

                    public function convert(event:MouseEvent):void {

                              /*convert cm to inches 1inch = 2.54cm   1 cm = 0.39370079 inches

                              input.type = "input";

                              input.text = "";

                              input.maxChars = 3;

                              input.restrict = "0-100";

                              output.text = "";

                              converting = "Centimeters";

                              //converting = "output.text" == (["input.text"] / 2.54);*/

                              input.text = "";

                              //output.text = cmGuess;

                              trace ("input.text");

                              //converting = (Math.floor(Math.random() * "randomNumber" / (2.54)));

                              cmGuess = (Math.floor(Math.random() * randomNumber / (2.54)));

                              output.text = "cmGuess";

                        cmGuess = (Math.floor(Math.random() * randomNumber / (2.54)));

                                        trace ("cmGuess");

                    }

 

                    /*public function keyPressHandler(event:KeyboardEvent):void

                    {

                              if (event.keyCode == Keyboard.ENTER)

                              {  

                                        cmGuess = (Math.floor(Math.random() * randomNumber / (2.54)));

                                        trace ("cmGuess");

                              }

                    }*/

 

                    public function addImage(imageFile:String, imgName:String, xPos:int, yPos:int, btnMode:Boolean, objType:Class):* {

                              var imageURL: URLRequest;

                              var imageLoad: Loader;

                              var image:* = new (objType);

                              imageURL = new URLRequest();

                              imageLoad = new Loader();

                              imageURL.url = imageFile;

                              imageLoad.load(imageURL);

                              image.addChild(imageLoad);

                              stage.addChild(image);

                              image.name = imgName;

                              image.buttonMode = btnMode;

                              image.x = xPos;

                              image.y = yPos;

                              image.visible = true;

                              return image;

                    }

 

                    public function placeTextField (spriteName:String, spriteX:uint, spriteY:uint, spriteWidth:uint, spriteHeight:uint, tfType:String, bkVisible:Boolean, bkColor:String): void {

                              this[spriteName] = new TextField();

                              this[spriteName].defaultTextFormat = tfFormat;

                              this[spriteName].width = spriteWidth;

                              this[spriteName].height = spriteHeight;

                              this[spriteName].background = bkVisible;

                              this[spriteName].backgroundColor = bkColor;

                              this[spriteName].border = false;

                              stage.addChild(this[spriteName]);

                              this[spriteName].x = spriteX;

                              this[spriteName].y = spriteY;

                              this[spriteName].type = tfType;

                              this[spriteName].wordWrap = true;

                    }

 

                    public function setTextFieldFormat():void {

                              tfFormat.font = "Candara";

                              tfFormat.size = 16;

                              tfFormat.color = 0x000000;

                              tfFormat.align = TextFormatAlign.LEFT;

                    }

 

                    public function setLabelFormat():void {

                              tfFormat.font = "Candara";

                              tfFormat.size = 14;

                              tfFormat.bold = true;

                              tfFormat.color = 0xFFFFFF;

                              tfFormat.align = TextFormatAlign.LEFT;

                    }

          }

}

Ned Murphy
Legend
April 23, 2014

My first suggestion to you after seeing all of that code is for you to simplify the problem and work in a separate file until you resolve the mechanics.  The simplified mechanics being to have an input textfield that you enter a value into and have a button that you click.  Have clicking that button result in a function being called that presents the converted value... in a textfield or even just as a trace.