Loading plist into array and randomly grabbing item filename

Discussion in 'iOS Programming' started by acctman, Apr 15, 2013.

  1. acctman macrumors 6502a

    acctman

    Joined:
    Oct 26, 2012
    Location:
    Georgia
    #1
    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;
    }
    
     
  2. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #2
    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.
     
  3. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #3
    Not necessarily. NSArrsys can hold all types of objects, including UIImages.
     
  4. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #4
    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.)
     
  5. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #5
    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?
     
  6. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #6
    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.

    ----------


    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.
     
  7. larswik macrumors 68000

    Joined:
    Sep 8, 2006
    #7
    True. He said png files and I always store that data in NSData since you can write that to plists.
     
  8. acctman thread starter macrumors 6502a

    acctman

    Joined:
    Oct 26, 2012
    Location:
    Georgia
  9. Duncan C macrumors 6502a

    Duncan C

    Joined:
    Jan 21, 2008
    Location:
    Northern Virginia
    #9
    Did you solve your problem, then?
     

Share This Page