I'm writing OCLint rule based on VisitBlockDecl using clang.
How can I output to console value of VarDecl *var variable?
static SmallVector<VarDecl *, 4> blockVars;
for (int i=0; i < blockVars.size(); i++) {
VarDecl *var = blockVars[i];
}
I created a level editor for web game, I can build, save, load and play levels.
Now I want to edit some levels but I have a weird situation.
I export a level as a single array, it looks like this 3,4,5,5,7,89,4,2,1...and those numbers represent frames. (tile-based).
Now if I want to edit this level and save it again, I need a level to be described as multidimensional array.
Actually, when I save the level I have a string that describes my map, then I convert string to array.
So can you tell me (if possible), how to convert this array1 (or string) to array2?
Lets say I have only 25 tiles (map from level editor is array1)
array1 =
1,1,1,1,1,
2,2,2,2,2,
3,3,3,3,3,
4,4,4,4,4,
5,5,5,5,5
I need this:
array2 =
[
[1,1,1,1,1],
[2,2,2,2,2],
[3,3,3,3,3],
[4,4,4,4,4],
[5,5,5,5,5]
];
UPDATE:
So I need 2d array to build level container.
I do not have experience with tile based games, but here you can see what I do.
Let's say I have 2d array and this is how I create a new level container:
for (i = 0; i < array2.length; i++)
{
for (var j = 0; j < array2[i].length; j++)
{
tile = new Tile();
tile.name = "" + i + j;
tile.x = j * tile.width;
tile.y = i * tile.height;
levelContainer.addChild(tile);
tile.gotoAndStop(array2[i][j]+1);
tile.addEventListener(MouseEvent.MOUSE_DOWN,
buildingLeve);
}
}
addChild(levelContainer);
I have tried to get 2d array from single array as Rudolfwm and Marcela suggested, but when I want to edit a level container using new array2, my tiles go on wrong frames.
For example, if correct frame is 1, tile goes to frame 11,
This code above (building level) works if I create my own 2d array, but not if I convert string to 2d array as suggested.
Try array1[x+y*row] which gives the same result as copying all your data to array2[x][y].
Or if you insist on 2d arrays:
var array2 = new Array(row);
for (var y = 0; y < row; y++) {
array2 [y] = new Array(column);
for(var x=0; x < column; x++) {
array2 [y][x]=array1[x+y*row];
}
}
You start with a String and convert that into an Array using String.split().
Once you have a temporary array, you use a nested loop to populate the final array (arr21).
var row:int = 5;
var column:int = 5;
var arr1:String = "1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5";
var tempArr:Array = arr1.split(',');
var arr2:Array = [];
for(var i:int = 0; i < row; i++)
{
arr2[i] = []; // create a new row array
for(var j:int = 0; j < column; j++)
{
// grab the first item from the temp array and push it onto the row array
arr2[i].push(tempArr.shift());
}
}
NOTE: This is not optimized, and could become quite laggy with larger level maps. This is just to give you an idea of where you can start.
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.
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
I'm just writing my first CUDA program, and it's actually a rewrite of a C++ code. Now it deals with a lot of vector maths, so I use the float4 datatype which provides exactly what I need. However, the old code contains a lot of
float *vec;
vec = new float[4];
for(int i=0; i<4; i++) vec[i] = ...;
Now with float4 all I can do is write a line for each .x,.y,.z,.w which I find a bit annoying. Is there no way of accessing float4 elements in a similar fashion, i.e.
float4 vec;
for(int i=0; i<4; i++) vec[i] = ...;
Unfortunately I couldn't find any hints on the internet.
Thanks in advance.
You could use a union, e.g.
typedef union {
float4 vec;
float a[4];
} U4;
U4 u;
for (int i = 0; i < 4; ++i) u.a[i] = ...;
For your arrays of float4 you would just change the underlying type to U4.
Note: technically it's UB to write to one variant of a union and then read from another, but it should work OK in this case and you don't need to worry about portability since this is CUDA-specific.
Probably not safe, but here is the easiest way.
float *vec;
vec = new float[4];
for(int i=0; i<4; i++) vec[i] = ...;
float4 vec4 = *(float4 *)vec;
Or you can flip this
float4 vec4;
float *vec = (float *)&vec4; // Do not free this pointer
for(int i=0; i<4; i++) vec[i] = ...;
EDIT
The only way to directly store into an array would be like this
float4 vec4 = {val[0], val[1], val[2], val[3]};
so if you have an array of float4s, you can do somehting like the following
float4 *vec4 = new float4[10];
float *vec = new float[4];
for(int i = 0; i < 10; i++) {
for(int j = 0; j < 4; j++) vec[j] = j;
vec4[i] = (float4){vec[0], vec[1], vec[2], vec[3]}
}
Other than this, I cant figure an easier way.