8 Replies - 1063 Views - Last Post: 09 December 2016 - 08:06 AM

#1 charco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-November 08

SharedObject does not work

Posted 07 December 2016 - 04:50 PM

Hi there fellow travellers:

I'm having a sticky with SharedObject in a quiz program wot I have rote.

On entering the program a user is given the following:

var my_so:SharedObject;

function jumpIn(){
	attachMovie("startScreen", "startScreen", _root.getNextHighestDepth(),{_x:0,_y:24});
	if(my_so.data.player != undefined){
		trace(my_so.data.player);
		startScreen.myName.text = my_so.data.player;
	}
	else{
		my_so = SharedObject.getLocal("dataGatherer"); // defines the SharedObject
	}
}

jumpIn();



Where the start screen is a movieClip form with some input text fields. This SO is really a proof_of_concept test for me, as I don't really know how it works.

Then associated with the attached MC (still on the _root) there is the code:

startScreen.dataEntry.onRelease = function() {  // button is being pressed here
	numQ = startScreen.myQ.text;
	//count=numQ;
	me = startScreen.myName.text;
	myMail = startScreen.myEmail.text;
	if (startScreen.myName.text == "" || startScreen.myQ.text == "" || startScreen.myEmail.text == "") {
		display.htmlText = "Please enter your name, your email address and the number of questions you wish to answer.";
	} else if (numQ<1) {
		display.htmlText = "You must select between 1 and 50 questions";
	} else {
		my_so.data.player = startScreen.myName.text;  // send data to sharedobject from form
		timer = numQ*90;// total time available in seconds
		initial_time = timer;
		startClock();
		display.htmlText = "Click the question button to continue "+me;
		startScreen._visible = false;
		qbtn.enabled = true;
		reset_btn.enabled = false;
		for (var k = 0; k<topics.length; k++) {
			var tarray = topics[k].slice(2, 3);
			loadArray(myQuestions[tarray]);// load up the array
		}
		randomiseArray();
	}
};



When I reload the page the SO does not fill in the form input field as required.

Any ideas where I'm going wrong:

Cheers,

Charco.

Is This A Good Question/Topic? 0
  • +

Replies To: SharedObject does not work

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: SharedObject does not work

Posted 07 December 2016 - 05:07 PM

Do you call jumpIn more than once? Because the first time you call it that if statement is going to be false and it's going to go into the else to create the shared object. It's not going to go back and set the form value. You should restructure that so it creates the SO first, and then checks if the value in it is undefined.
Was This Post Helpful? 0
  • +
  • -

#3 charco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-November 08

Re: SharedObject does not work

Posted 08 December 2016 - 06:59 AM

View PostArtificialSoldier, on 07 December 2016 - 05:07 PM, said:

Do you call jumpIn more than once? Because the first time you call it that if statement is going to be false and it's going to go into the else to create the shared object. It's not going to go back and set the form value. You should restructure that so it creates the SO first, and then checks if the value in it is undefined.



Hmmm ...

I thought that once the swf had played once the SharedObject.data was then available for when the swf is reloaded.

So my_so is not declared the first time around in function jumpIn()

So the welcome screen is unaltered and when the visitor fills in his name and email these then get send to the SharedObject, which then makes them available for the second visit.

Here's how I saw it:

var my_so:SharedObject; // do I need to do this? In all of the examples it wasn't done, but as its an object I thought that it should be declared??

function jumpIn(){
attachMovie("startScreen", "startScreen", _root.getNextHighestDepth(),{_x:0,_y:24}); // a welcomeScreen MC
if(my_so.data.player != undefined){
06
trace(my_so.data.player);
07
startScreen.myName.text = my_so.data.player;
08
}
09
else{
10
my_so = SharedObject.getLocal("dataGatherer"); // defines the SharedObject
11
}
12
}
13

14
jumpIn();
Was This Post Helpful? 0
  • +
  • -

#4 charco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-November 08

Re: SharedObject does not work

Posted 08 December 2016 - 07:06 AM

View PostArtificialSoldier, on 07 December 2016 - 05:07 PM, said:

Do you call jumpIn more than once? Because the first time you call it that if statement is going to be false and it's going to go into the else to create the shared object. It's not going to go back and set the form value. You should restructure that so it creates the SO first, and then checks if the value in it is undefined.



I'm sorry but the post above was not complete. It seemed to post itself when I pressed the tab!!!

I was trying to explain my logic:

var my_so:SharedObject; // do I need to do this? In all of the examples it wasn't done, but as its an object I thought that it should be declared??

function jumpIn(){
attachMovie("startScreen", "startScreen", _root.getNextHighestDepth(),{_x:0,_y:24}); // a welcomeScreen MC
if(my_so.data.player != undefined){ //if the SO is already defined (from a previous visit)
startScreen.myName.text = my_so.data.player; // put the stored player data into the input textField
}
else{
my_so = SharedObject.getLocal("dataGatherer"); // defines the SharedObject if not already defined
}
}
jumpIn();

