存档

2011年5月 的存档

Best way to generate random images for moving top to down

2011年5月27日 没有评论

I am creating a game which have similar concept like Tap Tap Ant. I am generating dynamic images using NSTimer using following code.

 NSTimer *tmrGenerateImages1 = [NSTimer scheduledTimerWithTimeInterval:0.4 target:self selector:@selector(generateImages) userInfo:nil repeats:YES];

 -(void)generateImages
 {
      UIImageView *tmpImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImg.png"]];
      [tmpImg setFrame:CGRectMake([self randomXValueBetween:30 andValue:280] , 50, 118, 97)];
      [self.view addSubview:tmpImg];
      [self.view bringSubviewToFront:tmpImg];
 }

 - (float)randomXValueBetween:(float)low andValue:(float)high {
     return (((float) arc4random() / 0xFFFFFFFFu) * (high - low)) + low;
 }

My question is in tap tap ant they are generating 2/3 images at a time and in my case I am able to generate only single image at a time and there is plenty of time user has to tap on it.

Please help me.

EDIT:
BOUNTY

I want to generate dynamic images using NSTimer or any other way as same as the Tap Tap Ant. Please help me in that regards. If solution is offered all the points are yours.

If you wan’t your game to be both fast responding and stable, I would really recommend looking into OpenGL or Cocos2d.

Allocating a UIImageView for every and and having them move bit by bit (you can not interrupt animations with taps) will really make the game… well… slow.

You Need To Take An Array Of ImageView up To MAX Amount of Ant you Want to Display on the scene At the random count

 UIImageView img[6];   

call this method with amount of array count from timer method

 -(void)InsideTimerMethod
   {
     for()
      {
      img[i] = [self createImageWithBackgroundInRect::CGRectMake(random*, y+i+constant*, 40, 80) withImageName:STRING FROM THE ARRAY RANDOMLY];
      }
   }


//custom method
- (uiimageview *)createImageWithBackgroundInRect:(CGRect)rect withImageName:(NSString   *)imageName{    
//set up all the necessary need for the image customize  
 }

Hope this Help you
good Luck:-)

This way you’ll get either two or three images per each trigger.

NSArray * countPool = [NSArray arrayWithObjects:[NSNumber numberWithInt:2], [NSNumber numberWithInt:3], nil];   

int count = [[countPool objectAtIndex:arc4random()%2] intValue];

for (int i = 0; i < count; i ++) {

    UIImageView *tmpImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myImg.png"]];
    [tmpImg setFrame:CGRectMake([self randomXValueBetween:30 andValue:280] , 50, 118, 97)];
    [self.view addSubview:tmpImg];
    [self.view bringSubviewToFront:tmpImg];

}

Even though I would assume that you will get the best performance when you will use the APIs closer to the native drawing interface than what is provided by UIKit, I still have an idea.

First you need to identify how many images you will present at maximum in each level. Assume you will have 100 images.

  1. In the setup method for the view,
    allocate all images
  2. Place all
    images in an area that is not
    visible (e.g. out of bounds)
  3. For
    each NSTimerInterval choose to move
    any number of images to a random
    place

With this approach you will defer the allocation to a different point in time and only perform translations which should be cheaper in terms of rendering costs.

However, I’m not sure how this will interfere with the general concept of rendering animations for the device. I would suggest having a look at the concepts of CoreAnimation which is the standard framework for performing animations on the iOS device family.

分类: stackoverflow精选, unity3d 标签: