Set XML .#<index> based on interator AS3 - actionscript-3

In AS3 is it possible to export multiple iterations of the same variable as seen in the below example:
var item:String = "obj";
var child:XML = new XML(<{item}/>);
child.#x = String(object.x);
child.#y = String(object.y);
child.#n = String(object.name);
child.#w = String(object.width);
child.#h = String(object.height);
//...instead of:
child.#s = String(object.sprite);
//...is the below possible:
for (i = 0; i < <length>; ++i) {
child.#s[i] = String(object.get_sprite(i));
}
//...desired <filename>.xml output:
obj.s0 = "sprite_0"
obj.s1 = "sprite_1"
obj.s2 = "sprite_2"
obj.s3 = "sprite_3"
etc..

Haven't worked on AS3 for a while now, but if I remember correctly you should be able to do:
for (i = 0; i < <length>; ++i) {
var attrName = "s" + i.toString();
child.#[attrName] = String(object.get_sprite(i));
}
Sorry, I don't have tools available to try it out myself, but should work.

Related

Read binary file with extendscript

Limited to using Extendscript in Photoshop I'm trying to write and then read in the same binary file.
I can write the file okay, but I'm not sure where I'm going wrong with the read part.
The data will be RGB colours in hex, so I'll either want to return the data from the read function as array or a string. Only I can't even get it to tell me the file just written exists. And I'm not sure if I should be using seek() or read(). Confused.
var f = new File("D:\\temp\\bin.act");
var w = write_binary(f);
var r = read_binary(w);
alert(r);
function write_binary(afile)
{
afile.encoding = "BINARY";
afile.open ("w");
for(i = 0; i < 256; i++)
{
afile.write(String.fromCharCode (i));
}
afile.close();
}
function read_binary(afile)
{
var f = new File(afile);
f.open("r");
f.encoding = "BINARY";
//var data = f.read();
//if(f.exists) alert(afile);
//alert (data);
var arr = [];
for (var i = 0; i < f.length; i+=4)
{
f.seek(i, 0);
var hex = f.readch().charCodeAt(0).toString(16);
if(hex.length === 1) hex = "0" + hex;
arr.push(hex);
}
return arr;
}
You can read it like this:
// Open binary file
var afile = "/Users/mark/StackOverflow/data.bin"
var f = new File(afile);
f.open("r");
f.encoding = "BINARY";
alert('OK');
var hexstring=""
while (true){
var b = f.readch().charCodeAt(0);
if(f.eof){break;}
var hex = b.toString(16);
if(hex.length === 1) hex = "0" + hex;
hexstring += hex;
}
alert(hexstring);
The corresponding writing part of this answer is here.

calculate an average with 2 variables

I'm creating a game in AS3.
I've got a toolbar with a string for money, one for happiness and one for tourists.
The happiness is coded like that :
var happyString:String = "0";
var happyNumber = Number(happyString);
bonheur.text = String(happyString);
trace(happyString);
I'd like the "happy" value depends on two variables (invisibles for the player).
A variable "confort" and a variable "distraction".
And the "happy" value would be = ("confort"+"distraction")/2
I don't really know how to do it though...
I did that :
var confortString:String = "0";
var confortNumber = Number(confortString);
var distractionString:String = "0";
var distractionNumber = Number(distractionString);
stageRef.addEventListener("PiscineBuilt", piscineVariables, false, 0, true);
private function piscineVariables(event):void{
confortNumber = Number(confortString) +3;
trace(confortNumber);
distractionNumber = Number(distractionString) +20;
trace(distractionNumber);
}
First, I don't think the "confort" and "distraction" values are added each time the function is called...
Second, How I can do in order to have the value "happy" (which is visible for the player in the toolbar) equal to ("confort"+"distraction")/2
var happyString:String = distractionNumber + confortNumber ;
var happyNumber = Number(happyString);
bonheur.text = String(happyString);
trace(happyString);
??
Thank you for your help !
EDIT
So, here I am now :
var confort:Number = 0;
var distraction:Number = 0;
var happy:Number = 0;
happy=(confort+distraction)/2;
bonheur.text=String(happy);
stageRef.addEventListener("PiscineBuilt", piscineVariables, false, 0, true);
private function piscineVariables(event):void{
confort+=10;
distraction+=30;
trace(confort);
trace(distraction);
}
GOOD:
Forgot to change the value of my bonheur.text :
bonheur.text = String( Number(bonheur.text ) +(confort+distraction)/2 );
Why do you store strings and make numbers dependant on them? You just do:
var confort:Number = 0;
var distraction:Number = 0;
var happy:Number = 0;
And whenever your numbers are updated, you should also update the text in bonheur:
happy=(confort+distraction)/2; // making an average is as simple as this, if you're not using strings!!!
bonheur.text=String(happy);

Adding up text box numbers with AS3