Then later on in the start screen when the enter button is pressed:

my_so.data.player = startScreen.myName.text; // send data to SharedObject from entry Input textBox

I can't see what's wrong ...
Was This Post Helpful? 0
  • +
  • -

#5 charco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-November 08

Re: SharedObject does not work

Posted 08 December 2016 - 07:50 AM

OK I think I've got it.

It is the DATA that has to be undefined NOT the SharedObject.

Flash has to look inside the shared object to see if there is data there. to do this the shared object must first be declared. as in:

var my_so:SharedObject;

function jumpIn(){
	attachMovie("startScreen", "startScreen", _root.getNextHighestDepth(),{_x:0,_y:24});
	my_so = SharedObject.getLocal("user_data");
	if(my_so.data.player != undefined){
		startScreen.myName.text = my_so.data.player;
	}
	else{
		// do nothing here
	}
}

jumpIn();



am I right now?

Thanks for your help by the way. :balloon:

Charco
Was This Post Helpful? 0
  • +
  • -

#6 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: SharedObject does not work

Posted 08 December 2016 - 10:35 AM

That looks better. It sounds like you were assuming that creating a SharedObject object will automatically populate the data, but that's not true. You can create multiple SOs, so you use getLocal or getRemote to tell it which one you are trying to get and then the data for that will become available.
Was This Post Helpful? 0
  • +
  • -

#7 charco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-November 08

Re: SharedObject does not work

Posted 08 December 2016 - 02:02 PM

View PostArtificialSoldier, on 08 December 2016 - 10:35 AM, said:

That looks better. It sounds like you were assuming that creating a SharedObject object will automatically populate the data, but that's not true. You can create multiple SOs, so you use getLocal or getRemote to tell it which one you are trying to get and then the data for that will become available.


Hi again, I'm still battling with SharedObject, although I have managed to get some way forward.

My problem now is to use SO to tally up the number of times that a test is done, and then tell the visitor when he comes back again how many times he has tried the test and what his average score is.

So I have created a variable:

var numTests:Number;


and setting it to zero if its a new visitor.

and then everytime that a test is completed it augments:

numTests++;


and then I write to the SharedObject:

my_so.data.number_tests=numTests;


and on returning I set a textField to show it:

returnScreen.previousTests.text = my_so.data.numTests;


But it always returns "undefined" - where am I goping wrong?

Here is the relevant section:

var my_so:SharedObject;			// do I need this declaration?

function jumpIn(){	                                                             // we test to see if a visitor has been here previously by looking for sharedObject data
	my_so = SharedObject.getLocal("user_data");
	if(my_so.data.player != undefined && my_so.data.myEmail != undefined  && my_so.data.numTests != undefined){       // if the player and email have been previously entered in an earlier session
		attachMovie("returnScreen", "returnScreen", _root.getNextHighestDepth(),{_x:0,_y:24});  // attach the return screen
		me = my_so.data.player;
		number_tests =  my_so.data.numTests;
		returnScreen.myName.text = me;                                           // enter the player's name into the return screen 
		returnScreen.previousTests.text = number_tests;                         // enter the number of tests attempted previously
	}
	else{
		numTests=0;                                                                              // first time visitor initialise the noTests counter
		attachMovie("startScreen", "startScreen", _root.getNextHighestDepth(),{_x:0,_y:24});    // attach the startScreen
		startScreen.myName.text = "";
		startScreen.myEmail.text ="";
	}
}

jumpIn();

returnScreen.dataEntry.onRelease = function() {                                                         // this is the screen that will be seen by return visitors
	numQ = returnScreen.myQ.text;                                                                       // set the number of questions required 
	if(numQ==""){
		returnScreen.warningMessage.htmlText = "You must fill in the number of questions that you wish to answer";
		this.onRelease.enabled=false;
	}                                                                                                                                    
	else if (numQ<5) {
		returnScreen.warningMessage.htmlText = "You must select between 5 and 50 questions";
	} else {
		this.onRelease.enabled=true;
		timer = numQ*90;                                                                                // total time available in seconds
		initial_time = timer;
		startClock();
		display.htmlText = "Click the question button to continue "+me;
		qbtn.enabled = true;
		reset_btn.enabled = false;
		for (var k = 0; k<topics.length; k++) {
			var tarray = topics[k].slice(2, 3);
			loadArray(myQuestions[tarray]);                                                              // load up the array
		}
		randomiseArray();
		removeMovieClip(returnScreen);
	}
	
};

