# How to improve this code further better?

**URL:** <https://forum.kirupa.com/t/how-to-improve-this-code-further-better/307444>\
**Category:** Uncategorized\
**Created:** [April 3, 2010, 11:50am UTC](https://forum.kirupa.com/t/how-to-improve-this-code-further-better/307444 "2010-04-03T11:50:50Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![alanpras](https://avatars.discourse-cdn.com/v4/letter/a/f6c823/32.png) [@alanpras](https://forum.kirupa.com/u/alanpras)\
**Post date:** [April 3, 2010, 11:50am UTC](https://forum.kirupa.com/t/how-to-improve-this-code-further-better/307444/1 "2010-04-03T11:50:50Z")

</div>

Hi guys I am a beginner in AS 3.0, I have done the scripting for  
There are 4 input text boxex in the stage, 1 Submit button, and there are three MC of equal width of different colors says for ex red\_mc, blue\_mc, green\_mc. Now when the end user enters 4 values (in ascending order) in the input box and press the submit button program has to check for the difference between four values that is val2-val1, val3-val2, val4-val3 from these three results the 3 MC should scale accordingly that if u enter 10,25,40,150 then result is 15, 15, 110 so the red\_mc should scale only 15%, blue\_mc should scale 15% and green\_mc has to scale 70%… this was the concept, there is a rest button too, and value 5is for movig the pointer to the MC which will scale larger I created a code by when the person saw this code he told that the logic has to be improved a lot but can some one suggest me what is wrong with this and how to improve the logic and code please, I have attached the code too

var val1:Number = mc.red\_mc.x;  
mc.x = stage.x;  
submit\_but.addEventListener(MouseEvent.CLICK, begin, false, 0, true);  
reset\_but.addEventListener(MouseEvent.CLICK, reset, false, 0, true);  
function reset(evt:MouseEvent):void  
{  
ip1\_txt.text = “”;  
ip2\_txt.text = “”;  
ip3\_txt.text = “”;  
ip4\_txt.text = “”;  
ip5\_txt.text = “”;

```
mc.red_mc.width = mc.blue_mc.width = mc.yel_mc.width = 100;

mc.red_mc.x = val1;
mc.blue_mc.x = val1 + mc.red_mc.width;
mc.yel_mc.x = mc.blue_mc.width + mc.blue_mc.x;
mc.move_mc.x = 5;

```

}

function begin(evt:MouseEvent):void  
{  
var val1:Number = parseInt(ip1\_txt.text);  
var val2:Number = parseInt(ip2\_txt.text);  
var val3:Number = parseInt(ip3\_txt.text);  
var val4:Number = parseInt(ip4\_txt.text);  
var val5:Number = parseInt(ip5\_txt.text);

```
trace(val1);
var res1:Number = (val2-val1);
var res2:Number = (val3-val2);
var res3:Number = (val4-val3);
trace(res1,res2,res3);
trace(mc.red_mc.width);
if(res1&gt;res2 && res1&gt;res3)
{
    mc.red_mc.width = 300 - (res2+res3);
    trace(mc.red_mc.width);
    mc.blue_mc.x = mc.red_mc.width;
    mc.blue_mc.width = res2;
    trace(mc.blue_mc.width);
    mc.yel_mc.x = (mc.blue_mc.width + mc.red_mc.width);
    mc.yel_mc.width = res3;
        
}
else if (res2&gt;res1 && res2&gt;res3)
{
    mc.red_mc.width = res1;
    mc.blue_mc.width = 300 - (res1+res3);
    trace(mc.red_mc.width);
    mc.blue_mc.x = mc.red_mc.width;
    trace(mc.blue_mc.width);
    mc.yel_mc.x = (mc.blue_mc.width + mc.red_mc.width);
    mc.yel_mc.width = res3;
}
else if(res3&gt;res1 && res3&gt;res2)
{
    mc.red_mc.width = res1;
    trace(mc.red_mc.width);
    mc.blue_mc.x = mc.red_mc.width;
    mc.blue_mc.width = res2;
    trace(mc.blue_mc.width);
    mc.yel_mc.x = (mc.blue_mc.width + mc.red_mc.width);
    mc.yel_mc.width = 300 - (res1 + res2);
}
if(val5&gt;val1 && val5&lt;val2)
{
    mc.move_mc.x = mc.red_mc.width /2;
}
else if(val5&gt;val2 && val5&lt;val3)
{
    mc.move_mc.x = mc.blue_mc.width / 2;
}
else if(val5&gt;val3 && val5&lt;val4)
{
    mc.move_mc.x = mc.yel_mc.width / 2;
}

```

}
