AS3 Syntax error: expecting identifier before var - actionscript-3

I get following error:
1084: Syntax error: expecting identifier before var.
this my code, I try a lot of search on Google this error, but don't have.
How I can fix it?
code:
public function setAchievements(Subject:Array, Desc:Array, Coins:Array, Score:Array, Completed:Array) : *
{
(error line here) for(var achievement:Achievement = null,var color:Color = null; this.achievements_mc.achievements_content.content_mc.numChildren > 0; )
{
this.achievements_mc.achievements_content.content_mc.removeChildAt(0);
}
var x:* = 0;
var y:* = 0;
for(var i:int = 0; i < Subject.length; i++)
{
achievement = new Achievement();
achievement.Subject_txt.text = Subject[i];
achievement.Description_txt.text = Desc[i];
achievement.Coins_txt.text = String(Coins[i]);
achievement.Score_txt.text = String(Score[i]);
if(Completed[i])
{
color = new Color();
color.brightness = -0.4;
achievement.transform.colorTransform = color;
achievement.icon.visible = true;
}
achievement.x = x;
achievement.y = y;
y = y + 125;
this.achievements_mc.achievements_content.content_mc.addChild(achievement);
}
}

The correct syntax of for / for each statement in AS3 is:
for each (var yourvariable:TypeOfVariable in list of objects)
i.e.
for (var currObject:Person in listPerson)
(where your listPerson is an ArrayCollection)
Another correct syntax is:
for (var i:int; i < list.length(); i++)
but you have written:
for(var achievement:Achievement = null,var color:Color = null;
this.achievements_mc.achievements_content.content_mc.numChildren > 0;)
This isn't a correct syntax, because ther's no link between loop variables and exit condition
Maybe you want to write
for each(var achievement:Achievement in
this.achievements_mc.achievements_content.content_mc.numChildren)
and then you can initialize
var color:Color = 0 or another value linked to iteration

Related

How to Save positions for 3 objects in Array to make random position between each other by AS3?

