Duplicate and giving identity to movieclips - arrays

I have a question regarding array and snap function to ask about on ActionScript3
I am creating a simple cube game which requires players to drag cubes of different colors together and the cubes would snap on the right of each other.
Cubes are able to duplicate infinitely from a master stationary cube and they are all MovieClips.
I have conditioned cubes of different colors to snap into position as I've wanted but here is the problem...
Cubes for example red, will not snap together as each duplicated cube does not have its personal identity like red1, red2, red3 and so on as they do not know who to snap to due to the fact that both would be called reddup.
I've read up on duplicating cubes and numbering it at the same time using dynamic array, but I have no clue how to implement it into my game.

The answer is to not work with explicit names.
An instance name is just a reference to an object. But there are other ways to reference objects that do not require individual names. An array is an example for that. Just add the cube objects to the array:
var cubes:Array = [];
// then
cubes.push(new Cube());
You can then iterate over the array via an index:
for(var i:int = 0; i < cubes.length; i++)
{
cubes[i] // this is the i-th cube, use it like an instance name
}

Related

How to place a 3D object above the other one using ARKit?

I am working on a AR based POC using the sample given by Apple.
Is there a way to place a 3D object above another 3D object in SceneKit?
For example I have placed a table above which I have to place something else like a flower vase. How to achieve this?
Because as far as I know, ARKit is detecting only floor surface and if I try to place the flower vase over the already kept table, it is placing it under the table overlapping the existing 3D object. Is this doable?
When you're making hitTest, you could check if there's any nodes:
let results = sceneView.hitTest(checkLocation, options: [.boundingBoxOnly: true])
guard let node = results.first?.node else { return }
Then you can place your new node as child node for touched node.

Actionscript 3.0 - detecting multiple instances already placed on stage and adding them to an array

Right now I'm trying to make a game with dozens of the same, identical block being on stage and when the player hits the block, it stops. Right now, I've figured out how, using arrays, I can create multiple versions of the block automatically in code and place them on the stage, but I was wondering if there's any way that Actionscript can detect multiple instances of the same object placed on the screen by dragging and dropping in Flash, and adding those all to an array?
For example, instead of having the code place the blocks at specific x and y coordinates, I want to place them in the stage myself in the Flash editor and then have them all be a part of the same array or class or something that allows the same hit testing.
Thanks!
Make those blocks instance of a custom class (export to as -> custom class). Run a loop on the display list and check if the objects are instances of that custom class, eg:
for(var i:int = 0; i < this.numchildren; i++)
{
if(this.getChildAt(i) is MyCustomClass)
{
myarray.push(this.getChildAt(i));
}
}

ActionScript 3 - 2D array for a chessboard

I'm designing a chess game in ActionScript 3 using Flash Professional CC. I've created a chessboard using the IDE and placed pieces in their initial positions. Each tile has its own instance and is named its respective coordinate e.g. the top left tile is called A8.
For calculating moves that are valid and such, I planned to use two 2D arrays of objects. One array should contain the tile instances e.g. A8, B8, C8, D8 etc. and the other should contain the pieces of the board e.g. BR1, BB1.
I've noticed that ActionScript does not allow one to implement 2D arrays like C++ (a language I'm familiar with); instead, nested arrays are used. I'm slightly confused about how to set up these arrays. What is the most efficient way to declare and initialise these arrays (hopefully not involving repetitive code)?
welcome to the army of AS3 developers.
Here's few tips for you:
Arrays can be defined as var array:Array = []; and here's a 2d Array - var a:Array = [[]];. Arrays are dynamic object, you don't need to specify the depth of the array. So when adding tile to the array just add them via array[x][y] = tile
Having said that, don't use arrays :) You strictly typed collectors called Vectors. You need to define Vectors: 1d var myVector:Vector.< Tile > = new Vector.< Tile >(); and 2d version var myVector:Vector< Vector.< Tile > >; and so on.
Vectors are faster than Arrays.
More tips:
Saving black and write tiles in Library and constructing the grid on run-time might be better for you.
Accessing object on screen by their instance names is a bad idea - a lot of work and poor maintainability. Plus you won't be able to access them before the scene is finished building and you'll access them as Dynamic objects, you'll need to cast them to your needed class to work with them normally. That also leads to silent bugs since you won't get warning on compile time if you try to access something illegal.

