ActiveDen

2d arrays(?)

13 posts
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
boberic says
Not sure if 2d arrays will solve this issue, but its an idea. Essentially I want the following code to make a 5×5 array of boxes(that items can potentially be put into, but thats not important). So far I can get 1 line of boxes, but not quite sure how to get an array of them.
var itemsInInventory:Array = new Array;
var inventorySprite:Sprite = new Sprite;
var currentInvItem:Object = new Object;

addChild(inventorySprite);

var itemsArray:Array;
var numRows:Number = 5;
var numCols:Number = 5;

for(var m:Number = 0; m<numRows; m++){
    var invSpot:skSpot = new skSpot;

    invSpot.mouseChildren = false;
    inventorySprite.addChild(invSpot);

    invSpot.x = 55.5*m;
    //invSpot.sk_name.text = itemsArray[m];
    itemsInInventory.push(invSpot);
    inventorySprite.addChild(invSpot);
    //invSpot.name = String(m);
    for(var n:Number = 0; n<numCols; n++){
        invSpot.mouseChildren = false;
        inventorySprite.addChild(invSpot);
        invSpot.y = invSpot.height;
        invSpot.x = 55.5*m;
        //invSpot.sk_name.text = itemsArray[m];
        itemsInInventory.push(invSpot);
        inventorySprite.addChild(invSpot);
        //invSpot.name = String(m);
    }

}
5201 posts
  • Community Superstar
  • Italy
  • Sold between 10 000 and 50 000 dollars
  • Has been a member for 3-4 years
  • Microlancer Beta Tester
  • Beta Tester
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Author had a Free File of the Month
+2 more
doru says

your code is a little complicated but the nested “for loop” is a start. from what I understand from your code, the problem is that you create this “invSpot” in the first “for loop” then you define a position for it and then in the second “for loop” you give it another position.

the for loop should work like this: for x rows -> for each column(add a invSpot, give position and push it into an array to be referenced later)

for(var m:Number = 0; m<numRows; m++)
{
     for(var n:Number = 0; n<numCols; n++)
     {
         //create the object
         //add to your array
         //calculate x position = object width * n
         //calculate y position = object height * m
         //add to display
     }
}

hope I got this right I didn’t touch code for some time now

13 posts
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
boberic says
It seems like that should solve the issue however, I’m getting no Y positioning. It’s printing multiple lines, but on top of each other. So it still looks like I’ve only got one line.
for(var m:Number = 0; m<numRows; m++)//first for
{
     for(var n:Number = 0; n<numCols; n++)//second for
     {
         var invSpot:skSpot = new skSpot;//create the object
         inventorySprite.x = 50; //container for the invSpot's x position
         invSpot.mouseChildren = false; //dont want to interact with children.
         itemsInInventory.push(invSpot);//add to your array
         invSpot.Y = invSpot.height*m;//calculate y position = object height * m
         invSpot.x = invSpot.width*n;//calculate x position = object width * n
         inventorySprite.addChild(invSpot);//add to display
         invSpot.sk_name.text = itemsArray[m]; //text to be on the boxes. they all say empty.
         //invSpot.name = String(m);
     }
}
It would seem like it should work, unless I’m goofing something up…
5201 posts
  • Community Superstar
  • Italy
  • Sold between 10 000 and 50 000 dollars
  • Has been a member for 3-4 years
  • Microlancer Beta Tester
  • Beta Tester
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Author had a Free File of the Month
+2 more
doru says

It would seem like it should work, unless I’m goofing something up…
this:
 invSpot.Y = invSpot.height*m;//calculate y position = object height * m
should be:
 invSpot.y = invSpot.height*m;//calculate y position = object height * m

just a minor typo. :)

