How can I display a variable in a dynamic text field? - actionscript-3

I have an input textfield on the screen and I have it set to the text that a user enters is saved as a variable and is later called upon o be displayed in a dynamic text box. It's kinda like a high score kind of system, but with multiple variables.
Here is the frame actions where the variables are being set (at least I think they are)
button.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler);
function fl_MouseClickHandler(event:MouseEvent):void
{
var data:String = username.text + " " + date.text + " " + company.text;
var file:FileReference = new FileReference();
file.save(data, username.text + " " + date.text + " " + company.text + ".txt");
}
button.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextFrame_8);
function fl_ClickToGoToNextFrame_8(event:MouseEvent):void
{
nextFrame();
}
var nameperson = username.text;
var dateperson = date.text;
var companyperson = company.text;
And are the actions where I'm trying to display the variables in another frame:
var nScore:Number = 0;
for(var i:Number = 0; i < aQuestions.length; i++)
{
if(aUserAnswers[i].toUpperCase() == aCorrectAnswers[i].toUpperCase())
{
nScore++;
}
if(i == aQuestions.length - 1)
{
score_txt.text = nScore.toString();
}
}
endresult_name.text = nameperson;
endresult_date.text = dateperson;

I believe the problem is that you set these variables straight when you get to your 1st frame (the text inputs are empty at that point). You need to set them after the user have filled them out and clicked on the button:
var nameperson:String;
var dateperson:String;
var companyperson:String;
function fl_MouseClickHandler(event:MouseEvent):void
{
var data:String = username.text + " " + date.text + " " + company.text;
var file:FileReference = new FileReference();
file.save(data, username.text + " " + date.text + " " + company.text + ".txt");
nameperson = username.text;
dateperson = date.text;
companyperson = company.text;
}

Related

How much faster could I make this script?

