So yeah, I got a bit bored today and tried to make an arena rating calculator in Flex3.
Latest build (click)Build from below (click)Source code:
Code:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
private var rating:Number;
private var points:Number;
// Calculate the points you will get for a certain rating
private function CalculatePoints(percentage:Number, ratingInput:Number):Number
{
// if the rating is below 1500 use this formula
if (ratingInput < 1500)
{
points = Math.round((ratingInput * .22 + 14) * percentage);
return points;
}
// if the rating is over 1500 use this forumula
else
{
points = Math.round((1511.26 /(1 + (1639.28 * Math.pow(2.71828 , (-0.00412 * ratingInput))))) * percentage);
return points;
}
}
// calculate the rating needed for each bracket to gain certain points
private function CalculateRating(percentage:Number, pointsInput:Number):Number
{
// formula to go from points to rating
rating = Math.round(Math.ceil(Math.log((1511.26 / (1639.28 * pointsInput / percentage)) - ( 1 / 1639.28)) / ( -0.00412 )));
// if the rating is lower or equal to 1500 use the other forumla
// if not return the formula used first
if (rating <= 1500) {
rating = Math.round(((pointsInput / percentage)-14)/0.22);
return rating;
}
else
{
return rating;
}
}
// Calculate the ratings using the CalculateRating function
// then display the ratings in the given objects
public function ShowRatings(pointsInput:Number):void
{
ratingReqText.text = "Rating required for " + String(pointsInput) + " points:";
ratingOutput1.text = String(CalculateRating(.76, pointsInput))
ratingOutput2.text = String(CalculateRating(.88, pointsInput))
ratingOutput3.text = String(CalculateRating(1, pointsInput))
}
// Calculate the points using the CalculatePoints function
// then display the points in the given objects
public function ShowPoints(ratingInput:Number):void
{
pointsGivenText.text = "Arena points for a " + String(ratingInput) + " rated team:";
pointsOutput1.text = String(CalculatePoints(.76, ratingInput))
pointsOutput2.text = String(CalculatePoints(.88, ratingInput))
pointsOutput3.text = String(CalculatePoints(1, ratingInput))
}
]]>
</mx:Script>
<mx:Button x="222" y="14" label="calculate points" click="ShowPoints(Number(ratingInput.text))" width="125"/>
<mx:Button x="222" y="42" label="calculate rating" click="ShowRatings(Number(pointsInput.text))" width="125"/>
<mx:TextInput x="158" y="14" id="ratingInput" restrict="0-9" maxChars="4" width="55"/>
<mx:TextInput x="158" y="42" id="pointsInput" restrict="0-9" maxChars="3" width="55"/>
<mx:Label x="24" y="106" text="2v2 Bracket:" width="80"/>
<mx:Label x="24" y="132" text="3v3 Bracket:" width="80"/>
<mx:Label x="24" y="158" text="5v5 Bracket:" width="80"/>
<mx:Label x="24" y="222" text="2v2 Bracket: " width="80"/>
<mx:Label x="24" y="248" text="3v3 Bracket: " width="80"/>
<mx:Label x="24" y="274" text="5v5 Bracket:" width="80"/>
<mx:Label x="24" y="44" text="Desired arena points:"/>
<mx:Label x="24" y="16" text="Arena team rating:"/>
<mx:Label x="24" y="80" text="Rating required:" width="323" id="ratingReqText"/>
<mx:Label x="24" y="196" text="Arena points for rating:" width="323" id="pointsGivenText"/>
<mx:Label x="103" y="106" id="ratingOutput1" width="244"/>
<mx:Label x="103" y="132" id="ratingOutput2" width="244"/>
<mx:Label x="103" y="158" id="ratingOutput3" width="244"/>
<mx:Label x="103" y="222" id="pointsOutput1" width="244"/>
<mx:Label x="103" y="248" id="pointsOutput2" width="244"/>
<mx:Label x="103" y="274" id="pointsOutput3" width="244"/>
</mx:Application>
So the problem is that I want to seperate the Actionscript from the MXML file it is in now, because it will be good practice for later work.
I know how to build the seperate AS class, only now i'm stuck with the 2 show functions (showPoints and showRating) because they cant acces the labels anymore now.
So could anyone give me a handy solution for this? (my goal is to get rid of the most actionscript in the MXML file for as far as its possible)