Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

acctman

macrumors 65816
Original poster
Oct 26, 2012
1,323
856
Georgia
Hi currently my game is loading fl_gfood.png or fl_bfood.png from the fl_food.plist. That works fine but now I have 17 bad food items .png and 17 good food items .png. My question is how do I randomly display one of the 17 items from each group? I was told to load the plist into an array and then randomly grab an item filename using arc4random, but how do I do that?


Code:
@implementation Food

+ (void)loadAssets {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[FLSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[AssetHelper getDeviceSpecificFileNameFor:@"fl_food.plist"]];
    });

}

- (id)init {
    self = [super init];
    if (self) {
        self.size                   = CGSizeMake(16 * __HIGHRES_SCALE, 16 * __HIGHRES_SCALE);
        self.offset                 = CGPointMake(8 * __HIGHRES_SCALE, 8 * __HIGHRES_SCALE);
        self.removeAfterCollision   = YES;
        self.collideable            = NO;
        self.score                  = 10;
    }
    return self;
}

- (FLSprite *)sprite {
    if(_sprite == nil) {

        if(self.score < 10) {
            _sprite = [FLSprite spriteWithSpriteFrameName:@"fl_gfood.png"];
        } else {
            _sprite = [FLSprite spriteWithSpriteFrameName:@"fl_bfood.png"];
        }
    }
    return _sprite;
}
 

larswik

macrumors 68000
Sep 8, 2006
1,552
11
That should not be that hard. If you are storing png's in an array you must be storing them as NSData objects. Use the arcrRandom% myArray.count to set the max number of guesses. Then restore your PNG's from the NSData objects located at the selected index and add them as subViews to you view.

If arc4Random is confusing you google it. There are many examples out there, that is how I originally found a random number generator.
 

ArtOfWarfare

macrumors G3
Nov 26, 2007
9,563
6,062
The only things NSArrays can't hold are primitives (but boxing is now easy with the @ syntax) and structs (and unions, not that I'd expect anyone to be using those in an iOS app.)
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
My question is how do I randomly display one of the 17 items from each group? I was told to load the plist into an array and then randomly grab an item filename using arc4random, but how do I do that?

Divide-and-conquer, as it were. Describe to us, in pseudo-code if necessary, the steps you think are needed in order to solve this problem. Which steps are you comfortable coding yourself and with which steps do you require further assistance?
 

Duncan C

macrumors 6502a
Jan 21, 2008
853
0
Northern Virginia
The only things NSArrays can't hold are primitives (but boxing is now easy with the @ syntax) and structs (and unions, not that I'd expect anyone to be using those in an iOS app.)

Oops. I misread your post. I thought you said the only thing NSArrays CAN hold are primitives and structs.


As far as unions, I could see using them to look at pixel data as arrays of bytes or as RGB/RGBA groups, or something like that.

----------

Hi currently my game is loading fl_gfood.png or fl_bfood.png from the fl_food.plist. That works fine but now I have 17 bad food items .png and 17 good food items .png. My question is how do I randomly display one of the 17 items from each group? I was told to load the plist into an array and then randomly grab an item filename using arc4random, but how do I do that?


Code:
@implementation Food

+ (void)loadAssets {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        [[FLSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[AssetHelper getDeviceSpecificFileNameFor:@"fl_food.plist"]];
    });

}

- (id)init {
    self = [super init];
    if (self) {
        self.size                   = CGSizeMake(16 * __HIGHRES_SCALE, 16 * __HIGHRES_SCALE);
        self.offset                 = CGPointMake(8 * __HIGHRES_SCALE, 8 * __HIGHRES_SCALE);
        self.removeAfterCollision   = YES;
        self.collideable            = NO;
        self.score                  = 10;
    }
    return self;
}

- (FLSprite *)sprite {
    if(_sprite == nil) {

        if(self.score < 10) {
            _sprite = [FLSprite spriteWithSpriteFrameName:@"fl_gfood.png"];
        } else {
            _sprite = [FLSprite spriteWithSpriteFrameName:@"fl_bfood.png"];
        }
    }
    return _sprite;
}


Code:
        if(self.score < 10) {
            _sprite = [FLSprite spriteWithSpriteFrameName:@"fl_gfood.png"];
        } else {
            _sprite = [FLSprite spriteWithSpriteFrameName:@"fl_bfood.png"];
        }

Your code above is using a fixed filename to load a fixed good or bad sprite image.

What are the filenames for for your variety of images?

If you use filenames like
fl_gfood_0.png
fl_gfood_1.png
fl_gfood_2.png
fl_gfood_3.png
...
Then you could:

Generate a random number from 0 to 16 using arc4random_uniform(17)
Build a filename using that number and the NSString class method stringWithFormat.

Pass the resulting filename to your spriteWithSpriteFrameName method.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.