Referring to a two dimensional array - rows and columns

I have a two dimensional array and I want to know how I can refer to the rows and columns in it. Do I use [row,column] or [column,row]? I also have some graphics. Do I calculate the (x,y) coordinate set of each graphic with (row*size,col*size) or with (col*size,row*size)?
The whole two dimensional array is the building instructions for the grid.
Each element in the array refers to a piece of the grid.
I know how to construct this grid and its pieces and I know how to access and manipulate the array's elements.
The problem is that when I construct the grid I have to calculate the x and y coordinate of each piece, but I just don't know if my variable curRow should be used for the x or y coordinate. It's similarly with the variable curCol.
My code is working, but it confuses me.
I think of it like the rows control the y coordinates and the columns control the x coordinates, because I just learned of the way matrices are referred to.
I ask, because it came to my mind that I am unsure of how to do this.
In the past I have used [row,column] to loop and (row*size,col*size) to position.
The code so far is:
function buildGrid(gridInfo:Array):Sprite {
var displaySprite:Sprite = new Sprite();
for(var curRow:uint=0;curRow<gridInfo.length;curRow++) {
for(var curCol:uint=0;curCol<gridInfo[curRow].length;curCol++) {
var infoRef:Object = gridInfo[curRow][curCol];//create reference for fast access
var pieceGraphic:Shape = new Shape();
pieceGraphic.graphics.beginFill(infoRef.fillColor);
pieceGraphic.graphics.lineStyle(infoRef.borderThickness,infoRef.theBorderColor);
pieceGraphic.graphics.drawRect(0,0,infoRef.sideLength,infoRef.sideLength);
pieceGraphic.graphics.endFill();
pieceGraphic.x = curRow*(infoRef.sideLength+infoRef.spaceX);//later use of graphic requires known x
pieceGraphic.y = curCol*(infoRef.sideLength+infoRef.spaceY);//later use of graphic requires known y
displaySprite.addChild(pieceGraphic);
}
}
return displaySprite;
}
It's correct to refer to a two dimensional array with [row,column], but it's easier with other variable names like curX and curY. It's important to be consistent of what the indexes mean throughout the project and what indexes are used. To access the first elements in the array create a loop for rows or for curX and for the elements in the second dimension create a loop for columns or for curY.
You can calculate coordinate sets (x,y) for graphics in a grid with (row*size,col*size) or (curX*size,curY*size), so your code is correct and doesn't need to be changed.
Remember that rows are horizontal and columns are vertical.

How can I create arrays within a loop (within another loop) and pass them back out in Javascript?

