Simple For loops not working on cs6? - actionscript-3

In my code I have a few for loops in the main timeline that look like this
for (i = 0; i<2*speedY; i++)
{
code
}
I've done this exact syntax many times and there have been no issues, however when I did it it gave me the error that i is undefined. I then tried the same loops defining i as var i:int; however now it just gave me a namespace error. What am I doing wrong here?

To avoid the undefined error, you have to define your variable i, but also to avoid the namespace error you should do that once. So you can do that like this :
var i:int;
// 1st for loop
for(i = 0; i < 5; i++){
trace('1 : '+i);
}
// 2nd for loop
for(i = 5; i > 0; i--){
trace('2 : ' + i);
}

If you only need the variable i within the loop itself and don't need the variable beyond the scope of the loop, you can also declare it within the loop parameters:
for(var i:int = 0; i < 5; i++) {
trace(i);
}
In terms of performance it's a marginal difference, however it's generally a good practice to declare variables only within the scope in which they will be used.

Related

How to batch the calls in google app script

I was trying to optimize runtime some code that ran really slowly, when searching it up google came up with this: https://developers.google.com/apps-script/guides/support/best-practices#:~:text=Use%20batch%20operations,-Scripts%20commonly%20need&text=Alternating%20read%20and%20write%20commands,data%20out%20with%20one%20command.
it shows an example of inefficient code:
var cell = sheet.getRange('a1');
for (var y = 0; y < 100; y++) {
xcoord = xmin;
for (var x = 0; x < 100; x++) {
var c = getColorFromCoordinates(xcoord, ycoord);
cell.offset(y, x).setBackgroundColor(c);
xcoord += xincrement;
}
ycoord -= yincrement;
SpreadsheetApp.flush();
}
and efficient code:
var cell = sheet.getRange('a1');
var colors = new Array(100);
for (var y = 0; y < 100; y++) {
xcoord = xmin;
colors[y] = new Array(100);
for (var x = 0; x < 100; x++) {
colors[y][x] = getColorFromCoordinates(xcoord, ycoord);
xcoord += xincrement;
}
ycoord -= yincrement;
}
sheet.getRange(1, 1, 100, 100).setBackgroundColors(colors);
I tried to understand how this code works and tried running it but first of all "cell" doesn't seem to get used and I do not understand the code at all. What is a version of the code that actually works and how does this make it more efficient? And what part of this code batches the calls and how can I use this in my own coding?
Basically, what it does it reduce the number of calls by its methods.
The inefficient code above calls offset and setBackgroundColor every loop thus making it resource intensive and time consuming.
For the efficient code, it uses the loop to build the 2d array which then will be used on a single call on the method setBackgroundColors to execute it by bulk.
What takes much of the run time is the method calls, so reducing it would be very beneficial. This would make use of loops to build 2d arrays for the array versions of the methods single execution.
e.g.:
setValue -> setValues
setBackgroundColor -> setBackgroundColors
Pseudocode:
from:
loop {
setValue(x[i][j])
}
to:
loop {
x[i][j] = data
}
setValues(x)

What does 'for' do in Action Script 3?

Just as title implies, my question is, what does the 'for' piece of code do in ActionScript 3? I apologize if this seems to be a stupid question; I've very recently started coding.
It's a type of loop.
for (var i:int = 0; i < 10; i++)
{
// Do something.
}
This says:
1. Create an int called i and set it to 0.
2. Check to see if i < 10. If not, stop executing the for loop and move on.
3. Do something.
4. Add 1 to i.
5. Go back to #2.
for is used to create a loop. It can be a loop trough an array:
var array:Array = [1,2,3];
for(var i:int = 0; i < array.length; i++) {
// do something
}
Or it could be an object.
var object:Object = {};
for(var i:String in object) {
// do something
}
Or you could just have an loop like
for(var i:int = 0; i < 10; i++) {
// do something
}
A for loop through children on stage / Movieclip:
for(var i:int = 0; i < numChildren; i++){
// do something
}
So you can do many things with the for.

Why is this AS3 code generating an "Error #2006: The supplied index is out of bounds"?

