Issues with SharedObject? - actionscript-3

So, simply put, I am trying to use the SharedObject to simply hang onto a variable for me that I need to save for the user to remember an option choice. Nothing dramatic, but for some reason, no matter what I do, I cannot seem to actually create the SharedObject to interact with it in the following lines, I am running traces every step to just keep an eye out for where it is breaking, and the OBJ SET trace will run, but if I try to access the SortObject in anyway through .data, it just seems to not work? This is my first time messing with SharedObjects, and I am still pretty new to AS3 and since its an ailing platform to begin with, I cannot seem to find any reason for this to be failing? Every use of the 100 uses I have seen in action seem to use it in the same manner I do?
I am not throwing any compile errors as a result either.
From what I can tell, the SharedObject is simply not being created for some reason.
public function ChangeSortSetting(event:Event) : *
{
var SortTypeSetting = event.target.name == "SortTypeSet";
var ExtraSetting = event.target.name == "ExtraSet";
trace(" VAR SET ")
var SortObject:SharedObject = SharedObject.getLocal("SortObject")
trace(" OBJ SET? ")
if (SortObject.data.SortMethod == null)
{
SortObject.data.SortMethod = "alpha"
SortObject.flush()
}
trace(" / 1UIX / OBJ TRACE CHECK / " + SortObject.data.SortMethod)
}

You can not change the event name .
The problem is in those lines below .
var SortTypeSetting = event.target.name == "SortTypeSet";
var ExtraSetting = event.target.name == "ExtraSet";
Try changing to :
var SortTypeSetting = "SortTypeSet";
var ExtraSetting = "ExtraSet";

Related

Actionscript SharedObject data go randomly missing

I'm designing a game with save and load functions. Using the SharedObject technique I'm successfully saving most of the variables. However, there is this one Dictionary that sometimes ends up as "undefined". This happends perhaps 1/3 of the times i load the game.
This is my save function. It runs approximately every 2. second. I've removed a lot of code lines since they aren't relevant (saving the other variables). The trace line is for debugging purposes. Every time, also when the above mentioned error accurs, this line works. Consequently the dictionary isn't "undefined" at this moment.
private function saveGame():void
{
so = SharedObject.getLocal("progress", "/");
so.data.saved = true;
so.data.airportDict = airportDict;
trace(dayCount, so.data.airportDict["Australia"]);
so.flush();
}
The following lines run every time you open the program. The purpose is to decide whether there is a saved file to load or not.
so = SharedObject.getLocal("progress", "/");
if (so.data.saved == true)
{
loadProgress();
}
else
{
airportDict = new Dictionary();
resetAirportDict(); // This function just add lots of data to the dictionary.
}
And finally, the loading function:
private function loadProgress():void
{
so = SharedObject.getLocal("progress", "/");
airportDict = so.data.airportDict;
trace("Successfull start? " + airportDict);
}
As already mentioned, the airport dictionary's value is "undefined" maybe 1/3 of the time I run the program. For no appearant reason. This is a mystery.
SharedObject can store primitive types + Array and Object types. For other complex types you have to register the classes you wish to store using registerClassAlias(). In your cases Dictionary is a complex type that is not handled by default by SharedObject + the complex objects you might use as keys will have to be registered as well. So any complex class you use has to be register or else SharedObject will fail.
registerClassAlias("YourClassName", YourClassName);
This piece of code produce error because SharedObject doesn't understand complex objects:
var shareddata:SharedObject = SharedObject.getLocal("mydata")
var instance:MyClass;
if(shareddata.data["instance"] == undefined)
{
instance = new MyClass();
shareddata.data["instance"] = instance;
}
else
{
instance = shareddata.data["instance"];
}
But you can easily fix it by registering your class:
import flash.net.registerClassAlias;
registerClassAlias("MyClass", MyClass);
var shareddata:SharedObject = SharedObject.getLocal("mydata")
var instance:MyClass;
if(shareddata.data["instance"] == undefined)
{
instance = new MyClass();
shareddata.data["instance"] = instance;
}
else
{
instance = shareddata.data["instance"];
}
//no error and you truly get back your custom class instance the second time around.

ActionScript 3.0 Error #1010 - preloader function

