Syntax error: expecting semicolon before leftbracket - actionscript-3

I have the following code at frame one of the top layer:
var level:Array = new Array();
var level[0] = new Object();
I am getting the following error:
Scene 1, Layer 'levels', Frame 1, Line 2
1086: Syntax error: expecting semicolon before leftbracket.
I've looked at many examples which seem to be doing the same thing I'm doing, and I've also tried defining the object separately and then adding it to the array, with the same error. I've also searched for the answer, but all the questions I've found are a little different than this situation.

The syntax/grammar production
var level[0] = new Object();
is invalid.
The var production expects a simple Identifier, not an expression1. The parser was roughly trying to treat the production as var level;[0] = ..; (but failed due to the missing semicolon, and would have failed anyway because [0] = ..; is an invalid production as well).
Try:
var level:Array = new Array();
level[0] = new Object(); // no "var" here so it is a valid grammar production
or, more concisely:
var level:Array = [{}]; // using Array and Object literal notations
1 See AS3.g for that ANTLR production rules. Alternatively, for ECMAScript (from which AS derives), see 12.2 Variable Statement from ES5-Annotated and note that it requires an "Identifier" followed by an optional initializer (=) or another declaration (,) or end of statement (;).

Only is
level[0] = {};
or
level.push({});

Related

Array of stagewebviews

I am in the need of multiple staged WebViews for holding multiple loaded websites at the same time.
I was hoping to manage this by making an array of webviews object, so i could call them later as view[i].
var view:Array=[webview0, webview1, webview2];
for each (var v in view){
var v:StageWebView = new StageWebView();
This gives error: 1086: Syntax error: expecting semicolon before left bracket.
Does someone know how to make an array like that?
You're doing something really weird there in terms of syntax. If you just want an Array of freshly created instances, it goes like that:
// Initialize the array.
var Views:Array = new Array;
// This loop counts 0,1,2.
for (var i:int = 0; i < 3; i++)
{
// Create a new instance.
// Yes, you can omit () with new operator if there are no arguments.
var aView:StageWebView = new StageWebView;
// Assign the new element to your array.
Views[i] = aView;
}
Or, if you need only 3 then you don't need to go algorithmic.
var Views:Array = [new StageWebView, new StageWebView, new StageWebView];
Not on topic but related:
Here is an example of one HTML page hold multiple StageWebViews
https://www.w3schools.com/graphics/tryit.asp?filename=trymap_basic_many

1050 Cannot assign to a non-reference value as3

I'm having some trouble with the error "1050: Cannot assign to a non-reference value." I'm still fairly new to coding, and so being unable to fix this error is frustrating, any help will be greatly appreciated.
var PracticeDummyHealth:int=50
var PlayerAttack:int=20;
public function PlayerAttackFunction(){
if(PracticeDummyHealth>0){
PracticeDummyHealth-PlayerAttack=PracticeDummyHealth;
}
}
An grammar construct which is not a Property/Variable name is on the left of the = assignment operator:
// expression = expression
PracticeDummyHealth-PlayerAttack=PracticeDummyHealth;
// which makes as much sense to ActionScript as .. it's not an equation solver :)
// 100 - 50 = 100
Compare with this valid code:
// variable = new_value
PracticeDummyHealth = PracticeDummyHealth - PlayerAttack;
// or
PracticeDummyHealth -= PlayerAttack;
Note that a "reference" (read: Property/Variable name) appears on the left of the = (or compound -=) in both of these cases. This terminology comes from the specification which deals with l-values and it is slightly unfortunate it doesn't yield a nicer error message here.

As3IsoLib: IsoBoxes in an Array

I'm wanting to store four boxes in an array and iterate over all of them in a 'for' loop placing each at a different location. I'm using the isometric library As3IsoLib. Here is my code so far.
var BOX1:IsoBox = new IsoBox();
var BOX2:IsoBox = new IsoBox();
var myArray:Array = new Array(BOX1,BOX2);
for (var occr:IsoBox in myArray){
But I'm getting an error at my 'for' loop line which is
Description Resource Path Location Type
1067: Implicit coercion of a value of type String to an unrelated type as3isolib.display.primitive:IsoBox. isometric.as /main/src line 51 Flex Problem
This line:
for (var occr:IsoBox in myArray){
Should be:
for each (var occr:IsoBox in myArray){
That will solve the error. This is happening because it is intended to loop over properties of an object, not indexes of an array. So there is a strange type requirement.
The "for each" loop is much better suited to looping over elements of an array.

How to declare a filled Vector?

I am using Vectors in Flash 10 for the first time, and I want to create it in the same way I used to do with Arrays, e.g:
var urlList : Array = [url1, url2, url3];
I have tried various different methods but none seem to work, and I have settled on the following as a solution:
var urlList : Vector.<String> = new Vector.<String>();
urlList.push(url1, url2, url3);
Is this even possible?
When it doubt, check the AS3 docs. :)
var urlList : Vector.<String> = new <String>["str1", "str2", "str3"];
trace(urlList);
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Vector.html#Vector()
Direct quote of the line I adapted this from in the documentation:
To create a pre-populated Vector instance, use the following syntax instead of using the parameters specified below:
// var v:Vector.<T> = new <T>[E0, ..., En-1 ,];
// For example:
var v:Vector.<int> = new <int>[0,1,2,];
You coerce an array to a Vector:
var urlList:Vector.<String> = Vector.<String>([url1, url2, url3]);

Dynamic variables in ActionScript 3.0

so.... eval() out of the question, any idea to do this? I also don't know how to use "this" expression or set() in actionscript 3 ( i seem couldn't find any complete reference on it ), just say through php file a multiple variable (test1, test2, test3,...) sent by "echo", how the flash aplication recieved it? I'm trying not to use xml on mysql to php to flash aplication. Simply how to change a string to a variable ?
example
(in as3-actions frame panel)
function datagridfill(event:MouseEvent):void{
var varfill:URLVariables = new URLVariables();
varfill.tell = "do it";
var filler:URLRequest = new URLRequest();
filler.url = "http://127.0.0.1/flashdbas3/sendin.php";
filler.data = varfill;
var filling:URLLoader = new URLLoader();
filling.dataFormat = URLLoaderDataFormat.VARIABLES;
filling.load(filler);
filling.addEventListener(Event.COMPLETE, datain);
function datain(evt:Event){
var arraygrid:Array = new Array();
testing.text = evt.target.Name2 // worked
// just say i = 1
i=1;
arraygrid.push({Name:this["evt.target.Name"+i],
Test:this.["evt.target.Test"+i]}); // error
//or
arraygrid.push({Name:this["Name"+i],
Test:this.["Test"+i]}); // error too
// eval() noexistent, set() didn't worked on actions frame panel
//?????
}
};
I hope it's very clear.
You could use this[varName] if I understand your question right.
So if varName is a variable containing a string which should be a variables name, you could set and read that variable like this:
this[varName] = "someValue";
trace(this[varName]);
Update:
In your example, you could try: evt.target["Test"+i] instead of Test:this.["evt.target.Test"+i]
If you have a set of strings that you'd like to associate with values, the standard AS3 approach is to use an object as a hash table:
var o = {}
o["test1"] = 7
o["test2"] = "fish"
print(o["test1"])