« Flash AS3 - Programmer avec une classe » : différence entre les versions

De EduTech Wiki
Aller à la navigation Aller à la recherche
mAucun résumé des modifications
Ligne 79 : Ligne 79 :
public function MyStage()
public function MyStage()
{
{
                      //Execute the existing code of MovieClip Component written by Adobe developers
super();
super();
trace("MyStage constructor called");
trace("MyStage constructor called");

Version du 16 décembre 2011 à 12:18

Cet article est une ébauche à compléter. Une ébauche est une entrée ayant un contenu (très) maigre et qui a donc besoin d'un auteur.

<pageby nominor="false" comments="false"/>

Ce article montre comment travailler avec une ou plusieurs classes dans CS5

Exemple de sensibilisation

Exemple:

Les fichiers

Dans le cas le plus simple, on procède de la façon suivante:

(1) On crée un fichier *.fla

  • Exemple: MyStage.fla

Dans ce fichier on définit une classe associée par défault

  • Exemple: MyStage

Ensuite, dans les properties, il faut rentrer ce nom de class.

Finalement, si vous pensez utiliser des composants, ils doivent se trouver dans la library. Importer les packages dans le code AS3 ne suffit pas !!!

Voici une copie d'écran:

Fichier *.fla avec définition de classe par défault et un composant

(2) On crée un fichier *.as qui a exactement le nom de la classe

(donc respecter la casse par exemple)

  • Exemple: MyStage.as

La classe

Une classe doit se trouver dans un paquetage (package) et il faut définir un constructeur, c-a-d une méthode qui sera appelée lorsque Flash crée une instance de cette classe. Cela se fait automatiquement quand le fichier swf fait avec les *.fla est chargé.


/*
 * A package represents a hierarchical directory structure used to organize the class files. 
 * This class is located in the same directory and we don't need to define a package name
 * All class definitions must begin with the definition of the package to which the class belongs.
*/
package 
{
	/*
	 * Import the libraries that will be used inside the class
	 */
	import flash.display.MovieClip;
	import flash.display.Stage;
	import fl.controls.Button;
	import flash.display.Shape;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import MyCircle;

	/*
	 * Because this class is the document class of our main .fla file it has to extend the Sprite 
	 * or the MovieClip class. By extending the MovieClip class it will inherit(have access to) 
	 * all the properties(fields) and methods defined in the MovieClip (or Sprite) class.   
	*/
	public class MyStage extends MovieClip
	{

		// Define a Button component of this class
		private var my_btn:Button;

		/*
		 * This is a special method, called a contructor. It must have the same name as the class and it be 
		 * automatically called when we publsh the .fla file
		 *
		*/
		public function MyStage()
		{
                       //Execute the existing code of MovieClip Component written by Adobe developers 
			super();
			trace("MyStage constructor called");

			// Create a button and add an event handler
			intializeInterface();
			// add the newly created button to the stage
			addChild(my_btn);
		}

		/*
		* Perform the creation and the initialization of the button
		*
		*/
		public function intializeInterface():void
		{

			my_btn   = new Button();
			my_btn.x = stage.stageWidth - 140;
			my_btn.y = stage.stageHeight - 52;
			my_btn.width = 130;
			my_btn.height = 50;
			my_btn.label = "Create circle";
			my_btn.addEventListener(MouseEvent.CLICK, btnClick);
			
			function btnClick(e:MouseEvent)
			{
				// create a new circle each time the button is pressed
				createCircle();
			}

		}

		/*
		*  Handle the creation of new circles
		*  
		*/
		public function createCircle():void
		{
			var some_width = stage.stageWidth-80;
			var some_height = stage.stageHeight-80;
			// compute x and y coordinated of the new circle
			var xCoord:Number = (Math.round(Math.random()*some_width));
			var yCoord:Number = (Math.round(Math.random()*some_height));

			// create a new object(i.e. a new instance) of the MyCircle class. 
			// The two parameters, namely xCoord and yCoord will be passed to the 
			//contructor of the class
			// The MyCircle class is defined in file MyCircle.as
			var c:MyCircle = new MyCircle(xCoord,yCoord);

			// add the circle to the stage
			addChild(c);
		}
	}
}

Importer une autre classe

Bien qu'il soit possible d'écrire pas mal de code dans la classe de défaut, on modularise normalement le code. Dans le code ci-dessous on importe la classe MyCircle.

MyCircle possède une méthode de construction prend deux paramètres, par exemple:

 MyCircle (10,100)

Voici le code:

/*
* If a class is not defined in a diferent directory, 
* we don't need to define a package name, so the package name
* can be left blank.
*/

package {

	/*
	 * Import the libraries that will be used inside the class
	 */
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.geom.ColorTransform;
	
		
	public class MyCircle extends Sprite {
		
		/*
		* Here we define the properties(fields) of the class. These properties will have different values
		* for each new object instance of this class.
		*/
		// Holder of the circle
		var circle:Sprite = new Sprite();
		// Color of the inner filling of the circle
		var randColor:ColorTransform = new ColorTransform();
		// Color of the border of the circle
		var borderColor:ColorTransform = new ColorTransform();

		/*
		 * This is a special method, called a contructor. It must have the same name as the class and it be 
		 * automatically called each time we create a new instance of this class.
		 *
		*/
		public function MyCircle(xCoord:Number, yCoord:Number) {
			trace("MyCircle constructor called");
			// x coordinate of the circle
			circle.x = xCoord;
			// y coordinate of the circle
			circle.y = yCoord;
			createCircle();
		}
		
		
		/*
		* Draw the circle and add it to the stage.
		*/
		public function createCircle():void {
			
			circle.transform.colorTransform = randColor;
			circle.graphics.lineStyle(1, 0x0000);
			circle.graphics.beginFill(0x0000FF, Math.random()); // random Alpha
			circle.graphics.drawCircle((Math.round((Math.random()*140) - 50)),
								(Math.round((Math.random()*140) - 50)),
								(Math.round((Math.random()*140) - 50)));
			randColor.redOffset = Math.round(Math.random() * 480) - 240;
			randColor.greenOffset = Math.round(Math.random() * 480) - 240;
			randColor.blueOffset = Math.round(Math.random() * 480) - 240;
			borderColor.redOffset = Math.round(Math.random() * 240) - 120;
			borderColor.greenOffset = Math.round(Math.random() * 240) - 120;
			borderColor.blueOffset = Math.round(Math.random() * 240) - 120;
			addChild(circle);
		}
	}
}

Utiliser des symboles dans une classe

Il est tout à fait possible de combiner une utilisation "Flash designer" avec une logique d'informaticien.

  • Dans le fichier *.fla vous devez exporter tous les clips de la library que vous pensez utiliser
  • Ensuite, dans une classe vous les utilisez comme d'habitude:
obj = new MyClasse;

Lire: