PDA

View Full Version : Help me shorten this.



youwh
May 11th, 2008, 07:27 PM
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.


ActionScript Code:

for (var i:Number=0; i < 2; i++) {
var imageRequest:URLRequest=new URLRequest("cards/pic9.jpg");
var imageLoader:Loader=new Loader;

imageLoader.load(imageRequest);

card=new Card ;
addChild(card);
card.setType(imageLoader);

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

imageLoader2.load(imageRequest2);

card=new Card ;
addChild(card);
card.setType(imageLoader2);

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

for (var i:Number=0; i < 2; i++) {
var imageRequest:URLRequest=new URLRequest("cards/pic31.jpg");
var imageLoader31:Loader=new Loader;

imageLoader31.load(imageRequest31);

card=new Card ;
addChild(card);
card.setType(imageLoader31);

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

imageLoader32.load(imageRequest32);

card=new Card ;
addChild(card);
card.setType(imageLoader2);

card.x=cardX;
card.y=cardY;
cardX+= card.width + 10;
card.addEventListener(MouseEvent.CLICK,checkCards) ;
cardArray.push(card);
}
//end with the latest/last card

wvxvw
May 12th, 2008, 03:57 AM
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.COMPLET E, 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.COMP LETE, cardLoaded);
l.contentLoaderInfo.removeEventListener(IOErrorEve nt.IO_ERROR, cardNotLoaded);
if(cardArray.length == 3){
trace('All cards ware loaded!');
for (var i:int=0; i<cardArray.length; i++){
bmp = cardArray[i];
bmp.y = i*70;
addChild(bmp);
}
} else {
loadCardNum(cardArray.length);
}
}
loadCardNum(0);


This is an example, hope it helps.

youwh
May 12th, 2008, 06:42 PM
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[i];
//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.

wvxvw
May 12th, 2008, 07:21 PM
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[i];
//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.

youwh
May 13th, 2008, 02:33 PM
Ah, now its working! Thank You very much :D

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[i];
//current bmp = what ever card in array
card = new Card;
addChild(card);
card.addEventListener(MouseEvent.CLICK,checkCards) ;
card.setType(allCardsArray[i]);

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!

wvxvw
May 13th, 2008, 06:33 PM
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?

youwh
May 13th, 2008, 07:07 PM
yup~ each card is a pair. so can match it.

wvxvw
May 13th, 2008, 07:24 PM
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.COMPLET E, 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.COMP LETE, cardLoaded);
l.contentLoaderInfo.removeEventListener(IOErrorEve nt.IO_ERROR, cardNotLoaded);
if(cardArray.length == 3){
trace('All cards ware loaded!');
for (var i:int=0; i<cardArray.length; i++){
bmp = cardArray[i];
bmp.y = i*70;
addChild(bmp);
bmp = cardCopyArray[i];
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.

youwh
May 14th, 2008, 05:56 PM
Thanx a lot! You've helped a lot! :D