Classe Utilitaire Math : nombre aléatoire, conversion degrés radians…
Dans la majorité de nos applications et jeux flash, des opérations mathématiques identiques reviennent très souvent !
Plutôt que de recoder ces fonctions à chaque fois, il est préférable de simplifier cela.
Il existe une classe utilitaire de Math en ActionScript qui regroupent quelques fonctionnalités bien pratiques.
Voici le détail de la classe MathUtils :
- Permet de convertir des radians en degrés.
- Permet de convertir des degrés en radians.
- Fournit un nombre aléatoire de type int entre 2 valeurs.
- Fournit un nombre aléatoire de type Number entre 2 valeurs.
Exemple MathUtils
Voici un exemple de mise en pratique de la classe MathUtils.
Il s’agit, à partir d’un nombre aléatoire de type int, de le convertir de degrés en radians.
Et avec un nombre aléatoire de type Number, de le convertir de radians en degrés.
/**
The Initial Developer of the Original Code is
Matthieu - https://www.actionscript-facile.com
Portions created by the Initial Developer are Copyright (C) 2010
the Initial Developer. All Rights Reserved.
Contributor(s) :
*/
package
{
import net.richardlord.utils.MathUtils;
import flash.events.MouseEvent;
import com.as3facile.DefaultButton;
import com.actionscriptfacile.ui.text.UITextField;
import flash.events.Event;
import flash.display.Sprite;
import com.demonsters.debugger.MonsterDebugger;
public class Main extends Sprite
{
private var oTxtResult1 : UITextField;
private var oTxtResult2 : UITextField;
/**
* Constructeur.
*
*/
public function Main()
{
// attend la fin de l'initialisation de la scène
this.addEventListener(Event.ADDED_TO_STAGE, onReady, false, 0, true);
}
/**
* Démarrage de l'application.
* définit le nombre d'images par secondes dans les paramètres de compilation
* -default-frame-rate 40
*
*/
private function onReady(event : Event) : void
{
this.removeEventListener(Event.ADDED_TO_STAGE, onReady);
// Init De MonsterDebugger
MonsterDebugger.initialize(this);
MonsterDebugger.trace(this, "Start Application");
var oTxtDegRad:UITextField = this.addText("1- Conversion Degrés en Radians d'un nombre (int) aléatoire.");
oTxtDegRad.x = 20;
oTxtDegRad.y = 20;
this.addChild(oTxtDegRad);
var oTxtRadDeg:UITextField = this.addText("2- Conversion Radians en Degrés d'un nombre (Number) aléatoire.");
oTxtRadDeg.x = oTxtDegRad.x;
oTxtRadDeg.y = oTxtDegRad.y + 40;
this.addChild(oTxtRadDeg);
var oBtnStart:DefaultButton = new DefaultButton();
oBtnStart.getObject().label = "C'est Parti !";
oBtnStart.getObject().x = oTxtDegRad.x;
oBtnStart.getObject().y = oTxtRadDeg.y + 50;
oBtnStart.getObject().addEventListener ( MouseEvent.CLICK, onStartGame );
this.addChild(oBtnStart);
oTxtResult1 = this.addText("En attente...");
oTxtResult1.x = oTxtDegRad.x;
oTxtResult1.y = oTxtRadDeg.y + 100;
this.addChild(oTxtResult1);
oTxtResult2 = this.addText("En attente...");
oTxtResult2.x = oTxtDegRad.x;
oTxtResult2.y = oTxtResult1.y + 50;
this.addChild(oTxtResult2);
}
/**
* Nouvelle partie.
*
*/
private function onStartGame(event : MouseEvent) : void
{
var nVal1 : int = MathUtils.randomInt(1, 234);
var nResRad:Number = MathUtils.asRadians(nVal1);
var sStr1:String = "1- Nombre int aléatoire (entre 1 et 234) : "+nVal1+" degrés.\nCe qui donne : "+nResRad+" radians.";
oTxtResult1.text = sStr1;
var nVal2 : Number = MathUtils.randomNumber(0.34, 13.4);
var nDeg:Number = MathUtils.asDegrees(nVal2);
var sStr2:String = "2- Nombre Number aléatoire (entre 0.34 et 13.4) : "+nVal2+" radians.\nCe qui donne : "+nDeg+" degrés.";
oTxtResult2.text = sStr2;
}
/**
* Création d'un champ texte.
*
*/
private function addText(sValue:String) : UITextField
{
var oTxtField:UITextField = new UITextField();
oTxtField.text = sValue;
oTxtField.selectable = false;
oTxtField.autoSizeLeft();
oTxtField.alignLeft(); // centre le texte
oTxtField.changeFormat("size", 13);// changement de la taille de la police du texte
oTxtField.changeFormat("font", "Arial");// changement de la taille de la police du texte
oTxtField.changeFormat("color", 0X006f77);// changement de la couleur
return oTxtField;
}
}
}
/*
* Author: Richard Lord
* Copyright (c) Richard Lord 2007
* http://www.richardlord.net/
*
* Licence Agreement (The MIT License)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package net.richardlord.utils
{
/**
* The Maths class contains a coupleof useful methods for use in maths functions.
*/
public class MathUtils
{
private static const RADTODEG:Number = 180 / Math.PI;
private static const DEGTORAD:Number = Math.PI / 180;
/**
* Converts an angle from radians to degrees
*
* @param radians The angle in radians
* @return The angle in degrees
*/
public static function asDegrees( radians:Number ):Number
{
return radians * RADTODEG;
}
/**
* Converts an angle from degrees to radians
*
* @param radians The angle in degrees
* @return The angle in radians
*/
public static function asRadians( degrees:Number ):Number
{
return degrees * DEGTORAD;
}
/**
* Returns a random integer between two values.
*
* @param min The lowest value to return.
* @param max The highest value to return.
*
* @return A random integer between min and max inclusive.
*/
public static function randomInt( min:int, max:int ):int
{
return min + int( Math.random() * ( max - min + 1 ) );
}
/**
* Returns a random number between two values.
*
* @param min The lowest value to return.
* @param max The highest value to return.
*
* @return A random number between min and max inclusive.
*/
public static function randomNumber( min:Number, max:Number ):Number
{
return min + Math.random() * ( max - min );
}
}
}
SWF Démonstration MathUtils
Et voici le swf basique pour illustrer l’utilisation de cette classe Mathématiques Utilitaires.
https://www.actionscript-facile.com/wp-content/uploads/2012/02/MathUtils.swfTélécharger le code source MathUtils
Visiter le site de l’auteur : Richard Lord
Télécharger “Classe MathUtils” ex-mathutils.zip – Téléchargé 191 fois – 89 Ko
Utilisez-vous des classes utilitaires pour vous simplifier la programmation ActionScript ?
Partagez-les avec les Développeurs AS3 Facile, via les commentaires ci-dessous.
Développez des Jeux et Applications Flash avec la Formation AS3 Facile !
Recevez Gratuitement Des Cours en Vidéos, des codes source et des livrets de formation.
Et en bonus gratuit : Le Framework AS3 Facile !