Using this to compare objects (Actionscript 3) - arrays

I am trying to find the position of the caller of an Event inside the _ar_cards array, however this piece of code won't work. What is wrong?
for( var i = 0; i < 3; i++ )
{
if(this == _ar_cards[i])
{
mouseEvent.target.alpha = 0.1;
}
}

Just add trace functions in the cycle:
for (var i:int = 0; i < 3; ++i)
{
trace(this.name, _ar_cards[i].name);
if (this == _ar_cards[i])
{
trace("It works!");
mouseEvent.target.alpha = 0.1;
}
}
And you will see where is the problem.

Related

Is their a way to find the closest number to 0 in an Array?

I've got an array like that
var tideArray = new Array();
tideArray.push({tide:"haute1", difference: "-14"});
tideArray.push({tide:"haute2", difference: "-3"});
tideArray.push({tide:"basse1", difference: "-9"});
tideArray.push({tide:"basse2", difference: "4"});
tideArray.sortOn("difference", Array.NUMERIC);
trace(tideArray[0].tide);
For now, it's choosing the minimum number (-14) but I'd like to choose the closest number to 0.
Is there a way to do that ?
EDIT
I've tried that :
trace(closestToZero(tideArray));
function closestToZero(a:Array):int
{
var curDelta:int = Math.abs(0 - a[0].difference);
var curIndex:int = 0;
for(var i:int = 1; i < a.length; i++){
var thisDelta:int = Math.abs(0 - a[i].difference);
if(thisDelta < curDelta){
curIndex = i;
}
}
return curIndex;
}
But it seems that there is a mistake somewhere because the trace result is 3 (so it means that it's telling me that "basse2" (4) is the closest to 0... But, as you can see, it's "haute2" (-3) the closest).
I think it would be more efficient to simply loop over the array to find the item with the (absolute) minimum difference value:
if (tideArray.length > 0)
{
var minItem: Object = tideArray[0];
for (var index:int = 1; index < tideArray.length; index++)
{
if (Math.abs(tideArray[index].difference) < Math.abs(minItem.difference))
{
minItem = tideArray[index];
}
}
trace(minItem.tide);
}
Something like this
var tideArray = new Array();
...
function sortMyArray(a,b):int {
if (Math.abs(a) < Math.abs(b)) {
return -1;
}
if (Math.abs(a) > Math.abs(b)) {
return 1;
}
return 0;
}
tideArray.sort(sortMyArray);
Edit :
For your array.
function sortMyArray(a,b):int {
if (Math.abs(a.difference) < Math.abs(b.difference)) {
return -1;
}
if (Math.abs(a.difference) > Math.abs(b.difference)) {
return 1;
}
return 0;
}

.splice is not a function

I have this filter, and I have error filtered[i].splice is not a function
.filter('tsFilter', function() {
return function(model, filter) {
var filtered = [];
if (model != null) {
for (var i = 0; i < model.length; i++) {
filtered[i] = model[i];
}
}
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered[i].splice(i, 1);
}
return filtered;
};
})
Why I have this error? how to solve that?
Here you have to update
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered.splice(i, 1); //updated
}
Hope this will help you out.
It looks like you want to use:
for (i = 0; i < filtered.length; i++) {
if (filtered[i].List.length == 0)
filtered.splice(i, 1);
}
Although it is not a good idea to remove elements from the array that you are iterating over.

Group an array with same value in actionscript

I have an array:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
I want to array:
var resultArr:Array = [5,6,7,8,9,10,11,12,13,14];
This may use full to you.
var a:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
a.sort();
var i:int = 0;
while(i < a.length) {
while(i < a.length+1 && a[i] == a[i+1]) {
a.splice(i, 1);
}
i++;
}
for other, see here
Try this:
var exArr:Array = [5,6,10,6,5,11,7,9,12,8,8,13,7,9,14];
function group(subject:Array):Array
{
var base:Array = subject.slice().sort(Array.NUMERIC);
var prev:Number = base[0];
for(var i:int = 1; i < base.length; i++)
{
if(base[i] === prev)
{
base.splice(i, 1);
i--;
}
prev = base[i];
}
return base;
}
trace( group(exArr) );