How to Save positions for 3 objects in Array to make random position between each other by AS3?
import flash.geom.Point;
var arry:Point = new Point();
arry[0] = arry[78,200];
arry[1] = arry[217,200];
arry[2] = arry[356,200];
//object called b1
b1.x = arry[0][0];
b1.y = arry[0][1];
//object called b2
b2.x = arry[1][0];
b2.y = arry[1][1];
//object called b3
b3.x = arry[2][0];
b3.y = arry[2][1];
//make objects swap positions between each other
var rand:Number = (Math.random()*arry.length);
//output to see random position [[78,200],[217,200],[356,200]]
trace(arry);
to get random with tween like this... https://www.youtube.com/watch?v=8m_m64plQ6E
At compile time you should get this Error I suppose : "ReferenceError: Error #1069"
Here is a way to store the positions (like in the link you provided from youtube) :
import flash.geom.Point;
var squareWidth:uint = 40;
var squareHeight:uint = 40;
var marginX:uint = 100;
var marginY:uint = 75;
var spacer:uint = 10;
var positions:Vector.<Point > = new Vector.<Point > (9);
function setPositions(v:Vector.<Point>):void {
var count:uint = 0;
var posx:uint;
var posy:uint;
for (var i = 0; i < 3; i ++)
{
for (var j = 0; j < 3; j ++)
{
posx = (j * squareWidth) + (spacer * j) + marginX;
posy = (i * squareHeight) + (spacer * i) + marginY;
v[count] = new Point(posx,posy);
count++;
}
}
}
setPositions(positions);
trace(positions);
// output :
// (x=100, y=75),(x=150, y=75),(x=200, y=75),(x=100, y=125),(x=150, y=125),(x=200, y=125),(x=100, y=175),(x=150, y=175),(x=200, y=175)
So here you have nine Points to place the clips like in the video.
You just have to add a function to swap the nine boxes stored in another Vector.
In your case.
For 3 positions do the following if I understand your question.
import flash.geom.Point;
var positions:Vector.<Point> = new Vector.<Point>(3);
var p1:Point = new Point(78,200);
var p2:Point = new Point(217,200);
var p3:Point = new Point(356,200);
positions[0] = p1;
positions[1] = p2;
positions[2] = p3;
trace(positions);
// output : (x=78, y=200),(x=217, y=200),(x=356, y=200)
So, you're still unclear!
Your issue is to find a random position?
This may help you if this is the problem you're facing :
import flash.geom.Point;
var positions:Vector.<Point > = new Vector.<Point > (3);
var numbers:Vector.<uint> = new Vector.<uint>();
var numbersAllowed:uint = 3;
var rndNmbrs:Vector.<uint> = new Vector.<uint>(3);;
var p1:Point = new Point(78,200);
var p2:Point = new Point(217,200);
var p3:Point = new Point(356,200);
positions[0] = p1;
positions[1] = p2;
positions[2] = p3;
trace(positions);
function populateRndNmbrs(n:uint):void {
for (var i:uint = 0; i < n; i++)
{
numbers[i] = i;
}
}
populateRndNmbrs(numbersAllowed);
function populateRandomNumbers(n:uint):void
{
var rnd:uint;
for (var i:uint = 0; i < n; i++)
{
rnd = numbers[Math.floor(Math.random() * numbers.length)];
for (var j:uint = 0; j < numbers.length; j++)
{
if (rnd == numbers[j])
{
numbers.splice(j,1);
}
}
rndNmbrs[i] = rnd;
}
}
populateRandomNumbers(numbersAllowed);
trace("rndNmbrs = " + rndNmbrs);
for (var i:uint = 0; i < numbersAllowed; i++)
{
trace("b"+ (i+1) + ".x = " + positions[rndNmbrs[i]].x);
trace("b"+ (i+1) + ".y = " + positions[rndNmbrs[i]].y);
// In place of trace, you should place the boxes at those random positions.;
}
//output:
//(x=78, y=200),(x=217, y=200),(x=356, y=200)
//rndNmbrs = 2,0,1
//b1.x = 356
//b1.y = 200
//b2.x = 78
//b2.y = 200
//b3.x = 217
//b3.y = 200
Is that what you want? Or do you want to know how to create a motion effect?
I'm not sure about what you really need...
This will help you to place all the boxes in a random position.
You may do this like here bellow, and add a function to check if the random positions are not the same.
With only 3 MovieClips, you will often have the same random positions as long they're stored in the "positions Vector"
var squares:Vector.<MovieClip> = new Vector.<MovieClip>(3);
function populateMCs(target:DisplayObjectContainer,n:uint):void{
for (var i:uint = 0; i < n; i++){
squares[i] = target["b"+(i+1)];
}
}
function swapMCs():void{
for (var i:uint=0; i<squares.length; i++){
squares[i].x = positions[rndNmbrs[i]].x;
squares[i].y = positions[rndNmbrs[i]].y;
}
}
populateMCs(this,numbersAllowed);
swapMCs();
I give you a last example to get a motion effect in AS3.
I'm not a translator AS2 -> AS3 and a video is not the best way to show your code :(
This will make your boxes move smoothly, but not the way you want.
Now, you have to learn AS3 and try to make the job by yourself.
Then, if you have another issue, just ask clearly what you want.
var friction:Number = 0.15;
setDestination(squares[0],squares[0].x,350,friction);
setDestination(squares[1],squares[1].x,350,friction);
setDestination(squares[2],squares[2].x,350,friction);
squares[0].addEventListener(Event.ENTER_FRAME,moveClip);
squares[1].addEventListener(Event.ENTER_FRAME,moveClip);
squares[2].addEventListener(Event.ENTER_FRAME,moveClip);
function setDestination(mc:MovieClip,x:uint,y:uint,friction:Number):void{
mc.destinx = x;
mc.destiny = y;
mc.f = friction;
}
function moveClip(e:Event):void{
var mc:MovieClip = e.target as MovieClip;
trace(mc.name)
mc.speedx = (mc.destinx - mc.x);
mc.speedy = (mc.destiny - mc.y);
mc.x += mc.speedx*mc.f;
mc.y += mc.speedy*mc.f;
if((Math.floor(mc.speedx)<1) && (Math.floor(mc.speedy)<1)){
mc.removeEventListener(Event.ENTER_FRAME,moveClip);
trace("STOP MOVE FOR " + mc.name);
}
}

dynamically add children to component, then dynamically manipulate them?

