无需 opengl 和 cocos2D 创建粒子。降雪应用

发布于 2024-12-18 12:03:56 字数 2045 浏览 2 评论 0原文

好吧,我想在没有 openGL 或 cocs2D 的情况下创建像 (snow ) 这样的粒子,我发现了这个名为 snowfall 的示例代码,在这段代码中有这样的内容:

flakeImage = [UIImage imageNamed:@"flake.png"];

// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}



// Timer event is called whenever the timer fires

- (void)onTimer
{


// build a view from our flake image
UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

// use the random() function to randomize up our flake attributes
int startX = round(random() % 320);
int endX = round(random() % 320);
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;

// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 5.0 * scale, 5.0 * scale);
flakeView.alpha = 0.25;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:5 * speed];

// set the postion where flake will move to
flakeView.frame = CGRectMake(endX, 500.0, 5.0 * scale, 5.0 * scale);

// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];  }
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

UIImageView *flakeView = context;
[flakeView removeFromSuperview];
// open the debug log and you will see that all flakes have a retain count 
// of 1 at this point so we know the release below will keep our memory 
// usage in check
NSLog([NSString stringWithFormat:@"[flakeView retainCount] = %d", [flakeView retainCount]]);
[flakeView release];


}

我想知道这段代码是否会损害性能或内存(也许是因为它使用计时器)?如果这不是一个好主意,我听说 CAReplicator 可以用于 iphone,但 CAReplicatorDemo 只适用于苹果文档中的 mac :/ 抱歉我的英语我是法国人:/

well I wanted to create particles like (snow ) without openGL or cocs2D, and I found this sample code called snowfall and in this code there is this :

flakeImage = [UIImage imageNamed:@"flake.png"];

// start a timet that will fire 20 times per second
[NSTimer scheduledTimerWithTimeInterval:(0.5) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}



// Timer event is called whenever the timer fires

- (void)onTimer
{


// build a view from our flake image
UIImageView* flakeView = [[UIImageView alloc] initWithImage:flakeImage];

// use the random() function to randomize up our flake attributes
int startX = round(random() % 320);
int endX = round(random() % 320);
double scale = 1 / round(random() % 100) + 1.0;
double speed = 1 / round(random() % 100) + 1.0;

// set the flake start position
flakeView.frame = CGRectMake(startX, -100.0, 5.0 * scale, 5.0 * scale);
flakeView.alpha = 0.25;

// put the flake in our main view
[self.view addSubview:flakeView];

[UIView beginAnimations:nil context:flakeView];
// set up how fast the flake will fall
[UIView setAnimationDuration:5 * speed];

// set the postion where flake will move to
flakeView.frame = CGRectMake(endX, 500.0, 5.0 * scale, 5.0 * scale);

// set a stop callback so we can cleanup the flake when it reaches the
// end of its animation
[UIView setAnimationDidStopSelector:@selector(onAnimationComplete:finished:context:)];
[UIView setAnimationDelegate:self];
[UIView commitAnimations];  }
- (void)onAnimationComplete:(NSString *)animationID finished:(NSNumber *)finished context:(void *)context {

UIImageView *flakeView = context;
[flakeView removeFromSuperview];
// open the debug log and you will see that all flakes have a retain count 
// of 1 at this point so we know the release below will keep our memory 
// usage in check
NSLog([NSString stringWithFormat:@"[flakeView retainCount] = %d", [flakeView retainCount]]);
[flakeView release];


}

I wanted to know if this code can hurt performance or memory ( maybe because it use a timer)?And if it is not a good idea I heard about CAReplicator that we can use for iphone but the CAReplicatorDemo only work for mac on the apple documentation :/ sorry for my english I'm french :/

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

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

发布评论

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

评论(3

后来的我们 2024-12-25 12:03:56

当然,这会影响性能。但您不会在模拟器中看到它。 UIImageView 太重,无法每秒创建 20 次。您可以尝试使用 CALayer-s 池(和 CAAnimation)。

Sure, it will affect performance. You will not see it in Simulator though. UIImageView is too heavy to be created 20 times a second. You may try to use a pool of CALayer-s instead (and CAAnimation).

栀梦 2024-12-25 12:03:56

您可以使用 CAEmitterLayer 来在 iOS 5 上做粒子效果。我以前用过它来处理火焰和烟雾,但它应该适用于雪。

You can use a CAEmitterLayer to do particle effects on iOS 5. I've used it for flames and smoke before but it should work just fine for snow.

若沐 2024-12-25 12:03:56

你可以试试我的KHParticleView,它使用cocos2d粒子并在顶部添加cocos2d视图。检查此处的代码:https://github.com/lephukhanhhuy/KHParticleView

You can try my KHParticleView, it use cocos2d particle and add cocos2d view on the top. Check the code here: https://github.com/lephukhanhhuy/KHParticleView

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