生成从上到下移动的随机图像的最佳方法

发布于 2024-11-10 00:59:23 字数 1057 浏览 3 评论 0原文

我正在创建一个具有类似概念的游戏,例如 Tap Tap Ant。我使用 NSTimer 使用以下代码生成动态图像。

 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;
 }

我的问题是在 Tap Tap ant 中,它们一次生成 2/3 图像,在我的情况下,我一次只能生成单个图像,并且用户必须有足够的时间点击它。

请帮我。

编辑: BOUNTY

我想使用 NSTimer 或任何其他与 点击点击 Ant。请在这方面帮助我。如果提供解决方案,所有积分都是您的。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

故乡的云 2024-11-17 00:59:23

如果您不希望您的游戏既快速响应又稳定,我强烈建议您考虑 OpenGL 或 Cocos2d。

为每个对象分配一个 UIImageView 并让它们一点一点地移动(你不能用点击来中断动画)真的会让游戏......嗯......慢。

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.

A君 2024-11-17 00:59:23

您需要获取一个 ImageView 数组,最多达到您想要在场景上显示的蚂蚁的最大数量。在随机计数时,

 UIImageView img[6];   

使用计时器方法中的数组计数数量调用此方法

 -(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  
 }

希望这对您有帮助
祝你好运:-)

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:-)

勿忘初心 2024-11-17 00:59:23

这样,每个触发器都会获得两个或三个图像。

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];

}

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];

}
哆兒滾 2024-11-17 00:59:23

尽管我假设当您使用比 UIKit 提供的更接近本机绘图界面的 API 时,您将获得最佳性能,但我仍然有一个想法。

首先,您需要确定在每个级别中最多展示多少张图像。假设您有 100 张图像。

  1. 在视图的设置方法中,
    分配所有图像
  2. 全部放置
    图像不在某个区域
    可见(例如出界)
  3. 对于
    每个 NSTimerInterval 选择移动
    任意数量的图像随机
    通过

这种方法,您可以将分配推迟到不同的时间点,并且仅执行翻译,这在渲染成本方面应该更便宜。

但是,我不确定这将如何干扰设备渲染动画的一般概念。我建议看看 的概念CoreAnimation 是在 iOS 设备系列上执行动画的标准框架。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文