startScreen.dataEntry.onRelease = function() {                                                                      // data entry button pressed
	numQ = startScreen.myQ.text;
	myMail = startScreen.myEmail.text;
	if (startScreen.myName.text == "" || startScreen.myQ.text == "" || startScreen.myEmail.text == "") {            // require that all fields are filled before continuing
		display.htmlText = "Please enter your name, your email address and the number of questions you wish to answer.";
	} else if (numQ<1) {
		display.htmlText = "You must select between 1 and 50 questions";
	} else {
		my_so.data.player = startScreen.myName.text;                                                                // set the sharedObject.data.player to the input text
		my_so.data.myEmail = startScreen.myEmail.text;                                                              // set the sharedObject.data.myEmail to the input text
		my_so.flush();
		timer = numQ*90;                                                                                            // total time available in seconds
		initial_time = timer;
		startClock();
		display.htmlText = "Click the question button to continue "+me;
		qbtn.enabled = true;
		reset_btn.enabled = false;
		for (var k = 0; k<topics.length; k++) {
			var tarray = topics[k].slice(2, 3);
			loadArray(myQuestions[tarray]);// load up the array
		}
		randomiseArray();
		removeMovieClip(startScreen);   // only remove the startScreen if all fields and correct number of questions are chosen
	}
};

qbtn.onRelease = function(){                   // now ask the question
	clearRB();
	radioGroup.enabled = true;
	if (tally == numQ) {                      // tally is the questions asked so far, numQ is no. questions requested.
		end();                                // end test function call
	} else {
		removeMovieClip("formula");
		attachMovie(species[tally][1],"formula",150,{_x:display._x+(display._width/2), _y:(display._y+(display._height/2))});
		form = species[tally][0];
		display.htmlText = q[0][0]+form+' '+species[tally][4]+q[0][1];
		this.enabled = false;
		checkit.enabled = true;
		Key.addListener(keyListener);
		radioGroup.addEventListener("click",rbListener);
	}
	clearall();
};

function checkAnswer(molecular_shape) {                                                                                      // check the answer
	qbtn.enabled = true;
	if (species[tally][2] == molecular_shape) {
		var mark = "tick";
		correct++;
	} else {
		var mark = "cross";
		wrongUns.push(" "+species[tally][0]+" ("+species[tally][1]+")");                     // fill an array with all of the wrong answers
	}
	analyse();
	_root.attachMovie(mark,"themark",100,{_x:400, _y:100});
	suggestion.htmlText = "Click on the representation above for a full explanation.";
	attachMovie(species[tally][0],"theshape",200,{_x:260, _y:270});
	theshape.onRelease = function() {
		exp = _root.attachMovie(species[tally][3], "explanation", 1000, {_x:275, _y:212, _xscale:1, _yscale:1});
		scaler(explanation,1,102,0.5);
		suggestion.htmlText = "";
		explanation.onRelease = function() {
			removeMovieClip(this);
		};
	};
	tally++;
}



function clearall() {
	removeMovieClip("theshape");
	removeMovieClip("themark");
}
function init() {
	clearall();
	species = mol.sort(shuffle);
	tally = 0;
	reset.enabled = false;
	radioGroup.enabled = false;
}

init();

reset.onRelease = function() {
	for (var i = 0; i<(wrongUns.length); i++) {// clears the array
		wrongUns.pop();
	}
	this.enabled = false;
	clearall();
	total = numQ;
	tally = 0;
	correct = 0;
	analyse();
	display.htmlText = "Ready for a new test. Press the question button to start again";
	timer = numQ*90;
	startClock();
};

function clearRB() {
	radioGroup.selection.selected = null;
}

//  ------------------------  email function   ----------------------------

function end() {
	numTests++;
	my_so=SharedObject.getLocal("user_data");
	my_so.data.numTests = numTests;
	my_so.flush();
	clearInterval(countdownInterval);
	_root.attachMovie("summary","summary",_root.getNextHighestDepth(),{_x:0, _y:0});
	fillForm();
	summary.sendit.onRelease = function() {
		// send variables in form movieclip (the textfields)
		// to email PHP page which will send the mail
		summary.loadVariables("mail/submit01.php","POST");
		summary.onEnterFrame = function() {// do or show whatever you want after the form is sent, splash screen?
			//_root.nextFrame();
			removeMovieClip(summary);
		};
		reset.enabled = true;
		removeMovieClip("formula");
	};
}


any hints or nudges in the right direction?

Cheers,

Charco
Was This Post Helpful? 0
  • +
  • -

#8 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2031
  • View blog
  • Posts: 6,202
  • Joined: 15-January 14

Re: SharedObject does not work

Posted 08 December 2016 - 03:06 PM

You need to decide whether to use number_tests or numTests.
Was This Post Helpful? 0
  • +
  • -

#9 charco   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 21-November 08

Re: SharedObject does not work

Posted 09 December 2016 - 08:06 AM

View PostArtificialSoldier, on 08 December 2016 - 03:06 PM, said:

You need to decide whether to use number_tests or numTests.


now.I.feel.stupid!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1