i dont know why this doesnt work. i have a tabnavigator which i dynamically add a navigatecontainer to which has a textarea dynamically added to the nav container. the id's of both have the same stringname as the chatguys[c][0].
it gives errors saying 'TypeError: Error #1034: Type Coercion failed: cannot convert spark.skins.spark::SkinnableContainerSkin#9737851 to spark.components.TextArea." at runtime. wut do?
any help would be greatly appreciated, thank you :(
var idx:uint;
const len:uint = navigate.numChildren;
var alreadyexists:Boolean = false;
for (idx = 0; idx < len; idx++) {
//var check:spark.components.TextArea = navigate.getElementAt(idx) as spark.components.TextArea;
//var check:Tab = navigate.getTabAt(idx) as Tab;
var check:NavigatorContent = navigate.getChildAt(idx) as NavigatorContent;
// var check = navigate.getElementAt(idx);
//elmt.selected = tgBtn.selected;
if (check.label == evt.username)
{// trace(navigate.getChildAt(idx).label);
var c:uint;
const p:uint = chatguys.length;
for (c = 0; c < p; c++){
if(chatguys[c][0] == evt.username){
spark.components.TextArea(DisplayObjectContainer(navigate.getChildAt(idx)).getChildAt(chatguys[c][0])).textFlow=TextConverter.importToFlow(chatguys[c][1], TextConverter.TEXT_FIELD_HTML_FORMAT);
}}
b here ill show you the second half which works fine, but maybe i need to do it differently to get waht i need done ?
var chats:Array = [];
var chatguys:Array = [];
public function userlist_click() :void{
var windowname:Object =users_lst.selectedItem;
var idx:uint;
const len:uint = navigate.numChildren;
trace(navigate.numChildren);
var alreadyexists:Boolean = false;
for (idx = 0; idx < len; idx++) {
var check:NavigatorContent = navigate.getChildAt(idx) as NavigatorContent;
if (check.label == windowname.name)
{
alreadyexists = true;
}
}
if (alreadyexists == false)
{
chats[windowname.name] = new spark.components.TextArea();
chats[windowname.name].x = 10;
chats[windowname.name].y= 32;
chats[windowname.name].width= 517.19696 ;
chats[windowname.name].height= 343.18182;
chats[windowname.name].scroller
chats[windowname.name].x="9";
chats[windowname.name].y="2";
chats[windowname.name].width="517.19696";
chats[windowname.name].height="343.18182";
chats[windowname.name].setStyle("skinClass", spark.skins.spark.TextAreaSkin);
//textArea.skinClass= "spark.skins.spark.TextAreaSkin";
chats[windowname.name].text="ffg";
chats[windowname.name].setStyle("verticalScrollPolicy", ScrollPolicy.ON);
var match = new Array();
match.push(windowname.name);
match.push('bob');
chatguys.push(match);
for (var i in chatguys){
if (chatguys[i][0] == windowname.name){
chats[windowname.name].textFlow=TextConverter.importToFlow(chatguys[i][1], TextConverter.TEXT_FIELD_HTML_FORMAT);
}}
chats[windowname.name].id = windowname.name;
trace(chats[windowname.name]);
var messagebox:NavigatorContent = new NavigatorContent;
messagebox.percentWidth= 100;
messagebox.percentHeight= 100;
messagebox.label = chats[windowname.name];
messagebox.id = chats[windowname.name];
trace(messagebox.label);
messagebox.addElement(chats[windowname.name]);
navigate.addChild(messagebox); }}

How to call a variable of a function using concatenation (AS3)

I need to acess a variable inside this function using concatenation, following this example:
public function movePlates():void
{
var plate1:Plate;
var plate2:Plate;
var cont:uint = 0;
for (var i:uint = 0; i < LAYER_PLATES.numChildren; i++)
{
var tempPlate:Plate = LAYER_PLATES.getChildAt(i) as Plate;
if (tempPlate.selected)
{
cont ++;
this["plate" + cont] = LAYER_PLATES.getChildAt(i) as Plate;
}
}
}
EDIT:
public function testFunction():void
{
var test1:Sprite = new Sprite();
var test2:Sprite = new Sprite();
var tempNumber:Number;
this.addChild(test1);
test1.x = 100;
this.addChild(test2);
test2.x = 200;
for (var i:uint = 1; i <= 2; i++)
{
tempNumber += this["test" + i].x;
}
trace("tempNumber: " + tempNumber);
}
If i run the code like this, the line this["test" + i] returns a variable of the class. I need the local variable, the variable of the function.
Your loop on first step access plate0 this will cause not found error, if plate0 is not explicitly defined as class member variable or if class is not defined as dynamic. Same thing will happen for plate3, plate4, plate5... in case LAYER_PLATES.numChildren is more than 3.
EDIT:
Thanks to #Smolniy he corrected my answer plate0 is never accessed because cont is incremented before first access. So as he mentioned problem should be on plate3
You don't get the local variable with [] notation. your case has many solutions. You can use dictionary, or getChildAt() function:
function testFunction():void
{
var dict = new Dictionary(true);
var test1:Sprite = new Sprite();
var test2:Sprite = new Sprite();
var tempNumber:Number = 0;
addChild(test1);
dict[test1] = test1.x = 100;
addChild(test2);
dict[test2] = test2.x = 200;
for (var s:* in dict)
{
tempNumber += s.x;
//or tempNumber += dict[s];
}
trace("tempNumber: " + tempNumber);
};

XMLListCollection find index value

I want to find specific value on a XMLListCollection.
I try to use something like this but it doesn't work!
var xmllisteRDV:XMLList= XML(event.result).RDVClinik;
xmlCollSuivi = new XMLListCollection(xmllisteRDV);
var index:Number = -1;
for(var i:Number = 0; i < xmllisteRDV.length(); i++)
{
if(XML(xmllisteRDV[i]).#grDateDeb == todayDate)
{
index = i;
break;
}
}
First going to try pointing out errors in the original code:
var xmllisteRDV:XMLList= XML(event.result).RDVClinik; //Unnecessary cast, event.result is Object compiler will not check or know the run-time type, doesn't care because Object is declared dynamic meaning properties can be added to it dynamically, if RDVClinik didn't exist on the particular Object type it would simply be null casting as XML gives it no information about this "property"
xmlCollSuivi = new XMLListCollection(xmllisteRDV);
var index:Number = -1;
for(var i:Number = 0; i < xmllisteRDV.length(); i++) //length is a property not a method on XMLListCollection this should throw a compile time error
{
if(XML(xmllisteRDV[i]).#grDateDeb == todayDate)// I see no type when debugging for the result of xmllisteRDV[i] not positive here but this cast is at the least unnecessary
{
index = i;
break;
}
}
Here's a version I think will work possibly with changes to how todayDate is built
var date:Date = new Date();
var todayDate:String = date.dateUTC+"/"+date.dayUTC+"/"+date.fullYear;
var index:int=-1;
for(var i:int = 0; i < flex3Projects.length; i++)
{
trace(xmllisteRDV[i].#grDateDeb)
if(xmllisteRDV[i].#grDateDeb.toString() == todayDate)
{
index = i;
break;
}
}
With you help, I found the solution
private function setSelectedItem():void
{
var gData:Object = dgSuiviClini.dataProvider;
var todayDate:String= new DateUtility().DateAsToString(new Date());
for(var i:Number=0; i < gData.length; i++)
{
var thisObj:Object = gData.getItemAt(i);
if(thisObj.grDateDeb == todayDate)
{
dgSuiviClini.selectedIndex = i;
//sometimes scrollToIndex doesnt work if validateNow() not done
dgSuiviClini.validateNow();
//dgSuiviClini.scrollToIndex(i);
}
else{
dgSuiviClini.validateNow();
// dgSuiviClini.scrollToIndex(gData.length);
}
}
dgSuiviClini.validateNow();
dgSuiviClini.editedItemPosition = { rowIndex: gData.length-1, columnIndex: nColSaisie };
}
Thanks

Read set of numbers from txt file

I've been working on a project for a while but got stuck where I have a text file that contains a set of numbers in this format:
1-2-3-4
1-2-3-4
1-2-3-4
1-2-3-4
So I must read the numbers from the file and put them in an array according to the column so at the end I have
column1:Array (1,1,1,1)
column2:Array (2,2,2,2)
..... and so on. I can't figure how to do this.
What I managed to do was read all the file and have all the numbers in 1 array but just that.
Here's the code
var myTextLoader:URLLoader = new URLLoader;
var txtArray:Array;
myTextLoader.load(new URLRequest(inputFile.text));
myTextLoader.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(e:Event):void
{
txtArray = e.target.data.split(/\-|\n/g);
}
before split \n \r to array (reading with a loop)
and the same with... -
a loop in loop to get a multidimensional array
in mind the result is a "table"
finally to get a result do this.
variable[file][column]
a[2][3] ----> 4
;)
Try splitting it into two parts, first by line, then by element:
function onLoaded(e:Event):void
{
preArray:Array = e.target.data.split(/\n/g);
txtArray = new Array();
for(var i:int = 0; i < preArray.length; i++) {
txtArray.push(preArray[i].split(/\-/g));
}
}
This will give you a 2D array, which you'd access like this:
textArray[0][0]; // result: 1
textArray[2][3];
Et cetera.
Thanks to Llanis who gave me the idea. This is my final code. Sorry i didn't tag the language it never ocurred to me i'm new.
myTextLoader.load(new URLRequest(inputWX.text));
myTextLoader.addEventListener(Event.COMPLETE,onLoaded);
function onLoaded(e:Event):void
{
txtArray = e.target.data.split(/\-|\n/g);
var wArray:Array = new Array(txtArray.length/4);
var xArray:Array = new Array(txtArray.length/4);
var yArray:Array = new Array(txtArray.length/4);
var zArray:Array = new Array(txtArray.length/4);
var a:int = 0;
var b:int = 0;
var c:int = 0;
var d:int = 0;
var columna:int = 1;
for(var arrayIndex:int = 0; arrayIndex <= txtArray.length-1;arrayIndex++)
{
switch(columna){
case 1: wArray[a] = txtArray[arrayIndex]; a+=1;
break;
case 2: xArray[b] = txtArray[arrayIndex]; b+=1;
break;
case 3: yArray[c] = txtArray[arrayIndex]; c+=1;
break;
case 4: zArray[d] = txtArray[arrayIndex]; d+=1;
break;
}
if(columna == 4)
columna = 1;
else columna++;
}
}