So what I'm trying to do is go through each element of the array "maps" which contains 4 movieclips and look and the children within each of those movieclips to see which are of type "Block". However I'm getting a #2006 error and I'm not sure why, can anyone help please?
function findBlocks()
{
trace("findBlocks()");
for (i=0; maps.length; i++)
{
for (var j=0; maps[i].numChildren; j++)
{
var mc = maps[i].getChildAt(j);
if (mc is Block)
{
blocks.push(mc);
}
}
}
trace("blocks array: " + blocks);
}
Your for loop conditions are incorrect, try this :
for (var i=0; i < maps.length; i++){
for (var j=0; j < maps[i].numChildren; j++){
var mc = maps[i].getChildAt(j);
if (mc is Block){
blocks.push(mc);
}
}
}
You have to remember that arrays and the display list start at 0, so the index of the last element in your lists is length-1, and in the case of a display list numChildren-1
i < maps.length
and
j < maps[i].numChildren
are what solve the problem

Warning: Duplicate variable definition

If I have 2 for loops both declaring var i, then the second will produce a warning:
public function twoLoops(){
for (var i:int = 0; i < 100; i++) {
}
for (var i:int = 0; i < 100; i++) { // i is already declared once
}
}
I understand the reason this happens, it's explained in this answer, but is there a way around it (except for declaring i at the beginning of the method)?
It's simple - just don't declare it in the second loop:
public function twoLoops(){
for (var i:int = 0; i < 100; i++) {
}
for (i = 0; i < 100; i++) { // i is already declared once
}
}
This will work with no error - as your warning tells you, it's already defined, so you can use it again, setting it back to 0 to allow the loop to execute properly.
If you are so adamant in using the loop the way you are using, consider wrapping up in a function:
public function twoLoops() {
for (var i:int = 0; i < 10; i++) {
}
(function(){
for (var i:int = 0; i < 100; i++) { // i is already declared once
}
})();
}
Though it would not give any warning, I wonder what purpose would it really solve for you.

How to remove/reload movieclip/graphic in Flash CS5 with AS3

I have a setupBoard(); and a setupBlocks(); in my function:
function init(e)
{
setupBoard();
removeEventListener(Event.ENTER_FRAME , init);
setupCat();
setupBlocks();
}
function setupBoard()
{
var columns:Array = new Array();
var i,j:int;
var _place:place;
for (i = 0; i < 11; i++)
{
columns = [];
for (j = 0; j < 11; j++)
{
_place = new place();
_place.thisX=i;
_place.thisY=j;
_place.thisDistance=Math.min(i+1,j+1,11-i,11-j)*11;
_place.y = 56 * i + 3;
_place.x = 5 + 71 * j + 35*(i%2);
_place.buttonMode=true;
_place.addEventListener(MouseEvent.CLICK, setBlock);
columns[j] = _place;
// SÆTTER TAL PÅ BRIKKERNE
_place.thisText.text = _place.thisDistance + " - " + _place.thisX + " : " + _place.thisY;
addChild(_place);
}
rows[i] = columns;
}
}
The "place" is the MovieClip
this function loads when the game launches and when the game is finish/completed..
the setupBoard, setup the board ofc, and the setupBlocks setup some movieclips, which contain some graphic.
Here's my question, how do I remove/reload all the blocks when the game enters that function again?
At the moment they are just placed upon each other, which I don't like at all.
If I understood correctly, what you want to do is remove all the previous blocks (from the last time you ran the setup function) when you run setup a second time.
To do that, you should create a function which loops your rows and columns Arrays, and for each Place object it find, it does the following: removes it from the stage, removes all Event Listeners, and finally sets it to null. Your function could look something like this (and you would call it just before calling setup again):
for (i = 0; i < rows.length; i++)
{
var column:Array = rows[i];
for (j = 0; j < column.length; j++)
{
var place:Place = column[j];
if (contains(place))
{
removeChild(place);
}
place.removeEventListener(MouseEvent.CLICK, setBlock);
place = null;
}
column = [];
}
row = [];
I just wrote that straight into the box, so it's not tested. But basically it does the three things necessary to get those objects removed from the view, and clears up anything that would stop them from being freed from memory by the garbage collector.
Hope that helps.
Debu