Copy link to clipboard
Copied
Hi People,
I've been reading a lot about saving data but I can't seem to execute it. This little app allows a user to input 32 names, then click a button that removes unwanted objects from the stage and allows other objects to appear that are required. The user then clicks a button, the program associates a random number with a name and displays it. I got this working quite easy. All i want to do now is at least allow for the user to reopen a file and all the names that were input are still there. I'm assuming three is a quick easy way to achieve this?
Thanks
Source Code:
finitialise (null);
stop();
function fStartApp (evt:MouseEvent):void{
TXT_S1.visible = false;
TXT_S2.visible = false;
TXT_S3.visible = false;
TXT_S4.visible = false;
TXT_S5.visible = false;
TXT_S6.visible = false;
TXT_S7.visible = false;
TXT_S8.visible = false;
TXT_S9.visible = false;
TXT_S10.visible = false;
TXT_S11.visible = false;
TXT_S12.visible = false;
TXT_S13.visible = false;
TXT_S14.visible = false;
TXT_S15.visible = false;
TXT_S16.visible = false;
TXT_S17.visible = false;
TXT_S18.visible = false;
TXT_S19.visible = false;
TXT_S20.visible = false;
TXT_S21.visible = false;
TXT_S22.visible = false;
TXT_S23.visible = false;
TXT_S24.visible = false;
TXT_S25.visible = false;
TXT_S26.visible = false;
TXT_S27.visible = false;
TXT_S28.visible = false;
TXT_S29.visible = false;
TXT_S30.visible = false;
TXT_S31.visible = false;
TXT_S32.visible = false;
TXTGo.visible = false;
BTN_Button2.visible = false;
TXTStudentOutput.visible = true;
BTN_Button1.visible = false;
BTN_Button2.visible = true;
TXTLucky.visible = true;
}
BTN_Button1.addEventListener (MouseEvent.CLICK, fStartApp)
var nRandomNumber: Number;
function finitialise (evt:MouseEvent):void{
TXTStudentOutput.visible = false;
BTN_Button2.visible = false;
TXTLucky.visible = false;
}
function fMainProgram (evt:MouseEvent):void{
fRandom(null);
}
function fRandom (evt:MouseEvent):void{
nRandomNumber = Math.ceil(Math.random()*32);
if (nRandomNumber == 1)
TXTStudentOutput.text = (TXT_S1.text);
if (nRandomNumber == 2)
TXTStudentOutput.text = (TXT_S2.text);
if (nRandomNumber == 3)
TXTStudentOutput.text = (TXT_S3.text);
if (nRandomNumber == 4)
TXTStudentOutput.text = (TXT_S4.text);
if (nRandomNumber == 5)
TXTStudentOutput.text = (TXT_S5.text);
if (nRandomNumber == 6)
TXTStudentOutput.text = (TXT_S6.text);
if (nRandomNumber == 7)
TXTStudentOutput.text = (TXT_S7.text);
if (nRandomNumber == 8)
TXTStudentOutput.text = (TXT_S8.text);
if (nRandomNumber == 9)
TXTStudentOutput.text = (TXT_S9.text);
if (nRandomNumber == 10)
TXTStudentOutput.text = (TXT_S10.text);
if (nRandomNumber == 11)
TXTStudentOutput.text = (TXT_S11.text);
if (nRandomNumber == 12)
TXTStudentOutput.text = (TXT_S12.text);
if (nRandomNumber == 13)
TXTStudentOutput.text = (TXT_S13.text);
if (nRandomNumber == 14)
TXTStudentOutput.text = (TXT_S14.text);
if (nRandomNumber == 15)
TXTStudentOutput.text = (TXT_S15.text);
if (nRandomNumber == 16)
TXTStudentOutput.text = (TXT_S16.text);
if (nRandomNumber == 17)
TXTStudentOutput.text = (TXT_S17.text);
if (nRandomNumber == 18)
TXTStudentOutput.text = (TXT_S18.text);
if (nRandomNumber == 19)
TXTStudentOutput.text = (TXT_S19.text);
if (nRandomNumber == 20)
TXTStudentOutput.text = (TXT_S20.text);
if (nRandomNumber == 21)
TXTStudentOutput.text = (TXT_S21.text);
if (nRandomNumber == 22)
TXTStudentOutput.text = (TXT_S22.text);
if (nRandomNumber == 23)
TXTStudentOutput.text = (TXT_S23.text);
if (nRandomNumber == 24)
TXTStudentOutput.text = (TXT_S24.text);
if (nRandomNumber == 25)
TXTStudentOutput.text = (TXT_S25.text);
if (nRandomNumber == 26)
TXTStudentOutput.text = (TXT_S26.text);
if (nRandomNumber == 27)
TXTStudentOutput.text = (TXT_S27.text);
if (nRandomNumber == 28)
TXTStudentOutput.text = (TXT_S28.text);
if (nRandomNumber == 29)
TXTStudentOutput.text = (TXT_S29.text);
if (nRandomNumber == 30)
TXTStudentOutput.text = (TXT_S30.text);
if (nRandomNumber == 31)
TXTStudentOutput.text = (TXT_S31.text);
if (nRandomNumber == 32)
TXTStudentOutput.text = (TXT_S32.text);
}
BTN_Button2.addEventListener(MouseEvent.CLICK,fMainProgram);
Hi.
Here is a suggestion:
...import flash.net.SharedObject;
import flash.text.TextField;
function fStartApp(evt:MouseEvent):void
{
TXT_S1.visible = false;
TXT_S2.visible = false;
TXT_S3.visible = false;
TXT_S4.visible = false;
TXT_S5.visible = false;
TXT_S6.visible = false;
TXT_S7.visible = false;
TXT_S8.visible = false;
TXT_S9.visible = false;
TXT_S10.visible = false;
TXT_S11.visible = false;
TXT_S12.visible = false;
TXT_S13.visible = false;
TXT_S14.visible = f
Copy link to clipboard
Copied
Hi.
Here is a suggestion:
import flash.net.SharedObject;
import flash.text.TextField;
function fStartApp(evt:MouseEvent):void
{
TXT_S1.visible = false;
TXT_S2.visible = false;
TXT_S3.visible = false;
TXT_S4.visible = false;
TXT_S5.visible = false;
TXT_S6.visible = false;
TXT_S7.visible = false;
TXT_S8.visible = false;
TXT_S9.visible = false;
TXT_S10.visible = false;
TXT_S11.visible = false;
TXT_S12.visible = false;
TXT_S13.visible = false;
TXT_S14.visible = false;
TXT_S15.visible = false;
TXT_S16.visible = false;
TXT_S17.visible = false;
TXT_S18.visible = false;
TXT_S19.visible = false;
TXT_S20.visible = false;
TXT_S21.visible = false;
TXT_S22.visible = false;
TXT_S23.visible = false;
TXT_S24.visible = false;
TXT_S25.visible = false;
TXT_S26.visible = false;
TXT_S27.visible = false;
TXT_S28.visible = false;
TXT_S29.visible = false;
TXT_S30.visible = false;
TXT_S31.visible = false;
TXT_S32.visible = false;
TXTGo.visible = false;
BTN_Button2.visible = false;
TXTStudentOutput.visible = true;
BTN_Button1.visible = false;
BTN_Button2.visible = true;
TXTLucky.visible = true;
save(getTexts);
}
function finitialise(evt:MouseEvent):void
{
TXTStudentOutput.visible = false;
BTN_Button2.visible = false;
TXTLucky.visible = false;
}
function fMainProgram(evt:MouseEvent):void
{
fRandom(null);
}
function fRandom(evt:MouseEvent):void
{
nRandomNumber = Math.ceil(Math.random() * 32);
if (nRandomNumber == 1)
TXTStudentOutput.text = (TXT_S1.text);
if (nRandomNumber == 2)
TXTStudentOutput.text = (TXT_S2.text);
if (nRandomNumber == 3)
TXTStudentOutput.text = (TXT_S3.text);
if (nRandomNumber == 4)
TXTStudentOutput.text = (TXT_S4.text);
if (nRandomNumber == 5)
TXTStudentOutput.text = (TXT_S5.text);
if (nRandomNumber == 6)
TXTStudentOutput.text = (TXT_S6.text);
if (nRandomNumber == 7)
TXTStudentOutput.text = (TXT_S7.text);
if (nRandomNumber == 8)
TXTStudentOutput.text = (TXT_S8.text);
if (nRandomNumber == 9)
TXTStudentOutput.text = (TXT_S9.text);
if (nRandomNumber == 10)
TXTStudentOutput.text = (TXT_S10.text);
if (nRandomNumber == 11)
TXTStudentOutput.text = (TXT_S11.text);
if (nRandomNumber == 12)
TXTStudentOutput.text = (TXT_S12.text);
if (nRandomNumber == 13)
TXTStudentOutput.text = (TXT_S13.text);
if (nRandomNumber == 14)
TXTStudentOutput.text = (TXT_S14.text);
if (nRandomNumber == 15)
TXTStudentOutput.text = (TXT_S15.text);
if (nRandomNumber == 16)
TXTStudentOutput.text = (TXT_S16.text);
if (nRandomNumber == 17)
TXTStudentOutput.text = (TXT_S17.text);
if (nRandomNumber == 18)
TXTStudentOutput.text = (TXT_S18.text);
if (nRandomNumber == 19)
TXTStudentOutput.text = (TXT_S19.text);
if (nRandomNumber == 20)
TXTStudentOutput.text = (TXT_S20.text);
if (nRandomNumber == 21)
TXTStudentOutput.text = (TXT_S21.text);
if (nRandomNumber == 22)
TXTStudentOutput.text = (TXT_S22.text);
if (nRandomNumber == 23)
TXTStudentOutput.text = (TXT_S23.text);
if (nRandomNumber == 24)
TXTStudentOutput.text = (TXT_S24.text);
if (nRandomNumber == 25)
TXTStudentOutput.text = (TXT_S25.text);
if (nRandomNumber == 26)
TXTStudentOutput.text = (TXT_S26.text);
if (nRandomNumber == 27)
TXTStudentOutput.text = (TXT_S27.text);
if (nRandomNumber == 28)
TXTStudentOutput.text = (TXT_S28.text);
if (nRandomNumber == 29)
TXTStudentOutput.text = (TXT_S29.text);
if (nRandomNumber == 30)
TXTStudentOutput.text = (TXT_S30.text);
if (nRandomNumber == 31)
TXTStudentOutput.text = (TXT_S31.text);
if (nRandomNumber == 32)
TXTStudentOutput.text = (TXT_S32.text);
}
function setTexts(texts:Array):void
{
for (var i:int = 0, total:int = numChildren; i < total; i++)
{
if (getChildAt(i) is TextField && getChildAt(i).name.indexOf("TXT_S") == 0)
(getChildAt(i) as TextField).text = texts;
}
}
function getTexts():void
{
if (!sharedObject.data.texts)
sharedObject.data.texts = [];
for (var i:int = 0, total:int = numChildren; i < total; i++)
{
if (getChildAt(i) is TextField && getChildAt(i).name.indexOf("TXT_S") == 0)
sharedObject.data.texts = (getChildAt(i) as TextField).text;
}
}
function load(file:String, dataProperty:String, func:Function):void
{
sharedObject = SharedObject.getLocal(file);
if(sharedObject.data[dataProperty])
func(sharedObject.data[dataProperty]);
}
function save(func:Function):void
{
func();
sharedObject.flush();
}
var sharedObject:SharedObject;
var nRandomNumber:Number;
stop();
BTN_Button1.addEventListener(MouseEvent.CLICK, fStartApp)
BTN_Button2.addEventListener(MouseEvent.CLICK, fMainProgram);
finitialise(null);
load("save", "texts", setTexts);
I hope it helps.
Regards,
JC
Copy link to clipboard
Copied
Thanks I really appreciate your help. It does not look a simple as I thought it would be. I'm hoping I can bring myself to understand how you went about that. Unfortunately I get an error in my output:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at RandomStudents_fla::MainTimeline/getTexts()
at RandomStudents_fla::MainTimeline/save()
at RandomStudents_fla::MainTimeline/frame1()
As I don't understand the way the code functions yet, can you please point me in the right direction to go next?
Thanks
Copy link to clipboard
Copied
Great!
The error is saying that the getTexts() function cannot access an object, a property or method. Probably it has something to do with the shared object.
Would you mind sharing your FLA?
Thanks,
JC
Copy link to clipboard
Copied
I can, but not sure how to attach.
Copy link to clipboard
Copied
Google Drive:
- Go to drive.google.com;
- Click on the NEW button;
- Click on 'File upload';
- Select your file;
- Wait for the upload to finish;
- Once uploaded, right click on the file and select 'Share...';
- Click on 'Get shareable link';
- Click on 'Copy link';
- Click on 'Done';
- Then just paste the link here.
WeTransfer:
- Go to wetransfer.com;
- Click on 'Take me to free';
- Click on 'I agree';
- Click on the '...' button;
- Choose to 'Send as link';
- Click on 'Add your files';
- Click on 'Transfer';
- Wait for the upload;
- Click on 'Copy link';
- Then just paste the link here.
Please let me know if you have any problem.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Please forgive the GUI it's a skeleton version for now.
Copy link to clipboard
Copied
Thank you for sharing the file. And don't worry about the layout.
The problem is that the save(getTexts) function is being called outside the fStartApp function body. Just put the save(getTexts) function inside the fStartApp function body and everything should work.
Please let me know if it worked.
Regards,
JC
Copy link to clipboard
Copied
Fantastic!! Thanks. Now I've just got to understand the code for myself .
Copy link to clipboard
Copied
This is great!
Here are some links to help you understand better the concept of save and load using the SharedObject class in AS3:
AS3: SharedObject Class (Flash Cookies)
Understanding AS3 shared objects – Emanuele Feronato
https://as3gametuts.com/2012/02/28/actionscript-saving-and-loading-tutorial/
SharedObject - Adobe ActionScript® 3 (AS3 ) API Reference
Adobe ActionScript 3.0 * Storing local data
Regards,
JC
Copy link to clipboard
Copied
Thanks! I really appreciate it. I have one more question. I want to now create a title keyframe where the user can select different groups of students (classes). It is easy enough to make a movie clip goto another frame on a mouse click but can the keyframe with all the instances, objects and AS3 be duplicated whilst changing instance names and as3 functions in bulk without going through and changing everything individually?
Copy link to clipboard
Copied
All good I've found an easier way by changing the file "TXT_S" that it writes to and linking to multipe Swf's rather than spen hours changing instance names ect..
Copy link to clipboard
Copied
This only works with one version of the app. I would like to have multiple duplicates so the user can input several different class's of names. Ive tried changing properties of the "TXT_S" as well as changing the instance names on the duplicate fla but both fla files still influence each other. Please help.
Thanks
Copy link to clipboard
Copied
Copy link to clipboard
Copied
Hi!
Please notice that you can change the name of the file that will contain the saved data and also the key to store the data in this file.
Currently, we have the
load("save", "texts", setTexts);
function.
The first argument being passed is the file where we store the data.
The second argument being passed is the key inside of the file where we store the data.
In your case, you can give different names to these arguments for each app so you'll won't get a conflict.
Please let me know if it works for you.
Copy link to clipboard
Copied
I just changed the "save" to "save1", "save2"..etc..and it now works a treat thank you! It won't allow me to change the "texts" as it simply would load a blank swf and not load the text. Seems to work without changing "texts" at all.
Thanks heaps!
Copy link to clipboard
Copied
You're welcome!
Find more inspiration, events, and resources on the new Adobe Community
Explore Now