Help me shorten this

I have some scripts that loads in external jpeg for a flip and match game. Currently, for every 2 cards I have a for loop to call in the image, and after 10 card is load and duplicate, it will go to the 2nd line. If I have 60 cards on the board, that means I need to have duplicate almost 30 sets of coding with the arrangement of card in grid. I know this is not a smart way to do it, can anyone help me on this?

And the bigger problem I think is, the jpeg loaded into the card. I want to load in the last/latest 30 pictures as the card image. Because I will be constantly putting new images into the picture folder.

This is currently how I am doing.


[FONT=monospace]      ActionScript Code:
     [LEFT][COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]**var**[/COLOR] i:[COLOR=#0000ff]Number[/COLOR]=[COLOR=#000080]0[/COLOR]; i < [COLOR=#000080]2[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
                [COLOR=#000000]**var**[/COLOR] imageRequest:URLRequest=[COLOR=#000000]**new**[/COLOR] URLRequest[COLOR=#000000]([/COLOR][COLOR=#ff0000]"cards/pic9.jpg"[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#000000]**var**[/COLOR] imageLoader:Loader=[COLOR=#000000]**new**[/COLOR] Loader;

                imageLoader.[COLOR=#0000ff]load[/COLOR][COLOR=#000000]([/COLOR]imageRequest[COLOR=#000000])[/COLOR];

                card=[COLOR=#000000]**new**[/COLOR] Card  ;
                addChild[COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
                card.[COLOR=#000080]setType[/COLOR][COLOR=#000000]([/COLOR]imageLoader[COLOR=#000000])[/COLOR];

                card.[COLOR=#000080]x[/COLOR]=cardX;
                card.[COLOR=#000080]y[/COLOR]=cardY;
                cardX+= card.[COLOR=#0000ff]width[/COLOR] + [COLOR=#000080]10[/COLOR];
                card.[COLOR=#000080]addEventListener[/COLOR][COLOR=#000000]([/COLOR]MouseEvent.[COLOR=#000080]CLICK[/COLOR],checkCards[COLOR=#000000])[/COLOR];
                cardArray.[COLOR=#0000ff]push[/COLOR][COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
            [COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]**var**[/COLOR] j:[COLOR=#0000ff]Number[/COLOR]=[COLOR=#000080]0[/COLOR]; j < [COLOR=#000080]2[/COLOR]; j++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
                [COLOR=#000000]**var**[/COLOR] imageRequest2:URLRequest=[COLOR=#000000]**new**[/COLOR] URLRequest[COLOR=#000000]([/COLOR][COLOR=#ff0000]"cards/pic10.jpg"[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#000000]**var**[/COLOR] imageLoader2:Loader=[COLOR=#000000]**new**[/COLOR] Loader;

                imageLoader2.[COLOR=#0000ff]load[/COLOR][COLOR=#000000]([/COLOR]imageRequest2[COLOR=#000000])[/COLOR];

                card=[COLOR=#000000]**new**[/COLOR] Card  ;
                addChild[COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
                card.[COLOR=#000080]setType[/COLOR][COLOR=#000000]([/COLOR]imageLoader2[COLOR=#000000])[/COLOR];

                card.[COLOR=#000080]x[/COLOR]=cardX;
                card.[COLOR=#000080]y[/COLOR]=cardY;
                cardX+= card.[COLOR=#0000ff]width[/COLOR] + [COLOR=#000080]10[/COLOR];
                card.[COLOR=#000080]addEventListener[/COLOR][COLOR=#000000]([/COLOR]MouseEvent.[COLOR=#000080]CLICK[/COLOR],checkCards[COLOR=#000000])[/COLOR];
                cardArray.[COLOR=#0000ff]push[/COLOR][COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
_cardX = [COLOR=#000080]45[/COLOR]
_cardY+= _card.[COLOR=#0000ff]height[/COLOR] + [COLOR=#000080]50[/COLOR] [COLOR=#808080]*//next line of cards*[/COLOR]
[COLOR=#808080]*// and after some 20 or 30 cards loaded in...*[/COLOR]

[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]**var**[/COLOR] i:[COLOR=#0000ff]Number[/COLOR]=[COLOR=#000080]0[/COLOR]; i < [COLOR=#000080]2[/COLOR]; i++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
                [COLOR=#000000]**var**[/COLOR] imageRequest:URLRequest=[COLOR=#000000]**new**[/COLOR] URLRequest[COLOR=#000000]([/COLOR][COLOR=#ff0000]"cards/pic31.jpg"[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#000000]**var**[/COLOR] imageLoader31:Loader=[COLOR=#000000]**new**[/COLOR] Loader;

                imageLoader31.[COLOR=#0000ff]load[/COLOR][COLOR=#000000]([/COLOR]imageRequest31[COLOR=#000000])[/COLOR];

                card=[COLOR=#000000]**new**[/COLOR] Card  ;
                addChild[COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
                card.[COLOR=#000080]setType[/COLOR][COLOR=#000000]([/COLOR]imageLoader31[COLOR=#000000])[/COLOR];

                card.[COLOR=#000080]x[/COLOR]=cardX;
                card.[COLOR=#000080]y[/COLOR]=cardY;
                cardX+= card.[COLOR=#0000ff]width[/COLOR] + [COLOR=#000080]10[/COLOR];
                card.[COLOR=#000080]addEventListener[/COLOR][COLOR=#000000]([/COLOR]MouseEvent.[COLOR=#000080]CLICK[/COLOR],checkCards[COLOR=#000000])[/COLOR];
                cardArray.[COLOR=#0000ff]push[/COLOR][COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
            [COLOR=#000000]}[/COLOR]
            [COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000]**var**[/COLOR] j:[COLOR=#0000ff]Number[/COLOR]=[COLOR=#000080]0[/COLOR]; j < [COLOR=#000080]2[/COLOR]; j++[COLOR=#000000])[/COLOR] [COLOR=#000000]{[/COLOR]
                [COLOR=#000000]**var**[/COLOR] imageRequest32:URLRequest=[COLOR=#000000]**new**[/COLOR] URLRequest[COLOR=#000000]([/COLOR][COLOR=#ff0000]"cards/pic32.jpg"[/COLOR][COLOR=#000000])[/COLOR];
                [COLOR=#000000]**var**[/COLOR] imageLoader32:Loader=[COLOR=#000000]**new**[/COLOR] Loader;

                imageLoader32.[COLOR=#0000ff]load[/COLOR][COLOR=#000000]([/COLOR]imageRequest32[COLOR=#000000])[/COLOR];

                card=[COLOR=#000000]**new**[/COLOR] Card  ;
                addChild[COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
                card.[COLOR=#000080]setType[/COLOR][COLOR=#000000]([/COLOR]imageLoader2[COLOR=#000000])[/COLOR];

                card.[COLOR=#000080]x[/COLOR]=cardX;
                card.[COLOR=#000080]y[/COLOR]=cardY;
                cardX+= card.[COLOR=#0000ff]width[/COLOR] + [COLOR=#000080]10[/COLOR];
                card.[COLOR=#000080]addEventListener[/COLOR][COLOR=#000000]([/COLOR]MouseEvent.[COLOR=#000080]CLICK[/COLOR],checkCards[COLOR=#000000])[/COLOR];
                cardArray.[COLOR=#0000ff]push[/COLOR][COLOR=#000000]([/COLOR]card[COLOR=#000000])[/COLOR];
            [COLOR=#000000]}
//end with the latest/last card
[/COLOR][/LEFT]
[/FONT]

[FONT=monospace][LEFT]
[/LEFT]

[/FONT]

var cardArray:Array = [];
var l:Loader;
var r:URLRequest;
var loc:String = 'cards/pic%.jpg';
function loadCardNum(n:int):void {
 l = new Loader();
 r = new URLRequest(loc.replace('%',n));
 trace('Will load card from: '+loc.replace('%',n));
 l.contentLoaderInfo.addEventListener(Event.COMPLETE, cardLoaded);
 l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, cardNotLoaded);
 l.load(r);
}
function cardNotLoaded(evt:IOErrorEvent):void {
 trace('Unable to load card!');
}
function cardLoaded(evt:Event):void {
 var bmp:Bitmap = Bitmap(l.content);
 cardArray.push(bmp);
 l.contentLoaderInfo.removeEventListener(Event.COMPLETE, cardLoaded);
 l.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, cardNotLoaded);
 if(cardArray.length == 3){
  trace('All cards ware loaded!');
  for (var i:int=0; i<cardArray.length; i++){
   bmp = cardArray*;
   bmp.y = i*70;
   addChild(bmp);
  }
 } else {
  loadCardNum(cardArray.length);
 }
}
loadCardNum(0);

This is an example, hope it helps.

Hi wvxvw. Thanx for helping out here. I was going through your script, and theres few line of coding I’m not sure what and how does it actually run.

function loadCardNum(n:int):void
r = new URLRequest(loc.replace('%',n);

Whats the role of n here?
How does it actually know which card, and how many to load?

for (var i:int=0; i<allCardsArray.length; i++) {
					bmp = allCardsArray*;
					//current bmp = what ever card in array 
					bmp.y = i*70;
					addChild(bmp);
				}

This code seems like loading all the cards in a same line?
How do it know when to go for the next line?


function cardNotLoaded(event:IOErrorEvent):void
function cardLoaded(e:Event):void

I get error 1046:1046: Type was not found or was not a compile-time constant: for this 2

Thanx again for helping.

Ok.

function loadCardNum(n:int):void
r = new URLRequest(loc.replace('%',n);

loadCardNum expects integer as an argument (the integer you get by checking total sucsessful downloads number in the cardLoaded function.
String.replace(pattern, value) works like this:
Usually you’d use regExp as the first argument, but string is also ok, for the second argument you’d usually pass a string, but any other data type will be automatically converted to string, like if you’ll pass an object, it’ll convert it to ‘[object Object]’, array -> values of an array joined by commas, numbers -> like if you called Number.toString(10) etc.
Consider you have this string:
'abcd’
you want to change ‘b’ to ‘x’
‘abcd’.replace(‘b’,‘x’) will give you the result you want.

for (var i:int=0; i<allCardsArray.length; i++) {
                    bmp = allCardsArray*;
                    //current bmp = what ever card in array
                     bmp.y = i*70;
                    addChild(bmp);
                }

I don’t know how many rows/columns you want, this section will place images sequentaly in the order they ware loaded one below another with the space equal to 70 pixels between their top edges. If you need to make it, say, 3 rows by 3 columns (each column = 10 images) from the total of 30 images than:
bmp.y = (i%10)*70;
bmp.x = Math.floor(i/10)*70;
the result will look like this:
1 | 11 | 21
2 | 12 |
3 | 13 |
4 | 14 | … etc.

For the last: you need to import flash.events.IOErrorEvent.

Ah, now its working! Thank You very much :smiley:

Just one more question, how do you loop the array picture twice?

for (var j:int = 0; j<2; j++) {
					x = 10;
					y = 10;
					for (var i:int=0; i<allCardsArray.length; i++) {
						bmp = allCardsArray*;
						//current bmp = what ever card in array 
						card = new Card;
						addChild(card);
						card.addEventListener(MouseEvent.CLICK,checkCards);
						card.setType(allCardsArray*);
						
						card.y = (i%6)*92;
						//each colum have 6 card, with the height of 90

						card.x = Math.floor(i/6)*72;
						//x distance = 70, for 6 columns
					}
				}

I’m currently looping it inside another loop, and the duplicate things seems overlapping.

Thanx again!

You may loop through cardArray as many times as you wish after all the downloads finished. So, at any time after the download you may reference the picture (card) like cardArray[n] where n is some number from 0 to 35.

Hm… seems like I dont quite get the question… do you need to duplicate all the cards?

yup~ each card is a pair. so can match it.

var cardArray:Array = [];
var cardCopyArray:Array = [];
var l:Loader;
var r:URLRequest;
var loc:String = 'cards/pic%.jpg';
function loadCardNum(n:int):void {
	l = new Loader();
	r = new URLRequest(loc.replace('%',n));
	trace('Will load card from: '+loc.replace('%',n));
	l.contentLoaderInfo.addEventListener(Event.COMPLETE, cardLoaded);
	l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, cardNotLoaded);
	l.load(r);
}
function cardNotLoaded(evt:IOErrorEvent):void {
	trace('Unable to load card!');
}
function cardLoaded(evt:Event):void {
	var bmp:Bitmap = Bitmap(l.content);
	var bmpd:BitmapData = bmp.bitmapData.clone();
	var bmp_copy:Bitmap = new Bitmap(bmpd);
	cardArray.push(bmp);
	cardCopyArray.push(bmp_copy);
	l.contentLoaderInfo.removeEventListener(Event.COMPLETE, cardLoaded);
	l.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, cardNotLoaded);
	if(cardArray.length == 3){
		trace('All cards ware loaded!');
		for (var i:int=0; i<cardArray.length; i++){
			bmp = cardArray*;
			bmp.y = i*70;
			addChild(bmp);
			bmp = cardCopyArray*;
			bmp.y = i*70;
			bmp.x = 100;
			addChild(bmp);
		}
	} else {
		loadCardNum(cardArray.length);
	}
}
loadCardNum(0);

I’d do it like this, but there’re lots of possible ways to do it.

Thanx a lot! You’ve helped a lot! :smiley: