ActionScript 3 - 2D array for a chessboard - arrays

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.

Related

Using N-D interpolation with a generic rank?

I'm looking for an elegant way of useing ndgrid and interpn in a more "general" way - basically for any given size of input and not treat each rank in a separate case.
Given an N-D source data with matching N-D mesh given in a cell-array of 1D vectors for each coordinate Mesh={[x1]; [x2]; ...; [xn]} and the query/output coordinates given in the same way (QueryMesh), how do I generate the ndgrid matrices and use them in the interpn without setting a case for each dimension?
Also, if there is a better way the define the mesh - I am more than willing to change.
Here's a pretty obvious, conceptual (and NOT WORKING) schematic of what I want to get, if it wasn't clear
Mesh={linspace(0,1,10); linspace(0,4,20); ... linsapce(0,10,15)};
QueryMesh={linspace(0,1,20); linspace(0,4,40); ... linsapce(0,10,30)};
Data=... (whatever)
NewData=InterpolateGeneric(Mesh,QueryMesh,Data);
function NewData=InterpolateGeneric(Mesh,QueryMesh,Data)
InGrid=ndgrid(Mesh{:});
OutGrid=ndgrid(QueryMesh{:});
NewData=interpn(InGrid{:},Data,OutGrid{:},'linear',0.0)
end
I think what you are looking for is how to get multiple outputs from this line:
OutGrid = ndgrid(QueryMesh{:});
Since ndgrid produces as many output arrays as input arrays it receives, you can create an empty cell array in this way:
OutGrid = cell(size(QueryMesh));
Next, prove each of the elements of OutGrid as an output argument:
[OutGrid{:}] = ndgrid(QueryMesh{:});

How creating an array with array elements using Visual Basic

I want to work with a dynamic array using visual basic and I want that each element of the array will be an individual dynamic array too. How I should proceed? Thanks.
Read this - https://msdn.microsoft.com/en-us/library/aa716275(v=vs.60).aspx
And also this, in case you decide that reDim-ing things is annoying: https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx
Essentially though, you want to make an array and fill it with arrays. Then just treat those arrays independently for re-sizing purposes.

Swift 3 - Function to create n number of sprites with random x/y coordinates

I am trying to create multiple SKSpriteNodes that each have their own independent variables that I can change/modify. I would like to be able to run a function when the app starts, for example "createSprites(5)" which would create 5 sprites with the image/texture "shape.png" at random x and y coordinates and add all 5 Sprites to an array that I can access and edit different Sprite's positioning based on the index value. I would then like to be able to have another function "addSprite()" which, each time it is called, create a new Sprite with the same "shape.png" texture, place it at another random X and Y coordinate and also add it to the array of all Sprites to, again, be able to access later and change coordinates etc.
I have been looking through so many other Stack Overflow pages and can not seem to find a solution. My ideal solution would simply be the two functions I stated earlier. One to create an "n" number of Sprites and another function to create and add one more sprite to the array each time it is called.
Hope that makes sense, I'm fairly new to Swift and all this Sprite stuff, so simple informative answers would be very much appreciated.
You're not going to find an ideal solution from the past because nobody has likely had exactly the same desire with both Swift and SpriteKit. Having said that, there's likely partial answers you can blend together, and get the result you want or, at least, an understanding of how to do it.
Sprite Positioning in SK is probably the first thing to read up on:
https://developer.apple.com/library/content/documentation/GraphicsAnimation/Conceptual/SpriteKit_PG/Sprites/Sprites.html
having gotten that figured out, you can move to random positions.
Random positioning of Sprites:
Duplicate Sprite in Random Positions with SpriteKit
Sprite Kit random positions
Both use earlier versions of randomisation that aren't as powerful as what's available now, in GameplayKit. So... Generating random numbers in Swift with GameplayKit:
https://www.hackingwithswift.com/read/35/overview
It's hard to overstate the importance of understanding the various possibilities of game design implications of varying types of randomisation, so probably wise to read this, from Apple:
https://developer.apple.com/library/content/documentation/General/Conceptual/GameplayKit_Guide/RandomSources.html
After that, it's a case of needing to determine what constitutes a time or event at which to create more sprites at more random positions, and how fussy you want to be about proximity to other sprites, and overlaps.

Reinitialising an array after creation

Out of pure interest, why do most programming languages not allow the programmer to reinitialise an array after it's creation.
Example
int apples[4][4]
apples[0][1] = "blue"
apples = apples[8][8] // Reinitialise the array with a new size of 8x8
apples[7][4] = "purple"
Explanation of what I mean
As you can see above, I create an array that is 4x4, then I assign a value, then I reinitialise that same array with a new size of 8x8, then I assign another value. In theory, I'd prefer that it destroy the contents of the old array (so my new 8x8 array doesn't have that value at 0x1).
However, I've searched high and low, yet I've not managed to find anything that explains why programming languages enforce this restriction. In my eyes it seems greatly beneficial to allow this and I can't see any immediate issues. But clearly there is an issue otherwise this would be allowed.
Question
So my question is: What's the reason that programming languages do not allow programmers to reinitialise an array after it's creation?
Initialization can be done until code is not in running state, that's why initialization can take place only one time in a code, for the second time if you are initializing same array, its already out of creation phase and now in running phase.
for better clarity size of array needs to be a constant at compile time because it might be getting used in code at somewhere.
int i=arr.lenght;
if(i<5)
{
//do something}
you can resize a dynamic array by realloc() in c and by using collection properties with arrayList, but its not re initialization.

Is it possible to specify the type of an array in AS3?

In many languages you can specify that an array is of a certain type. For instance, in Java you could write:
String[] arrayOfStrings;
However in ActionScript 3 it seems that you can only specify that an object is of type Array, for instance:
var myArray:Array;
Is there a way to specify what type of object an AS3 array will contain?
You can use Vector.<String> to store several objects of the given type in an array. Vector is type-safe and is faster than Array so in almost all cases (when it's up to you) you should use Vector instead of Array.
I also recommend reading this article about the various ways to construct a vector. The article is from 2010 (so many Flash Player improvements have been done since then) but much of it still applies and you can download Jackson's test source to run the performance test on the current player.

Resources