I have noticed that the script is significantly faster when I comment out the line that calls setValues(). Not that it's a super big issue, but I am interested in the different ways (if any) that I could speed the process up. If anyone has some insight, I would greatly appreciate it! It would also help me see where I could make similar optimizations in other parts of my script.
Here is the code in question:
// import new value(s)
if ((numRaw - numDk) > 0) {
var iDate;
var lastRow = getLastDataRow(earSheet, columnToLetter(earSheet.getRange("EAR_DATES_RNG").getColumn()));
var distName = impSheet.getRange("A1").getValue().toString();
var defaultDept = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("All Lists").getRange("A2").getValue().toString();
impData.forEach(function (entry) {
iDate = new Date(entry[0]);
var newEar = earSheet.getRange("A" + lastRow + ":G" + lastRow).getValues(); // init copy
if (currData.get(iDate.getTime()) == null) {
newEar[0][0] = entry[0];
newEar[0][1] = distName;
newEar[0][6] = entry[1];
newEar[0][3] = "Royalties";
newEar[0][4] = defaultDept;
newEar[0][5] = "NOT SPECIFIED";
earSheet.getRange("A" + lastRow + ":G" + lastRow).setValues(newEar);
Logger.log("ADDED: " + entry[1] + " to row: " + lastRow);
addedNewData++;
lastRow++;
}
else {
Logger.log("EXISTING DATA FOUND: " + currData.get(iDate.getTime()));
}
});
}
Using Matt and Cooper's suggestions, I came up with this revised code which is significantly faster:
if ((numRaw - numDk) > 0) {
var iDate;
var prevLastRow = getLastDataRow(earSheet, columnToLetter(earSheet.getRange("EAR_DATES_RNG").getColumn()));
var currLastRow = getLastDataRow(earSheet, columnToLetter(earSheet.getRange("EAR_DATES_RNG").getColumn()));
var rangeSize = numRaw;
var distName = impSheet.getRange("A1").getValue().toString();
var defaultDept = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("All Lists").getRange("A2").getValue().toString();
var newEar = earSheet.getRange("A" + prevLastRow + ":G" + (prevLastRow + rangeSize - 1)).getValues(); // init copy
impData.forEach(function (entry) {
iDate = new Date(entry[0]);
if (currData.get(iDate.getTime()) == null) {
newEar[currLastRow-2][0] = entry[0];
newEar[currLastRow-2][1] = distName;
newEar[currLastRow-2][6] = entry[1];
newEar[currLastRow-2][3] = "Royalties";
newEar[currLastRow-2][4] = defaultDept;
newEar[currLastRow-2][5] = "NOT SPECIFIED";
Logger.log("ADDED: " + entry[1] + " to row: " + currLastRow);
addedNewData++;
currLastRow++;
}
else {
Logger.log("EXISTING DATA FOUND: " + currData.get(iDate.getTime()));
}
});
earSheet.getRange("A" + prevLastRow + ":G" + (prevLastRow + rangeSize - 1)).setValues(newEar);
}```

AS3: How to check, all zip files has been extracted?

How to check all zip files has been extracted?
var reader: ZipFileReader = new ZipFileReader();
reader.addEventListener(ZipEvent.ZIP_DATA_UNCOMPRESS, zipDataUncompressHandler);
var zipFile: File = new File(zipFilePath);
reader.open(zipFile);
var list: Array = reader.getEntries();
zipFileCount = list.length;
trace(zipFileCount + " Numbers of items");
for each(var entry: ZipEntry in list) {
var filename: String = entry.getFilename();
if (entry.isDirectory()) {
trace("DIR --->" + filename);
} else {
trace("FILE --->" + filename + "(" + entry.getCompressRate() + ")");
reader.unzipAsync(entry);
}
zipFileWritedCount = zipFileWritedCount + 1;
}
function zipDataUncompressHandler(e: ZipEvent): void {
var entry: ZipEntry = e.entry;
var zfile: File = File.userDirectory.resolvePath('somefolder' + File.separator + entry.getFilename());
var fs: FileStream = new FileStream();
fs.open(zfile, FileMode.WRITE);
fs.writeBytes(e.data);
fs.close();
trace("Refresh Scene");
//include "RefreshScene.as";
}
My files were extracted, but I need to check all files are actually extracted.
Is there any way i can do that.
And I am using airxzip while working with zip file.
Also if I can add an loader.
You can shorten zipFileWritedCount = zipFileWritedCount + 1;
By using just a zipFileWritedCount +=1; or even
zipFileWritedCount++;
Anyways for checking the "all files extracted" amount you could try
the Equality == operator as mentioned in the manual.
Quick example :
for each(var entry: ZipEntry in list)
{
var filename: String = entry.getFilename();
if ( entry.isDirectory() ) { trace("DIR --->" + filename); }
else
{
trace("FILE --->" + filename + "(" + entry.getCompressRate() + ")");
reader.unzipAsync(entry);
}
zipFileWritedCount += 1; //add plus 1
if ( zipFileWritedCount == zipFileCount ) //if Equal to zipFileCount..
{
trace ("unzipped all files...");
trace ("zipFileCount: " + zipFileCount + " -VS- " + "zipFileWritedCount: " + zipFileWritedCount )
}
}

Sort JSON data by Date/Time value

Hope someone could help with this small task. I have an array of text blocks that have a DateTime value assigned to them. I would like to publish those text blocks sorted by DateTime so that the latest updated item is always on top.
Here is the script:
function jsonCallBack(data) {
var strRows = "";
$.each(data.News, function(i, item) {
var htmlNewsBody = item["htmlNewsBody"];
var maxLength = 120
var trimmedString = htmlNewsBody.substr(0, maxLength);
trimmedString = trimmedString.substr( 0, Math.min( trimmedString.length,
trimmedString.lastIndexOf(" ") ) );
strRows += "<div id='nrNewsItem-" + i + "'>";
strRows += "<h3>" + item["txtTitle"] + "</h3>";
strRows += "<p>" + item["dtDateTime"] + "</p>";
strRows += "<p>" + trimmedString + "...</p>";
strRows += "</div>"
});
$("#printHere").html(strRows);
};
Also have a working jsFiddle with JSON data.
You can add a custom compare method:
function compare(a,b) {
if (a.dtDateTime < b.dtDateTime) {
return 1;
}
if (a.dtDateTime > b.dtDateTime) {
return -1;
}
return 0;
}
Then in your function:
function jsonCallBack(data) {
data.News.sort(compare);
....

AS3 Error: 1078: Label must be a simple identifier

I have some code not written by me that I'm trying to compile.
public static function getUserInfoObject(info:Array) : Object {
var lastBattleTime:Number = info[7];
var listLength:Number = info[8];
var list:Array = info.slice(9,9 + listLength);
var achievesLength:Number = info[9 + listLength];
var achievements:Array = info.slice(10 + listLength,10 + listLength + achievesLength);
var statsLength:Number = info[10 + listLength + achievesLength];
var stats:Array = info.slice(11 + listLength + achievesLength,11 + listLength + achievesLength + statsLength);
var commonInfo:Array = info.slice(11 + listLength + achievesLength + statsLength,11 + listLength + achievesLength + statsLength + 8);
return
{
"uid":info[0],
"name":info[1],
"chatRoster":info[2],
"status":info[3],
"displayName":info[5],
"list":list,
"achievements":achievements,
"stats":stats,
"commonInfo":commonInfo,
"creationTime":App.utils.locale.longDate(info[6]),
"lastBattleTime":(lastBattleTime == 0?"":App.utils.locale.longDate(lastBattleTime) + " " + App.utils.locale.longTime(lastBattleTime))
};
}
It gives me this error: 1078: Label must be a simple identifier. in every line in return.
Am I blind or dumb or this code is bad?
You should start your return statement with the curly brace, not with new line:
public static function getUserInfoObject(info:Array) : Object {
return { // <-Here
};
}

map.fitbounds not doing anything

I have a script which takes data from html and uses it to plot routes on a map. I'm trying to get it to take all the routes and zoom to show all of them. Here's the code:
var ombounds = new google.maps.LatLngBounds(); //bounds
for (var r=1; $("#master-master > div").length; r++)
{
// Add a placemark at the start of each route
var placemark_id = "#master-" + r + " > #placemarks-data > #placemark-0";
var markerParameters = {};
markerParameters.index = r-1;
markerParameters.total = $(placemark_id + " > .total").html();
markerParameters.mapData = $(placemark_id + " > .map").html();
markerParameters.instructionsData = $("#master-" + r + " > #snippet").html();
markerParameters.distanceData = $("#master-" + r + " > #distance").html();
markerParameters.durationData = $("#master-" + r + " > #duration").html();
markerParameters.titleData = $("#master-" + r + " > #title").html();
markerParameters.urltitleData = $("#master-" + r + " > #url-title").html();
markerParameters.imageData = $(placemark_id + " > .image").html();
$.OverMap.addMarker(markerParameters);
// Draw route
var points = new Array();
var pointsData = [];
var pointsData = $('#master-' + r + ' > #route-data').html();
if (pointsData != '')
{
var pointsArray = JSON.parse(pointsData);
for (var p=0; p<pointsArray.length; p++) {
var pt = new google.maps.LatLng(pointsArray[p][0], pointsArray[p][1]);
points.push(pt);
ombounds.extend(pt); //bounds
}
$.OverMap.drawRoute(points);
}
else $.OverMap.drawDirections({preserveViewport:false});
}
(map.getBounds()); //bounds
map.fitBounds(ombounds); //bounds
For some reason, map.fitBounds(ombounds) does nothing. The code still plots the routes, but I can't see what the bounds related functions are doing. I've added //bounds to all the lines related to the zooming - at the moment, they have no visible affect on the code's output. Anyone know where I'm going wrong?
When the preserveViewport option of the directions service is set to false, each call to the DirectionsRenderer will zoom and center the map on its result. If you want fitBounds to override it, you need to set that to true.