AS3 Array pulling specific information - arrays

I am using an array to keep information about 2 blocks that have landed. I then need to pull the spriteoffset from this array. How do I do this? My method below does not work.
aryBlockRow[(elementbody.yp + elementbody.block[ii].yp)/BLOCKSIZE][(elementbody.xp + elementbody.block[ii].xp)/BLOCKSIZE] = new ElementBlock((elementbody.xp + elementbody.block[ii].xp), (elementbody.yp + elementbody.block[ii].yp), elementbody.block[ii].spriteoffset);
trace("Block row: "+aryBlockRow.block[ii].spriteoffset)
Alternatively, each block has a tag (block[ii].tag), is it possible to pull this information from aryBlockRow, or is it not as I'm not specifically adding it?
Thanks
--
To clarify:
I am creating a tetris style game, however there are 4 different elements. In order to destroy the elements, I need to know when 4 of the same are in a line. The way I am planning to do this is having each of the elements having a variable ("tag") of either 0,1,2 or 3. I am calling the variable elementbody, and within it has an array "block".
trace("Left tag is: "+elementbody.block[0].tag)
trace("Right tag is: "+elementbody.block[1].tag)
This returns:
Left tag is: 3
Right tag is: 1
Which is correct.
What I now need to do is be able to continue to trace the tags once the elements have landed. As such, I am placing them into a new Array:
for(ii = 0; ii < elementbody.block.length; ii++) {
aryBlockRow[(elementbody.yp + elementbody.block[ii].yp)/BLOCKSIZE][(elementbody.xp + elementbody.block[ii].xp)/BLOCKSIZE] = new ElementBlock((elementbody.xp + elementbody.block[ii].xp), (elementbody.yp + elementbody.block[ii].yp),
elementbody.block[ii].spriteoffset);
Spriteoffset for each of the blocks (block[0], block[1]) will return either 0, 30, 60 or 90. If I can get this returned, then I can work out whether it has 4 in a row.
What I need to know is: Is it possible to pull "spriteoffset" from the aryBlockRow array, or do I need to somehow store this information elsewhere?
Elementbody changes to the new element everytime it lands on the bottom, and the existing is saved in this array, which is why I can not use my current method.

Related

Pinescript IF Statement Array

Hi i am struggling to get my array in Pinescript to produce anything other than a list of Nan. I am trying to create an array of the % difference of the low and 20sma when price bounces off the 20sma but currently when i print the array it only has Nan values.
sma_20 = sma(close,20)
sma_20_touch_band = open>sma_20 and low<=sma_20
sma_20_dif = ((low-sma_20)/sma_20)
sma_20_array = array.new_float(100)
if sma_20_touch_band
array.push(sma_20_array, sma_20_dif)
array.shift(sma_20_array)
That is most likely caused by not using a var array. Without the var keyword, your array will be re-initialized on each bar. You need to initialze your array once, and manipulate its elements later on. Therefore make it:
var sma_20_array = array.new_float(100)
Also, I'm not so sure about your usage of the array.shift() function.
You push something to the array, but with the array.shift() you remove the first element from the array. At the end of the day, you remove what you have just added. At least this is what I think is happening.

Calculate change for each element in Pine Script array

I want to calculate the percent change between periods of each element in the "features" array (simply using the array as a grouping of financial time series data to report on). However the way the script is working now, it seems that it wants to calculate the percent change between each element in the array and not FOR each element in the array.
I don't think I've done anything wrong here in how I reference the array elements but I get the feeling there's some sort of 'under the hood' concept about how variables are processed by TV that is causing this issue.
//#version=4
study("My Script")
pct_change(source, period) =>
now = source
then = source[period]
missing_now = na(now)
missing_then = na(then)
if not missing_now and not missing_then
(now - then) / abs(then)
else
missing_now ? 0 : 1
evaluate(sources) =>
s = array.size(sources)
bar_changes = array.new_float()
for i = 0 to 99999
if i < s
source = array.get(sources, i)
array.push(bar_changes, pct_change(source, 1))
continue
else
break
bar_changes
features = array.new_float()
array.push(features, open)
array.push(features, high)
array.push(features, close)
bar_changes = evaluate(features)
plot(pct_change(open, 1))
plot(array.get(bar_changes, 0))
plot(pct_change(high, 1), color=color.aqua)
plot(array.get(bar_changes, 1), color=color.aqua)
plot(pct_change(close, 1), color=color.red)
plot(array.get(bar_changes, 2), color=color.red)
I think you have come across the same problem I'm faced with, and it relates to using history referencing operator [] in connection with setting array element values.
I've boiled it down to a very simple script illustrating the problem
here.
In essence what you are doing in your code is passing array element to a pct_change() function, which uses [] operator, and then use returned result in array.push() to set array element value.
I've experienced weird results when I was trying to experiment with arrays in my scripts as soon as they've been introduced, so I started to dig in order to find the root of the problem. And it came down to the script referenced in the link above. So far I believe that Pine Script still has some bugs when it comes to arrays so we just have to wait until they'll be fixed.

Conditional Statement + UITableView: Repeat values in array over and over

I found this hard to put a title on so please feel free to modify it.
I have an array that contains 5 CGSizes that I use in cellForRowAtIndexPath.
NSArray *mElements = [NSArray arrayWithObjects:[NSValue valueWithCGSize:
CGSizeMake(600.0, 900.0)],
[NSValue valueWithCGSize:CGSizeMake(300.0, 800.0)],
[NSValue valueWithCGSize:CGSizeMake(546.0, 1032.0)],
[NSValue valueWithCGSize:CGSizeMake(300.0, 700.0)],
[NSValue valueWithCGSize:CGSizeMake(300.0, 800.0)],
nil];
I currently have it that a random size is taken from the array and applied to each cell, giving it a staggered layout. However the problem is that if I reload the UITableView all the rows will have different sizes, which is obvious because it will pick a random value each time.
int random = arc4random_uniform((uint32_t) mElements.count);
CGSize size =[[mElements objectAtIndex:random] CGSizeValue];
However the effect I want is that row 0 will always have the same size, possibly the index 0 of the array. Then row 1 to have the value at index 1, and then row 2 to have the value at index 2, and so on. I would then like the row at index 4 to have the value at index 0 of the array (so the array starts again). I have tried this, but it requires too much hardcoding.
if (indexPath.item%2 && !indexPath.item%4) {
size =[[mElements objectAtIndex:1] CGSizeValue];
}
else if(indexPath.item%3 && !indexPath.item%6){
size =[[mElements objectAtIndex:2] CGSizeValue];
}
else if(indexPath.item%4){
size =[[mElements objectAtIndex:2] CGSizeValue];
}
//... and so on
Im hoping to get help in creating a dynamic conditional state where I do not have to hard code every possible row index in the table. I can't seem to get my head around it though.
size = [[mElements objectAtIndex:indexPath.row % [mElements count]] CGSizeValue];
That'd give you a constant repeating pattern all the way down.
Number of rows could be
return [mElements count]*ROW_MULTIPLYER;
where
#define ROW_MULTIPLYER 10000

How to utilize arrays in corona?

I have created this text array and image array.
How do i make it such that when a random image appears the corresponding text appears?
Example: When the image shows 9, I want the text displayed to be 9
I really need help.
Are you looking for getting the index of arrays in Lua? I'll just provide a small example.
You can create an array like:
local myArray = {"element_1","element_2",...,"element_n"}
And you can get an array element as:
print(myArray[1]) -- Prints 'element_1' (1st element)
print(myArray[n]) -- Prints 'element_n' (nth element)
So, if you have two arrays as below;
local myQuestionArray = {"1","2",...,"n"}
local myImageArray = {"img_1","img_2",...,"img_n"}
you can take the question and image with respect to a number as:
-- for number = 1
print(myQuestionArray[1]); // 1
print(myQuestionArray[1]); // img_1
-- for number = n
print(myQuestionArray[n]); // n
print(myQuestionArray[n]); // img_n
For mode details, refer: Understanding Lua tables in Corona SDK
Keep Coding.......... :)