AS3 Array object showing up by ordering

I have 3 MC on stage which are all alpha=0
var mcArray:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 0;
}
I have one button which once I click then it will make 1 of the MC become alpha=1
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 1;
}
}
But with the script above it will makes all 3 MC become alpha=1.
I know that this can achieve by using below code:
if(mc1.alpha!=1){
mc1.alpha=1
}
if(mc2.alpha!=1){
mc2.alpha=1
}
if(mc3.alpha!=1){
mc3.alpha=1
}
this code will give what I want to achieve but if there is more than 3 MC the lines of script will be longer.
revealBtn.buttonMode = true;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
var mcArray:Array = [mc0,mc1,mc2];
function revealClick(event:MouseEvent):void
{
for(var i:uint = 0; i<mcArray.length; i++){
if(this['mc'+i].alpha !== 1){
this['mc'+i].alpha = 1;
break;
}
}
}
No that if statement would still turn all three to alpha 1.
Which of the three do you want to set to alpha = 1 when the click is made?
You could use something like this to set alpha = 1 on the first mc in the array which does NOT have alpha = 1
function revealClick(event:MouseEvent):void {
for (var j:int = 0; j < mcArray.length; j++){
if ( mcArray[j].alpha != 1 ){
mcArray[j].alpha = 1;
return;
}
}
}
Just use a counter.
var cnt : int = -1;
function revealClick(event:MouseEvent):void
{
if(++cnt < mcArray.length) mcArray[cnt].alpha = 1;
}

[AS3]Randomly do something without repeat

I've 3 movieclip on stage which is mc1,mc2,mc3
at first they are alpha=0
What I want is when i click on revealBtn, 1 of them will show up as alpha=1.
But with my code below, sometimes I need to click about 5 times or more only can make all those mc show up.
Is there any solution for what I wanted? I've try splice but it's still not working well.
var mcArray:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < mcArray.length; j++)
{
mcArray[j].alpha = 0;
}
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
var i:Number = Math.floor(Math.random() * mcArray.length);
var movieClipToEdit:MovieClip = mcArray[i] as MovieClip;
movieClipToEdit.alpha = 1;
}
Here's one of the many possible solutions. It destroys the initial array though. If you don't want to change the initial array, the rest depends on what you actually want to achieve.
var invisibleList:Array = [mc1,mc2,mc3];
for (var j:int = 0; j < invisibleList.length; j++)
{
invisibleList[j].alpha = 0;
}
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
if (invisibleList.length == 0) {
return;
}
var i:Number = Math.floor(Math.random() * invisibleList.length);
var movieClipToEdit:MovieClip = invisibleList[i] as MovieClip;
invisibleList.splice(i, 1);
movieClipToEdit.alpha = 1;
}
Make a second array to use as your selection source. Every time you pick an item, Splice it from the second array. Also, since all your items are MovieClips you should use a Vector instead.
var mcVector:Vector.<MovieClip> = [mc1,mc2,mc3];
var vector2:Vector.<MovieClip> = mcVector.Slice(0); // This just copies the Vector
for (var j:int = 0; j < mcVector.length; j++)
{
mcVector[j].alpha = 0;
}
revealBtn.buttonMode = true;
revealBtn.useHandCursor = false;
revealBtn.addEventListener(MouseEvent.CLICK, revealClick);
function revealClick(event:MouseEvent):void
{
var i:Number = Math.floor(Math.random() * mcVector.length);
// Retrieves and deletes the item in one step:
var movieClipToEdit:MovieClip = vector2.Splice(i, 1);
movieClipToEdit.alpha = 1;
}

Resources