Hi guys,
this is my first post i am haveing trouble with implementing shared object in my code, here is how far i got, i know i got mistakes, Its a simple multiple choise quiz. I just need the quiz not to be redone by the user and, when the user returns to the quiz he should see only his previous answers displayed.
here is the code :
//// VARIABLES GLOBALES PRINCIPALES (à modifier au besoin) ////
// Nom du projet (sert pour sauvegarder les données SharedObject) //
// CE NOM EST COMMUN POUR TOUS LES QUIZ //
// NE PAS UTILISER ESPACE NI ACCENT //
var PROJECT_NAME:String = 'evaluationEducateur'; // inscrire ici un nom plus approprié?
// Nom du quiz courant (sert pour sauvegarder les données SharedObject) //
// NE PAS UTILISER ESPACE NI ACCENT //
var QUIZ_NAME:String = 'rapportannuel';
// Delais d'affichage du résultat (bien joué ou oups)
var DELAIS_AFFICHAGE_RESULTAT:Number = 0.4;
// Delais d'affichage de la fenêtre éclaircissements en seconde (ex:0.75)
var DELAIS_AFFICHAGE_ECLAIRCISSEMENTS:Number = 0.75;
// Duree de l'animation 'fade' de la fenetre éclaircissements en seconde (ex:0.5)
var DUREE_FADE_FENETRE_ECLAIRCISSEMENTS:Number = 0.4;
// Nombre total de choix (peut varier: 2,3,4...)
// Il faut également ajouter autant de boutons nommés 'btn_choix1', 'btn_choix2', ...
var NB_CHOIX:int = 3;
// Bonne réponse (ex: 1, 2 ou 3)
var BONNE_REPONSE:int = 3;
// N.B. La fenêtre eclaircissements_mc peut être supprimée au besoin (optionnelle)
//// PROGRAMMATION (pas besoin de modifier) ////
// IMPORTS
import fl.transitions.Tween;
stop();
init();
// accéder aux données sauvegardées sharedObject
var userSavedAnswer:Object = loadData();
if( userSavedAnswer )
{
// modifier les délais d'animation des résultats
DELAIS_AFFICHAGE_RESULTAT = 0;
DELAIS_AFFICHAGE_ECLAIRCISSEMENTS = 0;
var len:int = NB_CHOIX;
for ( var i:int = 0 ; i < len ; i++ )
{
// évaluation des résultats
var questionId:int = i+1;
var ChoixButton:Object = this.choixButtons*;
var savedAnswer:int = userSavedAnswer[questionId];
var goodAnswer:int = BONNE_REPONSE[questionId];
// enregistrer les résultats aux objets
//dropButton.dragItemId = savedAnswer;
//var dragButton:Object = this.dragButtons[savedAnswer-1];
//dragButton.dropItemId = questionId;
}
_onClickValidezBtn(null);
}
// Initialisation de l'application
function init():void
{
this.selectionCourante = 0;
// Initialication du bouton valider
initBtnValidez( btn_validez );
btn_validez.setInteractive( true );
// Initialisation des boutons choix
this.choixButtons = new Array();
for( var iChoix:int = 1 ; iChoix <= NB_CHOIX ; iChoix++ )
{
var vue:MovieClip = this['btn_choix'+iChoix];
initChoixButton( vue, iChoix );
vue.setInteractive( true );
this.choixButtons.push( vue );
}
if( this['eclaircissements_mc'] is MovieClip )
initEclaircissement( this['eclaircissements_mc'] );
}
// Initialisation d'un bouton
function initBtnValidez( vue:MovieClip ):void
{
vue.setInteractive = function( bool:Boolean )
{
this.useHandCursor = bool;
this.buttonMode = bool;
if( bool )
{
this.addEventListener( MouseEvent.CLICK, _onClickValidezBtn );
this.addEventListener( MouseEvent.ROLL_OVER, _onRollOverValidezBtn );
this.addEventListener( MouseEvent.ROLL_OUT, _onRollOutValidezBtn );
}
else
{
this.removeEventListener( MouseEvent.CLICK, _onClickValidezBtn );
this.removeEventListener( MouseEvent.ROLL_OVER, _onRollOverValidezBtn );
this.removeEventListener( MouseEvent.ROLL_OUT, _onRollOutValidezBtn );
}
}
}
// Initialisation d'un bouton
function initChoixButton( vue:MovieClip, choixId:int ):void
{
vue.choixId = choixId; // intialisation dynamique de propriété
vue.setInteractive = function( bool:Boolean )
{
this.useHandCursor = bool;
this.buttonMode = bool;
if( bool )
{
this.addEventListener( MouseEvent.CLICK, _onClickChoixBtn );
this.addEventListener( MouseEvent.ROLL_OVER, _onRollOverChoixBtn );
this.addEventListener( MouseEvent.ROLL_OUT, _onRollOutChoixBtn );
}
else
{
this.removeEventListener( MouseEvent.CLICK, _onClickChoixBtn );
this.removeEventListener( MouseEvent.ROLL_OVER, _onRollOverChoixBtn );
this.removeEventListener( MouseEvent.ROLL_OUT, _onRollOutChoixBtn );
}
}
vue.scaleView = function( scale:Number )
{
this.scaleX = scale;
this.scaleY = scale;
}
}
// Initialisation du popup Éclaircissements
function initEclaircissement( vue:MovieClip ):void
{
const BTN_PREV_NAME:String = 'btn_precedent';
const BTN_NEXT_NAME:String = 'btn_suite';
var btnNames:Array = [BTN_PREV_NAME,BTN_NEXT_NAME];
// Navigation avec les fleches action
vue._onNavigateEclaircissement = function( evt:MouseEvent ):void
{
var btnName:String = evt.currentTarget.name;
var navDirection:int = btnName == BTN_PREV_NAME ? -1 : 1;
var goto:int = vue.currentFrame + navDirection;
vue.gotoAndStop( goto );
vue[BTN_PREV_NAME].visible = goto > 1;
vue[BTN_NEXT_NAME].visible = goto < vue.totalFrames;
}
vue.appear = function():void
{
var fps:int = DUREE_FADE_FENETRE_ECLAIRCISSEMENTS*stage.frameRate;
this.tween = new Tween( this, 'alpha', null, 0, 1, fps, false );
this.tween.start();
this.visible = true;
}
// Initialisation des boutons
for each( var btnName:String in btnNames )
{
var btnVue:SimpleButton = vue[btnName];
btnVue.addEventListener( MouseEvent.CLICK, vue._onNavigateEclaircissement );
}
// Initialisation de la vue
vue.visible = false;
vue[BTN_PREV_NAME].visible = false;
vue[BTN_NEXT_NAME].visible = false;
}
// Un bouton choix a été cliqué
function _onClickChoixBtn( evt:MouseEvent ):void
{
var vue:MovieClip = evt.currentTarget as MovieClip;
vue.scaleView( 1 );
this.selectionCourante = vue.choixId;
for each( var btnVue:MovieClip in this.choixButtons )
btnVue.gotoAndStop( btnVue == vue ? 'selection_choisie' : 'bouton_off' );
}
// Un bouton choix a été survolé, effet grossissant
function _onRollOverChoixBtn( evt:MouseEvent ):void
{
var vue:MovieClip = evt.currentTarget as MovieClip;
vue.scaleView( 1.1 );
}
// Un bouton choix quitte le survol, effet normal
function _onRollOutChoixBtn( evt:MouseEvent ):void
{
var vue:MovieClip = evt.currentTarget as MovieClip;
vue.scaleView( 1 );
}
// Le bouton validez est cliqué
function _onClickValidezBtn( evt:MouseEvent ):void
{
// Pas de reponse selectionnée
if( !this.selectionCourante )
{
this.messages_mc.gotoAndStop('pas_de_reponse');
return;
}
// Empecher interactivité
btn_validez.setInteractive( false );
btn_validez.gotoAndStop('dimmed');
for each( var vue:MovieClip in this.choixButtons )
vue.setInteractive( false );
if(DELAIS_AFFICHAGE_RESULTAT)
{
var delaisInMilliseconds:int = DELAIS_AFFICHAGE_RESULTAT*1000;
this.timeoutId = setTimeout( afficherResultat, delaisInMilliseconds ); // creation propriete dynamique
}
else
afficherResultat();
}
function afficherResultat():void
{
// Vérification si la réponse est bonne
var estBon:Boolean = this.selectionCourante == BONNE_REPONSE;
this.messages_mc.gotoAndStop( estBon ? 'bonne_reponse' : 'mauvaise_reponse' );
for each( var vue:MovieClip in this.choixButtons )
{
vue.setInteractive( false );
var estBonneReponse:Boolean = vue.choixId == BONNE_REPONSE;
var estSelection:Boolean = vue.choixId == this.selectionCourante;
var goto:String;
this.userAnswer[ vue.choixId ] = estBonneReponse;
}
// sauvegarder le résultats en shared object
if( !userSavedAnswer )
saveData( this.userAnswer );
if( estBonneReponse )
goto = estSelection ? 'bonne_reponse_choisie' : 'bonne_reponse_pas_choisie' ;
else
goto = estSelection ? 'mauvaise_reponse_choisie' : 'mauvaise_reponse_pas_choisie' ;
vue.gotoAndStop( goto );
}
if( this['eclaircissements_mc'] is MovieClip )
{
var delaisInMilliseconds:int = DELAIS_AFFICHAGE_ECLAIRCISSEMENTS*1000;
this.timeoutId = setTimeout( afficherEclaircissements, delaisInMilliseconds ); // creation propriete dynamique
}
}
function afficherEclaircissements():void
{
clearTimeout( this.timeoutId );
this['eclaircissements_mc'].appear();
}
// Le bouton validez a été survolé, effet grossissant
function _onRollOverValidezBtn( evt:MouseEvent ):void
{
var vue:MovieClip = evt.currentTarget as MovieClip;
vue.gotoAndStop('over');
}
// Le bouton validez quitte le survol, effet normal
function _onRollOutValidezBtn( evt:MouseEvent ):void
{
var vue:MovieClip = evt.currentTarget as MovieClip;
vue.gotoAndStop('off');
}
// Retourne boolean element est la liste
function estDansListe( lst:Array, element:int ):Boolean
{
for each( var elem:int in lst )
if( elem == element )
return true;
return false;
}
//// MÉTHODE DE SHAREDOBJECT À COPIER PARTOUT ////
function loadData():*
{
var so:SharedObject = SharedObject.getLocal(PROJECT_NAME);
return so.data[QUIZ_NAME];
}
function saveData( data:* ):void
{
var so:SharedObject = SharedObject.getLocal(PROJECT_NAME);
so.data[QUIZ_NAME] = data;
}
function flushAllData():void
{
var so:SharedObject = SharedObject.getLocal(PROJECT_NAME);
so.flush();
so.clear();
}