13 posts
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
boberic says
Sweet baby jesus…I ended up getting it to work…heres what i ended up doing:
var itemsInInventory:Array = new Array();
var rowOne:Array = [];
var rowTwo:Array = [];
var rowThree:Array = [];
var rowFour:Array = [];
var rowFive:Array = [];//THESE ARE PRETTY SELF EXPLANITORY
var inventorySprite:Sprite = new Sprite;//SKILL INVENTORY SPRITE
var currentInvItem:Object = new Object;//USELSESS..I SHOULD DELETE IT.
var skHeadSprite:Sprite = new Sprite;//SKILL WINDOW HEADER SPRITE
var fireTxtFormat:TextFormat = new TextFormat();//TEXT FORMAT VAR
/////TEXT FORMAT
fireTxtFormat.font = "DejaVu Sans";
fireTxtFormat.size = 16;
fireTxtFormat.color = 0xFF6600;

//var itemList:Array = new Array([sk1]);
//ALL ITEMS LISTED HERE!! THIS ARRAY WILL BE HUGE/////////////////////
var arrayOfItems:Array = new Array(sk1,sk2,sk3,sk4,sk5,sk6,sk7,sk8,sk9,sk10,sk11,sk12,sk13,sk14,sk15,sk16,sk17,sk18,sk19,sk20,sk21,sk22,sk23,sk24,sk25);
////////////////////////////////////////////////////////
//LISTENERS
stage.addEventListener(Event.ENTER_FRAME, makeInventoryItems);

//HEADER AND ITS CHILDREN/////
addChild(skHeadSprite);//ADD SKILL WINDOW HEADER
var skHead:skHeader = new skHeader; //HEADER CLASS
skHeadSprite.addChild(skHead); //ADD CHILD TO SPRITE
skHead.skTitle.text = "Fire Spells"; // TEXT FOR THE HEADER

/////FUNCTIONS!!!!!!///////////
function makeInventoryItems(e:Event) { //FUNCTION TO ADD ITEMS FROM ARRAYOFITEMS TO ITEMS LIST.
    skHead.x = inventorySprite.x;
    skHead.y = inventorySprite.y-skHead.height;
    skHead.width = inventorySprite.width;
    for(var r:int =0;r<arrayOfItems.length;r++) {
        arrayOfItems[r].addEventListener(MouseEvent.CLICK, getItem);
        arrayOfItems[r].buttonMode = true;

    }
}

function getItem(e:Event) { //GETS ITEM FROM STAGE AND MOVES IT TO INVENTORY
//SHIT GETS KIND OF CONFUSING FROM HERE
    var item:MovieClip = MovieClip(e.currentTarget); //WHAT I JUST CLICKED ON IS THE TARGET.
    trace("array size before="+itemsInInventory.length);
//////            BREAK DOWN FOR FUTURE REFERENCE:
//////    SO I CALCULATED THE POSITIONS OF TEH OBJECTS BASED ON THE LENGTH OF THE ARRAYS THEY WERE IN.
//////    SINCE THE itemsInInventory array is a single array and 2d arrays aren't possible...
//////    I decided to nest arrays into it equal to the number of rows I needed(5). First i calculated
//////    the length of row 1 which should be 5(0-4). Row 2 should be 6-10. Why 6 and not 5? because
//////    when we add rowTwo to the array it adds another value to the parent array of itemsInInventory!
//////    So we have to skip numbers to keep up. so from there its on to row three where things got
//////    tricky. After row 3 The code doesn't behave the same way. The way we were determining that we
//////    should go to the next row is by the length of the previous row's array. If the array was above
//////    4 then add the next array to the parent array. The only problem is for row one...we calculated
//////    using row ones array length since there was no preceeding array. On row 2 we used row ones length.
//////    This was a mistake! But I will leave it as is...because it works. I should've, instead, used row
//////    two's array length to calculate itself. like this:
//////        if(rowTwo.length<5){
//////            stuff...();
//////        }
//////    then I use a secondary if statement like in rows 3-5 to determine if we need to add another array 
//////    to the parent...

    if(itemsInInventory.length <=5){
        itemsInInventory.push(rowOne);
        trace("array size after="+itemsInInventory.length);
        if(rowOne.length <5){
            rowOne.push(item);
            inventorySprite.addChild(item);
            item.x = rowOne.length*item.width*1.02 - item.width-1;
            item.y = rowOne.height*item.height;

        }
    }
    if(itemsInInventory.length >=6){
        if(rowOne.length >4){
            itemsInInventory.push(rowTwo);
            trace("array size after="+itemsInInventory.length);
            rowTwo.push(item);
            inventorySprite.addChild(item);
            item.x = rowTwo.length*item.width*1.02 - item.width-1;
            item.y = item.height;
        }
    }
    if(itemsInInventory.length >=12){

        if(rowTwo.length >4){
            if(itemsInInventory.length <14){
                itemsInInventory.push(rowThree);
                trace("array size after="+itemsInInventory.length);
            }
            rowThree.push(item);
            inventorySprite.addChild(item);
            item.x = rowThree.length*item.width*1.02 - item.width-1;
            item.y = item.height+item.height;
        }

    }
    if(itemsInInventory.length >=18){

        if(rowThree.length >4){
            if(itemsInInventory.length <19){
                itemsInInventory.push(rowFour);
                trace("array size after="+itemsInInventory.length);
            }
            rowFour.push(item);
            inventorySprite.addChild(item);
            item.x = rowFour.length*item.width*1.02 - item.width-1;
            item.y = item.height+item.height+item.height;
        }

    }    
    if(itemsInInventory.length >=24){

        if(rowFour.length >4){
            if(itemsInInventory.length <24){
                itemsInInventory.push(rowFive);
                trace("array size after="+itemsInInventory.length);
            }
            rowFive.push(item);
            inventorySprite.addChild(item);
            item.x = rowFive.length*item.width*1.02 - item.width-1;
            item.y = item.height+item.height+item.height+item.height;
        }

    }    

}