I'm using Google Docs Spreadsheet API to keep track of a competition between some of my friends. I have some big ideas, but I'm stumped right now. I'm trying to create 20 different arrays inside of loops (see below) and then evaluate each one outside of the loop, and I really don't want to write 20 "if...then" statements.
NOTE: the following SUMMARY may or may not help you answer my question. You might want to skip down to the code, then read this if you need it :)
Summary of the program: Each player assigns point values in favor of one possible outcome of a set of binary-outcome events. As the events happen, players either gain the points assigned if their outcome occurs, or gain no points if the opposite outcome occurs. My goal is to 1) figure out exactly when a player is eliminated, and 2) highlight all remaining events that must be won for them to have a chance at tying for first.
Instead of trying to somehow evaluate all possibilities (5 players picking, 2^16 outcomes... I have zero computer science knowledge, but this seems like an incredibly huge task even for the modern computer) I've come up with an alternate idea. The script loops through each player, against each other opponent. It calculates the maximum number of points a player can score based on their value assignments and the already determined game. For one player and one opponent, it checks the best possible outcome by the player against that opponent, and if there is any opponent he cannot beat, even in the best case, then he is eliminated.
This part is easy-- after the loop runs inside the loop, I just adjust a global variable that I created earlier, and when the outer loop is done, just grab those variables and write them to the sheet.
Unfortunately, this misses the case of where he could have a best case against each individual opponent, but not against multiple opponents at once.
So the next step is what I'm trying to do now. I'm not even sure I can give a good explanation without just showing you the entire spreadsheet w/script, but I'll try. So what I want to do now is calculate the "value" of each event for each player against a given other player. If both player and opponent assigned points in favor of the same event outcome for one event, the event's value is the difference between the picks (positive if player picked higher, negative if lower), and it's the SUM if they picked opposite event outcomes. Now, I do the same thing as before-- take a best-case scenario for a player against a given opponent-- but now I check by how much the player can beat the opponent in a best-case scenario. Then I evaluate the (absolute value of the) event value against this difference, and if it's greater, then the event is a must win (or must lose if the event value is negative). And, if an event is both a "must-win" and a "must lose" event, then the player is eliminated.
The problem is that this second step requires me to create a new array of values for each player-opponent combination, and then do things with the values after they're created.
I realize one approach would be to create 20 different arrays, and throughout the entire loops, keep checking "if (player == "1" && opponent == 2){}" and populate the arrays accordingly, but this seems kind of ridiculous. And more importantly, this entire project is my attempt at learning javascript, so what's the point in using a time-intensive workaround that doesn't teach me anything new?
I'm trying to understand square bracket notation, since it seems to be the answer to my question, but a lot of people are also suggesting that it's impossible to create variable names by concatenating with the value of another variable... so anyway, here's what I'm trying. I'd really appreciate either a fix to my approach, or a better approach.
for (var player=1; player<6; player++){
if(player==1){look up certain columns in the spreadsheet and save them to variables}
//ditto for other players
for(var opponent=1; opponent<6; opponent++){
if(player!=opponent){
if(opponent==1){save more values to variables}
//ditto for other players
for(var row=9; row<24; row++) {
//Now the script goes down each row of an array containing the original
//spreadsheet info, and, based on information determined by the variables
//created above, get values corresponding to the player and opponent.
//So what I'd like to do here is create "array[1,2]==" and then "array[1,3]=="
//and so forth, creating them globally (I think I understand that term by now)
//so I can refer to them outside of the loops later to do some evaluatin'.
}
}}
//get array[1,2]...array[5,4] and do some operations with them.
Thanks for getting through this little novel... really looking forward to your advice and ideas!
How can I create arrays within a loop (within another loop)?
Code update 2
As you said: "i am trying to understand square bracket notation" You may take a look at my new demo and the code:
function getTeam(){
var array = [[1,2,3],[4,5,6],[7,8,9]]; // arrays within arrays
// array myTeam
var myTeam = [[],[],[],[]];
var playerNames = ["John", "Bert", "Dave", "Milton"];
var ages =[];
var weight = 104;
// loop over the team arrayadd each player (name, age and weight) to the team
for (i=0; i < myTeam.length; i++){
// fill the age array in a loop
for (j=0;j<myTeam.length;j++) {
ages[j] = 23 + j;
}
myTeam[i].push([playerNames[i], ages[i], weight]);
}
return myTeam;
}
And pass them back out in Javascript
Could you elaborate on this part?
Update
var valuesOfPlayers=[];
for (var player=1; player<6; player++){
// look up certain columns in the spreadsheet and save them to variables
// you could call a funcntion and return the values you
// collected in an array within an array as in the demo above
valuesOfPlayers[player] = lookupColumnValues(player);
for(var opponent=1; opponent<6; opponent++){
if(player!=opponent){
// save more values to variables
valuesOfPlayers[player] = addValuesToVar(player);
}
for(var row=9; row<24; row++) {
// if you collect the values in your first and second if clause
// what other information do you want to collect
// Please elaborate this part?
}
}}
One workaround:
I could create an array before the execution of the loops.
At the start of each loop, I could push a string literal to the array containing the value of player and opponent.
After the loops are done, I could split the array into multiple arrays, or just evaluate them in one big array using regular expressions.
I'd still rather create new arrays each time-- seems like it is a more universal way of doing this, and learning how would be more educational for me than using this workaround.

Resources