生成从上到下移动的随机图像的最佳方法
我正在创建一个具有类似概念的游戏,例如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您不希望您的游戏既快速响应又稳定,我强烈建议您考虑 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.
您需要获取一个 ImageView 数组,最多达到您想要在场景上显示的蚂蚁的最大数量。在随机计数时,
使用计时器方法中的数组计数数量调用此方法
希望这对您有帮助
祝你好运:-)
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
call this method with amount of array count from timer method
Hope this Help you
good Luck:-)
这样,每个触发器都会获得两个或三个图像。
This way you'll get either two or three images per each trigger.
尽管我假设当您使用比 UIKit 提供的更接近本机绘图界面的 API 时,您将获得最佳性能,但我仍然有一个想法。
首先,您需要确定在每个级别中最多展示多少张图像。假设您有 100 张图像。
分配所有图像
图像不在某个区域
可见(例如出界)
每个 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.
allocate all images
images in an area that is not
visible (e.g. out of bounds)
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.