I've been have a heck of a time doing all this. I hope everyone can help.So... what this is doing is taking 9 text box number and adding them up in a dynamic text box. So here are my problems.
How can I replace an empty text box with a 0, if the user gets rid of the 0 that is already in there its will come out NaN. The if statements below were supposed to fix it, maybe someone can improve it.
stage.addEventListener(Event.CHANGE, checkTotal);
nextQuestion_btn.addEventListener(MouseEvent.MOUSE_DOWN, nextQuestion);
function checkTotal(e:Event){
var work:Number = parseInt(work_txt.text);
var rnr:Number = parseInt(rnr_txt.text);
var exerciseB:Number = parseInt(exerciseB_txt.text);
var exerciseM:Number = parseInt(exerciseM_txt.text);
var chores:Number = parseInt(chores_txt.text);
var social:Number = parseInt(social_txt.text);
var food:Number = parseInt(food_txt.text);
var twt:Number = parseInt(twt_txt.text);
var partying:Number = parseInt(partying_txt.text);
var other:Number = parseInt(other_txt.text);
if(work_txt.text==""){
work=0;
}
if(rnr_txt.text==""){
rnr=0;
}
if(exerciseB_txt.text==""){
exerciseB=0;
}
if(exerciseM_txt.text==""){
exerciseM=0;
}
if(chores_txt.text==""){
chores=0;
}
if(social_txt.text==""){
social=0;
}
if(food_txt.text==""){
food=0;
}
if(twt_txt.text==""){
twt=0;
}
if(partying_txt.text==""){
partying=0;
}
if(other_txt.text==""){
other=0;
}
var total400:Number = work + rnr + exerciseB + exerciseM +
chores + social + food + twt + partying + other;
I can't let my text boxes add up over 400 so as the user types in 399 into one box, if the user types 2 into the next that current text box will revert to 0 because it would be over 400.
I was told using e.currentTarget could solve that problem but I'm not sure how to use it.
All my code...This is my first time on this site so please forgive me for my noobness.
work_txt.maxChars = 3;
rnr_txt.maxChars = 3;
exerciseB_txt.maxChars = 3;
exerciseM_txt.maxChars = 3;
chores_txt.maxChars = 3;
social_txt.maxChars = 3;
food_txt.maxChars = 3;
twt_txt.maxChars = 3;
partying_txt.maxChars = 3;
other_txt.maxChars = 3;
work_txt.restrict = "0-9"
rnr_txt.restrict = "0-9"
exerciseB_txt.restrict = "0-9"
exerciseM_txt.restrict = "0-9"
chores_txt.restrict = "0-9"
social_txt.restrict = "0-9"
food_txt.restrict = "0-9"
twt_txt.restrict = "0-9"
partying_txt.restrict = "0-9"
other_txt.restrict = "0-9";
/*work_txt.text = "0";
rnr_txt.text = "0";
exerciseB_txt.text = "0";
exerciseM_txt.text = "0";
chores_txt.text = "0";
social_txt.text = "0";
food_txt.text = "0";
twt_txt.text = "0";
partying_txt.text = "0";
other_txt.text = "0";*/
var survival:Number = 0;
nextQuestion_btn.visible=false;
stage.addEventListener(Event.CHANGE, checkTotal);
nextQuestion_btn.addEventListener(MouseEvent.MOUSE_DOWN, nextQuestion);
function checkTotal(e:Event){
var work:Number = parseInt(work_txt.text);
var rnr:Number = parseInt(rnr_txt.text);
var exerciseB:Number = parseInt(exerciseB_txt.text);
var exerciseM:Number = parseInt(exerciseM_txt.text);
var chores:Number = parseInt(chores_txt.text);
var social:Number = parseInt(social_txt.text);
var food:Number = parseInt(food_txt.text);
var twt:Number = parseInt(twt_txt.text);
var partying:Number = parseInt(partying_txt.text);
var other:Number = parseInt(other_txt.text);
if(work_txt.text==""){
work=0;
}
if(rnr_txt.text==""){
rnr=0;
}
if(exerciseB_txt.text==""){
exerciseB=0;
}
if(exerciseM_txt.text==""){
exerciseM=0;
}
if(chores_txt.text==""){
chores=0;
}
if(social_txt.text==""){
social=0;
}
if(food_txt.text==""){
food=0;
}
if(twt_txt.text==""){
twt=0;
}
if(partying_txt.text==""){
partying=0;
}
if(other_txt.text==""){
other=0;
}
var total400:Number = work + rnr + exerciseB + exerciseM +
chores + social + food + twt + partying + other;
trace(work);
trace(rnr);
trace(exerciseB);
trace(exerciseM);
trace(chores);
trace(social);
trace(food);
trace(twt);
trace(partying);
trace(other);
trace(total400);
total400_txt.text = String(total400);
if(total400 >= 400){
nextQuestion_btn.visible=true;
}else{
nextQuestion_btn.visible=false;
}
}
Q1
If the values only can be int type, try to use int instead of Number
var work:int = parseInt(work_txt.text);//work will be 0 if the text is empty
Q2
If you want revert the text to 0(the text box which input 2)
function checkTotal(e:Event){
var target:TextField = e.target as TextField;
if(total400 >= 400){
if (target) {//you may check if target is one of the text box you have listed.
target.text = "0";
}
nextQuestion_btn.visible=true;
}
}

Upload pre-selected file with actionscript

I'm creating a photoshop extension where I need to save the file people are working on and upload it to a server. So I want the extension to be able to automatically choose the current file and upload it to my server.
Problem is I don't know how to pre-select a file for people. Here's my code so far:
var app:Application = Photoshop.app;
var doc:Document = app.documents.add();
doc.selection.selectAll();
var color:RGBColor = new RGBColor();
color.red = 0;
color.green = 0;
color.blue = 255;
doc.selection.fill(color);
var saveOptions:JPEGSaveOptions = new JPEGSaveOptions();
//Add other PDF save options here.
doc.saveAs(File.applicationStorageDirectory, saveOptions);
var jsonOBJ:Object = {};
jsonOBJ.option = "iphone";
jsonOBJ.title = "c";
jsonOBJ.description = "s";
jsonOBJ.app_store_url = "iphone";
jsonOBJ.tags = "c";
jsonOBJ.temp_number = 1;
var _service:HTTPService = new HTTPService();
_service.url = "http://localhost:3000/designs";
_service.method = "POST";
_service.contentType = "application/json";
_service.resultFormat = "text";
_service.useProxy = false;
_service.makeObjectsBindable = true;
_service.addEventListener(FaultEvent.FAULT,faultRX);
_service.addEventListener(ResultEvent.RESULT,resultRX);
_service.showBusyCursor = true;
_service.send( JSON.encode( jsonOBJ ) );
function resultRX():void
{
trace(ResultEvent.RESULT);
}
function faultRX():void
{
trace(FaultEvent.FAULT);
}
var file:FileReference;
var filefilters:Array;
var req:URLRequest;
filefilters = [ new FileFilter('Images', '*.jpg') ]; // add other file filters
file.browse(filefilters);
It's a photoshop extension. I ended up using FileReference and link to the file. it worked. I dunno how to actually upload the image though. When I use file.upload(requestUrl), it sends along a wrong content type. –

Icon Field on List

I load label and icon from mysql database.
and i'd like to create a list with label and icon field.
So I try to do something like that but it doesn't work indeed each line contain label but icon is empty.
var xmllisteFamille:XMLList = XML(event.result).famille;
var xmlListCollFami = new XMLListCollection(xmllisteFamille);
for each (var item:Object in xmlListCollFami){
var vbox:VBox = new VBox;
vbox.label = item.sdfNom;
trace(vbox.label);
vbox.percentHeight=100;
vbox.percentWidth=100;
var xmlItem2:XMLList = item.commDent;
if(xmlItem2.length()>0){
/*
var listAcc:List = new List();
listAcc.percentHeight = 100;
listAcc.percentWidth =100;
listAcc.labelField = "name";
listAcc.dataProvider = xmlItem2;
vbox.addChild(listAcc);
accOnglet1.addChild(vbox); */
var urlImageRoot : urlManager = new urlManager();
var urlRootDental:String = urlImageRoot.urlDental();
trace(urlRootDental);
var list:Array = new Array();
var object:Object;
var xmlListdetail:XMLListCollection = new XMLListCollection(xmlItem2);
for each (var item2:Object in xmlListdetail)
{
object = new Object();
// -- --
object.label = item2.name;
var rootIcon:String= "http://127.0.0.1:10088/Sys/OEMySQL/Dental/"+item2.photo;
trace("rootIcon " + rootIcon);
object.icon = rootIcon;
trace("object.icon " + object.icon);
list.push(object);
}
/* var aNode:XML;
for each (aNode in xmlItem2)
{
object = new Object();
// -- --
object.label = aNode.name;
object.icon = new urlManager().urlDental()+aNode.photo;
list.push(object);
} */
var arrList:ArrayList;
arrList = new ArrayList(list);
var listAcc:List = new List();
listAcc.percentHeight = 100;
listAcc.percentWidth =100;
listAcc.labelField = "label";
listAcc.iconField="icon";
//listAcc.dataProvider = xmlItem2;
listAcc.dataProvider = arrList;
vbox.addChild(listAcc);
accOnglet1.addChild(vbox);
}
}
}
}
I hope that you can help me.
Thanks
it may be crossdomain issue
you need to know if flash player requires crossdomain.xml in this case
use charles proxy to check what exactly you are sending to server (request) and getting back from server (response)
charles proxy website
I found the solution.
I had to create an itemrenderer and add it in as3 to my list.
thanks