0

I have an array that has 2 balloon textures,one green and one red.I have 12 green balloon object at the start of the app.Basically I want one random green balloon to turn red every one second.If anyone can help it would be appreciated.

init method

    balloonTextures = [NSMutableArray array];
    [balloonTextures addObject:[SPTexture textureWithContentsOfFile:@"greenballoon.png"]];
    [balloonTextures addObject:[SPTexture textureWithContentsOfFile:@"redballoon.png"]];
    [balloonTextures retain];
    playFieldSprite = [SPSprite sprite];
    [self addChild:playFieldSprite];
    [self addBalloon];

add ballon method

  -(void)addBalloon
  {
      for(int i = 0; i < 12; i++)
      {
         SPImage *image = [SPImage imageWithTexture:[balloonTextures objectAtIndex:0]];
         image.x = 40*i;
         image.y = 10 ;
         [playFieldSprite addChild:image];
      }
  }
Anupdas
  • 10,211
  • 2
  • 35
  • 60
Food Mood
  • 1
  • 2

3 Answers3

1

For getting random number:

#include <stdlib.h>

int r = arc4random() % 12;

Use a NSTimer to be called every second: How do I use NSTimer?

Community
  • 1
  • 1
EhTd
  • 3,260
  • 4
  • 19
  • 18
0

You could use CADisplayLink instead of NSTimer. The reason is that CADisplayLink is synchronized its drawing to the refresh rate of the display.

How to setup CADisplayLink:

id displayLink = [NSClassFromString(@"CADisplayLink") displayLinkWithTarget:self selector:@selector(changeBalloonTexture)];
[displayLink setFrameInterval:60];    // refresh rate is 60fps, 1=60fps, 60=1fps
[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSRunLoopCommonModes];

How to check whether CADisplayLink is supported or not on the particular device:

// code from cocos2d
displayLinkSupported = FALSE;
NSString *reqSysVer = @"3.1";
NSString *currSysVer = [[UIDevice currentDevice] systemVersion];
if ([currSysVer compare:reqSysVer options:NSNumericSearch] != NSOrderedAscending)
   displayLinkSupported = TRUE;
nzs
  • 3,252
  • 17
  • 22
0

For this you'll need to keep track of Green Balloons & change one of them to red randomly.

Put all your balloon sprites in one NSArray say balloonArray & schedule a timer to run a method every second. In that method, iterate the balloonArray, & collect all green balloons into another array say greenBalloonsArray.

Then use arch4random() method to generate a random number (index) from 0 to the length of greenBalloonsArray. Use random number as an index of newly created greenBalloonsArray to fetch the balloonSprite and change it's texture to red.

// Sudo Code
// Add all objects to one array
NSMutableArray *balloonsArray = [[NSMutableArray alloc] initWithObjects: ..... ];


- (void) timerMethod
{
        __block NSMutableArray *greenBalloonsArray = [[NSMutableArray alloc] init];
        [balloonsArray enumerateObjectsUsingBlock:^(BalloonSprite *object, NSUInteger idx, BOOL *stop) {
            if (object.isGreen)
                [greenBalloonsArray addObject:object];
        }];

        int index = arc4random() % greenBalloonsArray.count;
        GreenBallonArray * greenBalloon = [greenBalloonsArray objectAtIndex:index];
        [greenBallon turnToRed];
}
atastrophic
  • 3,153
  • 3
  • 31
  • 50