I am a Flash and ActionScript newbie. I am trying to follow a video tutorial to make a preloader and I'm having a problem that the video didn't seem to address. I believe I have entered in all of the code correctly from the video. This is it:
stop();
addEventListener(Event.ENTER_FRAME, loaderF);
function loaderF(e:Event):void{
var toLoad:Number = loaderInfo.bytesTotal;
var loaded:Number = loaderInfo.bytesLoaded;
var total:Number = loaded/toLoad;
if( loaded == toLoad ){
removeEventListener(Event.ENTER_FRAME, loaderF);
gotoAndStop(2);
} else {
preloader_mc.preloaderFill_mc.scaleX = total;
preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
preloader_mc.ofBytes_txt.text = loaded + "bytes";
preloader_mc.totalBytes_txt.text = toLoad + "bytes";
}
}
What I typed in doesn't generate a compiler error, but the output tells me:
TypeError: Error #1010: A term is undefined and has no properties.
at preloader_fla::MainTimeline/loaderF()
And since I really don't have any experience outside of what I'm learning from this tutorial series, I don't know what to do to fix this.
I don't use Flash CS5, but you should be able to get the line # for where the error is occurring, I believe, by executing the SWF by pressing CTRL+SHIFT+ENTER.
Once you get the line number, you should see that something on that line is null or not defined. The error says it occurs in the function loaderF(), and looking at that code the only place such an error could occur is in the else block:
} else {
preloader_mc.preloaderFill_mc.scaleX = total;
preloader_mc.percent_txt.text = Math.floor( total * 100 ) + "%";
preloader_mc.ofBytes_txt.text = loaded + "bytes";
preloader_mc.totalBytes_txt.text = toLoad + "bytes";
}
In the above code block, one of these things is not defined:
preloader_mc.preloaderFill_mc,
preloader_mc.percent_txt,
preloader_mc.ofBytes_txt,
preloader_mc.totalBytes_txt
Maybe your preloader movie clip is missing one of these objects...
First, you'll want to turn on debugging found under (File > Publish Settings > Flash (.swf) > Permit Debugging). This will provide line numbers and allow additional debugging to help track down errors.
Secondly, in the code sample you've provided, you haven't declared a loader, so when you call on loaderInfo, it makes sense that flash complains about "a term is undefined". Although, technically, the loaderInfo object is a child of the event object. Thus, loaderInfo.bytesTotal would become e.loaderInfo.bytesTotal, assuming you added the event listener to the loader object; currently yours is added to the timeline.
Bookmark Adobe's Actionscript 3.0 Reference. Use it. As you begin your journey in Flash, this will be your indispensable handbook to speaking AS3. Specifically, you'll want to refer to the Loader class.
Here's what you're likely missing in your code:
var myLoader:Loader = new Loader();
myLoader.load(new URLRequest("path/to/my/file"));
Your function loaderF is being called during every frame update to the screen (likely every .034 seconds). You'd probably be happier with ProgressEvent.PROGRESS instead of Event.ENTER_FRAME. If so, you'll also want to catch the complete event, and that'd look like this:
myLoader.addEventListener(Event.COMPLETE, loadComplete);
myLoader.addEventListener(ProgressEvent.PROGRESS, loadProgress);
function loadComplete(e:Event):void {
// Stuff to do when the file finishes loading.
}
function loadProgress(e:Event):void {
var current:int = e.bytesLoaded;
var total:int = e.bytesTotal;
var percent:Number = current/total;
// Update the readout of your loading progress.
}
Hopefully that points you in the right direction. :)

flash as3 and external text config file

