03.07.2013 Views

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

Guide de reference du langage ActionScript 2.0 - PowWeb

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Disponibilité : <strong>ActionScript</strong> 1.0 ; Flash Player 6<br />

Paramètres<br />

name:String - Chaîne ; nom <strong>de</strong> la propriété d'objet à créer.<br />

getter:Function - Fonction appelée pour récupérer la valeur <strong>de</strong> la propriété ; ce paramètre<br />

est un objet Function.<br />

setter:Function - Fonction appelée pour définir la valeur <strong>de</strong> la propriété ; ce paramètre est<br />

un objet Function. Si vous transmettez la valeur null pour ce paramètre, la propriété est en<br />

lecture seule.<br />

Valeur renvoyée<br />

Boolean - Valeur booléenne : true si la propriété a été créée correctement ; false dans tous<br />

les autres cas.<br />

Exemple<br />

Dans l'exemple suivant, un objet comporte <strong>de</strong>ux métho<strong>de</strong>s internes, setQuantity() et<br />

getQuantity(). Une propriété, bookcount, permet d'appeler ces métho<strong>de</strong>s lorsqu'elle est<br />

définie ou récupérée. Une troisième métho<strong>de</strong>, getTitle(), renvoie une valeur en lecture<br />

seule qui est associée à la propriété bookname. Lorsqu'un script récupère la valeur <strong>de</strong><br />

myBook.bookcount, l'interpréteur <strong>ActionScript</strong> appelle automatiquement<br />

myBook.getQuantity(). Lorsqu'un script modifie la valeur <strong>de</strong> myBook.bookcount,<br />

l'interpréteur appelle myObject.setQuantity(). La propriété bookname ne spécifie pas une<br />

fonction <strong>de</strong> définition set. Les tentatives <strong>de</strong> modification bookname sont donc ignorées.<br />

function Book() {<br />

this.setQuantity = function(numBooks:Number):Void {<br />

this.books = numBooks;<br />

};<br />

this.getQuantity = function():Number {<br />

return this.books;<br />

};<br />

this.getTitle = function():String {<br />

return "Catcher in the Rye";<br />

};<br />

this.addProperty("bookcount", this.getQuantity, this.setQuantity);<br />

this.addProperty("bookname", this.getTitle, null);<br />

}<br />

var myBook = new Book();<br />

myBook.bookcount = 5;<br />

trace("You or<strong>de</strong>red "+myBook.bookcount+" copies of "+myBook.bookname);<br />

// output: You or<strong>de</strong>red 5 copies of Catcher in the Rye<br />

1028 Chapitre 2: Classes <strong>ActionScript</strong>

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!