Nicolas Pirson :
Go to my website : tatactic.be [FR] (Website about hepatitis)
nicolas.pirson.me [FR - EN] (current website)
business card - blog
[FR - EN] (just for fun)
Nicolas Pirson CV page [FR] on tatactic.be
Back to AS3 - FLASH index ... [EN]
Oui, l'exemple est très basique mais permet de mettre en évidence chez des débutants l'intérêt d'une manière de concevoir le code en flash.
Cette façon de structurer le code permet de créer des Objets totalement indépendants.
Si un fichier .fla peut déclarer "classes.ParrotSketch" dans ses propriétés comme étant la classe de base à excécuter, un appel tel que :
import classes.ParrotSketch;
var sketch:ParrotSketch = new ParrotSketch();
aura le même résultat lors de son appel dans une classe quelquonque ou dans l'ActionPane de FLASH PROFESSIONNAL.
Le fait de ne conserver que des méthodes statiques dans une classe principale peut donc à mon avis être une bonne pratique. Cette façon de coder est par ailleurs inévitable en JAVA, C# ou dans d'autres languages de POO.
1 . Empty.fla in a folder containg a folder named classes...
Properties : AS Class : classes.ParrotSketch wich is the main class
2 . classes/ParrotSketch.as
package classes { import flash.display.MovieClip; public class ParrotSketch extends MovieClip{ import classes.Person; private static var person1:Person; private static var person2:Person; private static var theDeathParrot:Person; private static var winnersArray:Array; private static var winnerMessage:String; public function ParrotSketch():void { // constructor code (the only non-static method here) super(); // implicit but I just wrote it. main(); // Call the static real main method. }; private static function main():void{ winnersArray = new Array(); winnerMessage = new String(); trace("function main() of " + ParrotSketch + " called"); person1 = new Person(); person2 = new Person(); theDeathParrot = new Person(); // 3 static instances of Person class are created as static vars in the Main class setPersonName(person1,"Michael Edward","Palin"); setPersonBirthDate(person1,new Date(1943,04,05)); setPersonName(person2,"John Marwood","Cleese"); setPersonBirthDate(person2,new Date(1939,09,27)); setPersonName(theDeathParrot,"thedeath","Parrot"); setPersonBirthDate(theDeathParrot,new Date(2542,00,01)); PresentActors(new Array(person1,person2,theDeathParrot)); makeSomebodyTalk(person2,"I want to register a complaint!"); makeSomebodyTalk(person1,"Oh, yes sir, what's the problem?"); makeSomebodyTalk(person2,"I have a problem with this parrot!"); makeSomebodyTalk(person1,"Oh, yes Sir, and what's the problem...\nOoooh wonderful Norwegian blue plumage!"); changePersonScore(person1,+1); makeSomebodyTalk(person2,"Indeed, I've no problem with the color, but I will tell you about the problem...\nObviously this parrot is death!"); changePersonScore(person2,+1); makeSomebodyTalk(person1,"This parrot is not death, he just moved!"); changePersonScore(person1,+1); makeSomebodyTalk(person2,"He didn't move! You just pushed him!"); changePersonScore(person2,+1); changePersonScore(person1,-1); makeSomebodyTalk(person1,"I didn't pushed him!"); changePersonScore(person1,-1); makeSomebodyTalk(person2,"Yes, you did!"); changePersonScore(person2,+1); makeSomebodyTalk(person1,"Oh, yes, but let me explain you. He's not death, he's resting."); changePersonScore(person1,-1); makeSomebodyTalk(person2,"Resting?"); makeSomebodyTalk(person2,"I can recognize a death parrot when I see one, and I'm looking at one just right now!"); makeSomebodyTalk(theDeathParrot,"Aaaaargh!"); changePersonScore(person2,+1); changePersonScore(theDeathParrot,+4); // Just uncomment or comment the line here above to obtain EXAQUOs or not getPersonsScore([person1,person2,theDeathParrot]) getResults([person1,person2,theDeathParrot]); }; private static function PresentActors(actors:Array):void{ trace(" * --------------------------"); for(var i:int = 0 ; i < actors.length ; i++){ trace("Person name : " + getPersonName(Person(actors[i]))); // Cast actors[i] as Person(actors[i]) to get the autocompletion on for the instance. // See below in getPersonsScore and getResults functions. Person(persons[i]).score trace("Person birthdate : " + getPersonBirthDate(Person(actors[i]))); trace(""); } trace(" * --------------------------"); }; private static function getPersonsScore(persons:Array):void{ trace(" * --------------------------"); for(var i:int = 0 ; i < persons.length ; i++){ trace("Person name : " + getPersonName(Person(persons[i])) + ". SCORE = " + Person(persons[i]).score); } trace(" * --------------------------"); }; private static function getResults(persons:Array):void{ var scores:Array = new Array(); for (var i:int = 0 ; i < persons.length ; i++){ scores.push({score:Person(persons[i]).score,name:Person(persons[i]).name}); } sortResults(scores); }; private static function sortResults(personScores:Array):void{ var hasBeenSwapped:Boolean = false; do{ hasBeenSwapped = false; for (var j:int = 1 ; j < personScores.length ; j++){ if(personScores[j].score > personScores[j-1].score){ // SWAP ARRAY IF NEEDED var highest:Object = personScores[j]; var lowest:Object = personScores[j-1]; personScores[j-1] = highest; personScores[j] = lowest; hasBeenSwapped = true; } } }while(hasBeenSwapped==true); getFirstPlaces(personScores); }; private static function getFirstPlaces(scoresOrdered:Array){ var winners:Array = new Array(scoresOrdered[0]); var winnerOrWinnersString:String = "And the winner is :\n"; var winnersString:String = new String(); for (var k:int = 0 ; k < scoresOrdered.length-1 ; k++){ if(scoresOrdered[k].score == scoresOrdered[k+1].score){ winners.push(scoresOrdered[k+1]); winnerOrWinnersString = "We have EX-EQUOS... And the winners are :\n" } } winnersArray = winners as Array; for (var i:int = 0 ; i< winnersArray.length ; i++){ winnersString += " - " + winnersArray[i].name + " with a score of : " + winnersArray[i].score.toString(); if(i < winnersArray.length-1){ winnersString += "\nand also : \n"; } } winnerMessage = winnerOrWinnersString + winnersString trace(winnerMessage); }; private static function changePersonScore(p:Person,value:int):void{ p.changeScore(value); }; private static function makeSomebodyTalk(p:Person,sentence:String):void{ trace(makePersonTalk(p,sentence)); }; private static function setPersonName(p:Person,firstNm:String,lastNm:String):void{ p.firstName = firstNm; p.lastName = lastNm; }; private static function setPersonBirthDate(p:Person,date:Date):void{ p.birthDate = date; }; private static function getPersonBirthDate(p:Person):Date{ return p.birthDate; }; private static function getPersonName(p:Person):String{ var out:String = new String(); out += p.name; return out as String; }; private static function makePersonTalk(p:Person,sentence:String):String{ return p.say(sentence) as String; }; }; }
package classes { public class Person { private var _firstname:String; private var _lastname:String; private var _birthdate:Date; private var _score:Number; private static var constructor:Class = Person; private namespace privatePerson; public namespace publicPerson; public function Person():void { // constructor code __init__(); }; private function __init__():void{ this.initScore = 0 trace("score for " + this + " has been set to ZERO by the class " + constructor); trace(publicPerson.toString() + new String(" , ") + privatePerson.toString() + " , " + new String("new Person added")) }; public function set firstName(firstname:String):void{ _firstname = firstname; }; public function changeScore(increment:int):void{ _score += increment; }; public function get score():Number{ return _score as Number; }; private function set initScore(scoreVal:Number){ _score = scoreVal; }; public function set lastName(lastname:String):void{ _lastname = lastname; }; public function set birthDate(date:Date):void{ _birthdate = date; }; public function get firstName():String{ return _firstname; }; public function get lastName():String{ return _lastname; }; public function get name():String{ return _firstname + " " + _lastname; }; public function get birthDate():Date{ return _birthdate; }; public function say(sentence:String):String{ var out:String = new String(); out += firstName.slice(0,1) + " " +lastName + " - " + '"'; out += sentence as String; out += '"'; return out as String; }; } }
// OUTPUT :
function main() of [class ParrotSketch] called score for [object Person] has been set to ZERO by the class [class Person] classes:Person/publicPerson , classes:Person/private:privatePerson , new Person added score for [object Person] has been set to ZERO by the class [class Person] classes:Person/publicPerson , classes:Person/private:privatePerson , new Person added score for [object Person] has been set to ZERO by the class [class Person] classes:Person/publicPerson , classes:Person/private:privatePerson , new Person added * -------------------------- Person name : Michael Edward Palin Person birthdate : Wed May 5 00:00:00 GMT+0200 1943 Person name : John Marwood Cleese Person birthdate : Fri Oct 27 00:00:00 GMT+0200 1939 Person name : thedeath Parrot Person birthdate : Mon Jan 1 00:00:00 GMT+0100 2542 * -------------------------- J Cleese - "I want to register a complaint!" M Palin - "Oh, yes sir, what's the problem?" J Cleese - "I have a problem with this parrot!" M Palin - "Oh, yes Sir, and what's the problem... Ooooh wonderful Norwegian blue plumage!" J Cleese - "Indeed, I've no problem with the color, but I will tell you about the problem... Obviously this parrot is death!" M Palin - "This parrot is not death, he just moved!" J Cleese - "He didn't move! You just pushed him!" M Palin - "I didn't pushed him!" J Cleese - "Yes, you did!" M Palin - "Oh, yes, but let me explain you. He's not death, he's resting." J Cleese - "Resting?" J Cleese - "I can recognize a death parrot when I see one, and I'm looking at one just right now!" t Parrot - "Aaaaargh!" * -------------------------- Person name : Michael Edward Palin. SCORE = -1 Person name : John Marwood Cleese. SCORE = 4 Person name : thedeath Parrot. SCORE = 4 * -------------------------- We have EX-EQUOS... And the winners are : - John Marwood Cleese with a score of : 4 and also : - thedeath Parrot with a score of : 4