Author Topic: Behave array!  (Read 1295 times)

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Behave array!
« on: Wed, Sep 26, 2012 »
Ugh.

I need to get this array to behave. I have one that creates a row of 5 sprites to show off an example of some filters. That single row array looks like this:

Code: [Select]

coins = new Array();

for (var i:int = 0; i < 6; i ++)
{
coins[i] = new FilterCoin();
//coins[i].y = 100;
coins[i].x = 20 + (i * 128);
add(coins[i]);
coins[i].play("spin");


}

Now. What I want to do, is create a 4 height by 5 wide array of objets that are 64x64 each and I want them placed like this: The first one is placed manually, then the next one in the row is 64 pixels away and so on. And the object below it is also 64 pixels. Now I have something from an arcanoid and minesweeper example that places an array of objects in a grid. Here is that one:

Code: [Select]
var bx:int = 10;
var by:int = 30;

var brickColours:Array = [ 0xffd03ad1, 0xfff75352, 0xfffd8014, 0xffff9024, 0xff05b320, 0xff6d65f6 ];

for (var y:int = 0; y < 6; y++)
{
for (var x:int = 0; x < 20; x++)
{
var tempBrick:FlxSprite = new FlxSprite(bx, by);
tempBrick.velocity.y = 2;
tempBrick.makeGraphic(15, 15, brickColours[y]);
tempBrick.immovable = true;
bricks.add(tempBrick);
bx += 15;
}

bx = 10;
by += 15;
}

I'm able to tweak the one above just a little bit. But I don't really understand it. How can I get an array to do what I described?

Thanks in advance!

Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Behave array!
« Reply #1 on: Wed, Sep 26, 2012 »
Hmm, the first thing I notice that if you have "for (var i:int = 0; i < 6; i ++)", it will actually loop 6 times as i goes 0, 1, 2, 3, 4, 5, and then at 6 it stops as 6 is no longer < 6.

Anyways, here's some similar code I use frequently in my projects that's generic enough that it should fit your uses I think...

Code: [Select]
const ROWS:uint = 5;
const COLS:uint = 4;

const START_X:uint = 200;
const START_Y:uint = 100;

const SPACING_X:uint = 64;
const SPACING_Y:uint = 64;

for (var row:uint = 0; row < ROWS; row++)
{
for (var col:uint = 0; col < COLS; col++)
{
var sprite:FlxSprite = new FlxSprite(START_X + col * SPACING_X, START_Y + row * SPACING_Y);
add(sprite);
}
}

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Behave array!
« Reply #2 on: Fri, Sep 28, 2012 »
Hey thanks!

While that does work. It has it's own problems too.


The blue boxes are the ones I hand coded in and the green boxes are created via array.

Notice that a lot of the spacing is off for some of them. What causes that? Also. I need to get them into an array so that in the playstate I can go board.boxes[1].x. Or does flxgroup do something like that?
« Last Edit: Fri, Sep 28, 2012 by xhunterko »
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

wg/funstorm

  • Global Moderator
  • Key Contributor
  • *****
  • Posts: 596
  • Karma: +0/-0
    • View Profile
    • Funstorm
Re: Behave array!
« Reply #3 on: Fri, Sep 28, 2012 »
Your blue boxes are 64x64, spaced apart 120 on the x-axis and 90 on y-axis.
Your green boxes are 62x62, spaced apart 128 on the x-axis and 90 on y-axis.

Looks to me like you just used different values when hardcoding and looping.

FlxGroup has a "members" array. When you add something to a group, it gets added to the members array. If you only add the boxes to the group, and don't add/remove any, you can access them using myGroup.members[num] and they will be in the order you added them. But if you do other things with the group, you will get other objects in there, and they may no longer be in the order you expect.

For convenience, I would probably store a set of references to the pieces in a 2d array like so:

Code: [Select]
const ROWS:uint = 5;
const COLS:uint = 4;

const START_X:uint = 200;
const START_Y:uint = 100;

const SPACING_X:uint = 64;
const SPACING_Y:uint = 64;

var myGroup:FlxGroup = new FlxGroup();
var gameBoard:Array = new Array();

for (var col:uint = 0; col < COLS; col++)
{
gameBoard.push(new Array());
for (var row = 0; row< ROWS; row++)
{
var sprite:FlxSprite = new FlxSprite(START_X + col * SPACING_X, START_Y + row * SPACING_Y);
myGroup.add(sprite);
gameBoard[col].push(sprite);
}
}

Then you can access your pieces through gameBoard[col][row]

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Behave array!
« Reply #4 on: Fri, Sep 28, 2012 »
Quote
Your blue boxes are 64x64, spaced apart 120 on the x-axis and 90 on y-axis.
Your green boxes are 62x62, spaced apart 128 on the x-axis and 90 on y-axis.

Okay. But I did that to see if I was losing the sprites behind the originals and not seeing them. I just couldn't see the originals so I changed the background color. If they were the same size as the originals, the exact same thing still happens. Regardless of size. So I dunno what's causing that. They are supposed to spaced apart like the originals are though. I thought they were 64x64 but they are 128 pixels away and 90 pixels down from each other. I got the numbers wrong in the first post my bad. But I didn't know you could do that with groups. I'll see about trying something like you suggested there. Any ideas on what might be wrong with the grid?
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha

xhunterko

  • Contributor
  • ****
  • Posts: 449
  • Karma: +0/-0
    • View Profile
Re: Behave array!
« Reply #5 on: Wed, Oct 3, 2012 »
Hey again!

Just wanted to say thanks for helping out. I finally got it to work with a custom object last night and also took a second look at it. Apparently, my original objects are not evenly spaced. At least, I don't think so. But these in this structure are. So thanks again!
Now on twitter: http://twitter.com/xhunterko I made a game that's in alpha you can buy here: http://xhunterko.itch.io/wave-miner-alpha