addChild(inventorySprite);

inventorySprite.x = -400;
inventorySprite.y = stage.stageHeight -stage.stageHeight +40;
var itemsArray:Array = ["empty","empty","empty","empty","empty"]; //just somethings to print on the boxes.
var numRows:Number = 5; //5 rows
var numCols:Number = 5; //5 collumns
trace(itemsArray);
for(var m:Number = 0; m<numRows; m++)//first for
{
     for(var n:Number = 0; n<numCols; n++)//second for
     {

         var invSpot:skSpot = new skSpot;

         inventorySprite.addChild(invSpot);

         invSpot.y = invSpot.height*m;
         invSpot.x = invSpot.width*n-1;
         invSpot.sk_name.text = itemsArray[m];

     }
}
5201 posts
  • Community Superstar
  • Italy
  • Sold between 10 000 and 50 000 dollars
  • Has been a member for 3-4 years
  • Microlancer Beta Tester
  • Beta Tester
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Author had a Free File of the Month
+2 more
doru says

well :) I’m glad is working

13 posts
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
boberic says

yeah. Me too. Now the only thing left to do is to remove the event listener that calls getItem(); function and add a new one to use the item…but where to put the removeEventListener…hmmm

13 posts
  • Bought between 1 and 9 items
  • Has been a member for 1-2 years
boberic says

oh Cool! got that working. Only problem is it does things in the order that the objects are stored in the array. Is there a way to address specific objects in an array dynamically? Lets say I have 5 objects in an array and they are all on the screen. If I click on object 3 and add it to the inventory I want to remove the listeners from that object, but how do I know what object I’ve clicked? I guess what I’m asking is…is there a way to dynamically trace what objects are being pushed? tracing by the objects name, i would assume..

5201 posts
  • Community Superstar
  • Italy
  • Sold between 10 000 and 50 000 dollars
  • Has been a member for 3-4 years
  • Microlancer Beta Tester
  • Beta Tester
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Exclusive Author
  • Author had a Free File of the Month
+2 more
doru says

My AS3 got rusty :)

so I will write it like this:

click object with event listener (getItem() function is fired)
access object with the currentTarget
do my stuff
remove listeners on that object

so I think you can put the remove listeners inside the getitem function, but it depends on what you need to do.

by
by
by
by
by