My goal was to have an external text file config for a client. I didnt want to go through a crazy xml thing, I just wanted it to be simple to change.
I started with a urlLoader, and was able to dynamically generate an object no problem. This is the function which parses and sets the properties of the object.
function onLoaded(e:Event):void//initializes the config
{
var myString = String(e.target.data);
//trace(e.target.data);
//trace(myString);
var propsArray:Array = myString.split("\n");
for (var i = 0; i < propsArray.length; i++){
var thisLine:Array = propsArray[i].split("=");
var thisPropName:String = thisLine[0];
thisPropName = thisPropName.replace(rex,'');
var thisPropValue:String = thisLine[1];
thisPropValue = thisPropValue.replace(rex,'');
trace("thePropName is: " + thisPropName);
trace("thePropValue is: " + thisPropValue);
config[thisPropName] = thisPropValue;
}
}
The text file would just look something like:
gateway = "http://thePathto/theFile.php
toast = sonofabitch
timer = 5000
xSpeed = 5.0
That way, I could just put a little bit of as3 code in, type what things I wanted configured, then all I would have to do was type config.timer and
var myTimer:Timer = new Timer(Number(config.timer));
I think the problem is load order and scope. The config.timer is not created yet, so the timer is unable to access the value of the config.timer.
I'd look at using XML in future projects of this nature, however to answer your question:
I think the problem is load order and scope. The config.timer is not created yet, so the timer is unable to access the value of the config.timer.
Correct, you will need to initialize your Timer within the onLoaded() method, as the data will be received asynchronously and is not available until this happens.
ok not long ago i had created a download manager that uses this exact concept.
The link below will take you straight to the website where you can download the full swf including my source files. also this website is a good place for resources
http://ffiles.com/flash/web_applications_and_data/dynamic_download_manager_3529.html
Below is my loader:
addEventListener(Event.ENTER_FRAME, update);
var myLoader:URLLoader = new URLLoader();
myLoader.dataFormat = URLLoaderDataFormat.VARIABLES;
myLoader.load(new URLRequest("settings.txt"));
myLoader.addEventListener(Event.COMPLETE, onDataLoad);
function onDataLoad(evt:Event)
{
box1.text = evt.target.data.Id_1;
box2.text = evt.target.data.Id_2;
box3.text = evt.target.data.Id_3;
box4.text = evt.target.data.Id_4;
box5.text = evt.target.data.Id_5;
}
Add some dynamic text boxes to stage and name them "box1, box2 ect..."
Now creat your text file:
Id_1=this is what ever you want
&Id_2=this is what ever you want
&Id_3=this is what ever you want
&Id_4=this is what ever you want
&Id_5=this is what ever you want
Hope this helps.

AS3 Putting function blah(){ around code generates errors on lines of code I dont have

I've got a block of code that's doing what I want - it generates a grid of MC's.
As soon as I put something like function blah() around it, it starts generating errors indicating lines of code I don't have e.g.
TypeError: Error #1010: A term is undefined and has no properties.
at flightCellMaker_fla::MainTimeline/myXMLtrace() [flightCellMaker_fla.MainTimeline::frame1:87]
at flightCellMaker_fla::MainTimeline/processFPBxml() [flightCellMaker_fla.MainTimeline::frame1:52]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at flash.net::URLLoader/onComplete()
When I take the function out, it does what I want it to do. What's up with that?
var testXML:XML;
var myFPBxml:XML;
// Initialise a URLLoader to get XML data from XML file
var myFPBLoader:URLLoader = new URLLoader();
myFPBLoader.load(new URLRequest("flightPlannerBoard.xml"));
// Check XML data fully loaded
myFPBLoader.addEventListener(Event.COMPLETE, processFPBxml);
// Once the flight board planning data is loaded, save it to a variable
function processFPBxml(e:Event):void {
myFPBxml = XML(e.target.data);
myXMLtrace();
}
// Grab the XML data load completed and make it available elsewhere
function myXMLtrace(){
testXML = XML(myFPBxml);
}
trace("***********************" + testXML.*); This throws an error (not within myXMLtrace tho)
OK, so here's the rest of the code that will run correctly on it's own but not in a function:
// Create and place all the flight cells for planning and drag and drop
// Setup 2 loops: j for columns and i for Rows
for (var j:Number =0; j < rowNum; j++){
for (var i:Number =0; i<9; i++){
// Create copies of flightCell for board grid
var my_mc = new flightCell();
my_mc.name = "mc"+i+j;
addChild(my_mc);
// Set event Listeners on all Child objects
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_OVER, fl_MouseOverHandler);
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_OUT, fl_MouseOutHandler);
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_DOWN, fl_MouseDownHandler);
my_mc.myDragShape.addEventListener(MouseEvent.MOUSE_UP, fl_MouseUpHandler);
Object(this).my_mc.yellowHiLite.visible = false;
//cellPos[j] = myXML.cellPosX[j];
//trace(stage.myXML.*);
//trace(my_mc.name);
my_mc.x = (100 + colWidth);
my_mc.y = myRowHeight;
colWidth = colWidth + 155;
//trace(myXML.*);
cellArray[arrayCount] = [my_mc.x, my_mc.y];
trace("CellArrayCount = " + cellArray[arrayCount]);
arrayCount = arrayCount + 1;
}
myRowHeight = myRowHeight + 105;
colWidth = 50;
}
Your trace is throwing an error because it is being executed before the XML is loaded.
ActionScript is asyncronous, which means that while the XML is being loaded, the program execution carries on going, and looks something like this:
Declare testXML:XML and myFPBxml:XML
Create loader and start loading
Add a listener for load completion
Trace testXML
Execute processFPBxml when the XML is loaded
If you move your trace into the myXMLTrace function then it will work correctly.
With the newly added code, the problem is with this line:
Object(this).my_mc.yellowHiLite.visible = false;
There is no need to use the this keyword at all here. This will work inside or outside a function:
my_mc.yellowHiLite.visible = false;
The reason your code fails inside a function is because inside and outside the function this will be the scope of the object in which this code resides. However, when you put the code into a function, the reference you create - my_mc - is scoped locally to the function, not to the parent object, and so this.my_mc is undefined, because this is not the function scope.
Are you taking the code out of the function it's currently in and calling the new function from where the code used to be? Or are you trying to define a function variable?
If you're only putting function blah() { ... } around code in the same place it is currently running, you'll get a syntax error for sure. In that case, try:
var blah:Function= function() { ... };
blah();
If you declare variables within a function they fall inside the scope of the function only.
It sounds like you are trying to access variables from elsewhere in your code.
So what you need to do is declare your variables outside of the function. e.g.
var one:VarType;
var two:VarType;
function blah():void {
trace(one);
}