Inserting data into an array sequentially

I am currently trying to figure out how to design some sort of loop to insert data into an array sequentially. I'm using Javascript in the Unity3D engine.
Basically, I want to store a bunch of coordinate locations in an array. Whenever the user clicks the screen, my script will grab the coordinate location. The problem is, I'm unsure about how to insert this into an array.
How would I check the array's index to make sure if array[0] is taken, then use array[1]? Maybe some sort of For loop or counter?
Thanks
To just add onto the end of an array, just use .push().
var myArray = [];
var coord1 = [12,59];
var coord2 = [87,23];
myArray.push(coord1);
myArray.push(coord2);
myArray, now contains two items (each which is an array of two coordinates).
Now, you wouldn't do it this way if you were just statically declaring everything as I've done here (you could just statically declare the whole array), but I just whipped up this sample to show you how push works to add an item onto the end of an array.
See https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/push for some reference doc on push.
In case you need to know the array's length when reading the array in the future, you can use the .length attribute.
var lengthOfArray = myArray.length;
Using the .push() method as suggested by jfriend00 is my recommendation too, but to answer your question about how to work out what the next index is you can use the array's length property. Because JavaScript arrays are zero-based The length property will return an integer one higher than the current highest index, so length will also be the index value to use if you want to add another item at the end:
anArray[anArray.length] = someValue; // add to end of array
To get the last element in the array you of course say anArray[anArray.length-1].
Note that for most purposes length will give the number of elements in the array, but I said "one higher than the current highest index" above because JavaScript arrays are quite happy for you to skip indexes:
var myArray = [];
myArray[0] = "something";
myArray[1] = "something else";
myArray[50] = "something else again";
alert(myArray.length); // gives '51'
// accessing any unused indexes will return undefined

Resources