ActionScript 3 name property is not returning the right name...?

I experienced a problem with the name property in as3, I created this "dot" movieclip and I exported to a class,
then I anonymously created a bunch of dots using a loop. I assigned numbers as name to each dots
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
dotContainer.addChild(d]);
}
}
so far so good, I checked that if I trace the name here, I will get the number I want.
However, it's not giving me the numbers if I trace it in other functions.
I added all of my dots to "dotContainer", and if I click on one of the dots, it will call this function
private function callFullSub(e:MouseEvent):void
{
var full_loader:Loader = new Loader();
var temp:XMLList = subImages[sub];
var full_url = temp[e.target.name].#IMG;
full_loader.load(new URLRequest(full_url));
full_loader.contentLoaderInfo.addEventListener(Event.INIT, fullLoaded);
}
e.target.name is suppose to be numbers like 1 or 2, but it's giving me "instance66" "instance70" and I
have no idea why. Because I did the same thing with loaders before and it totally worked.
Any ideas? Thanks.
christine
The e.target returns the inner most object clicked on, this could be a TextField, another MovieClip or posibly a shape (I'm not 100% of the last one) inside the "Dot".
To prevent this you could try to set the mouseChildren property to false on the Dot's when you add them. This should insure that nothing inside the dots can dispatch the click event, and thus the Dot's should do it.
Perhaps you could also in the event handler verify the target type with code like this:
private function callFullSub(e:MouseEvent):void
{
if(!e.target is Dot)
throw new Error("target in callFullSub is not Dot but: " + e.target.toString());
//The rest of you code here
}
The answer is [e.currentTarget.name] I perform this all the time!
Should return "Dot1" "Dot2", etc.
If the value you wish to return is a number or other data type other than a string (name of object) use [e.currentTarget.name.substr(3,1).toString()]
Should return 1, 2, etc.
Navee
I tried to reproduce your problem first with Flex using runtime created movieClips and then with Flash using Dot movieClip symbols exported for ActionScript. Neither application exhibited the problem.
You may already know names like "instance66" "instance70" are default enumerated instance names. So, whatever is dispatching the MouseEvent is NOT the dot instance. Perhaps you are unintentionally assigning callFullSub to the wrong targets, maybe your containers? Try assigning it to dot instance right after you create them, like this:
private function callDots(num:Number):void
{
for (var i = 0; i < subImagesTotal[num]; i++)
{
var d:Dot = new Dot();
d.x = i*23;
d.y = 0;
d.name = i;
d.addEventListener(MouseEvent.CLICK, callFullSub);
dotContainer.addChild(d]);
}
}
Be sure to temporarily comment out your original assignment.
Try this might work,..
d.name = i.toString();
You have not shown enough of your code for me to be able to give you a DEFINATE answer, I will however say this.
//After you create each loader you need to set its mouseEnabled
//property to false if you do not want it to be the target of
//Mouse Events, which may be superseding the actual intended target;
var full_loader:Loader = new Loader();
full_loader.mouseEnabled = false;
//Also you could name the loaders and see if what comes back when you click is the same.
ALSO! Add this to your Mouse Event handler for CLICK or MOUSE_DOWN:
trace(e.target is Loader); //If traces true you have an answer
I believe that the mouse events are being dispatched by the Loaders.
please provide more of your code, the code where the Loader.contentLoaderInfo's COMPLETE handler fires. I assume this is where you adding the loaders to the display list as I cannot see that now.