我想在用戶在屏幕上拖動手指時產生一些粒子。我有所有的拖動代碼,但我的問題是,我根本無法看到任何粒子繪圖!如何添加一個UIView與CAEmitterLayer到我的遊戲?
有了一點谷歌搜索,我拿出我所做的UIView的子類下面的類:
#import "PrettyTouch.h"
#import <QuartzCore/QuartzCore.h>
@implementation PrettyTouch
{
CAEmitterLayer* touchEmitter;
}
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
touchEmitter = (CAEmitterLayer*) self.layer;
touchEmitter.emitterPosition= CGPointMake(0, 0);
touchEmitter.emitterSize = CGSizeMake(5,5);
CAEmitterCell* touch = [CAEmitterCell emitterCell];
touch.birthRate = 0;
touch.lifetime = 0.6;
touch.color = [[UIColor colorWithRed:0.2 green:0.3 blue:1.0 alpha:0.06] CGColor];
touch.contents = (id)[[UIImage imageNamed:@"part.png"]CGImage];
touch.velocity = 0;
touch.velocityRange = 50;
touch.emissionRange = 2*M_PI;
touch.scale = 1.0;
touch.scaleSpeed = -0.1;
touchEmitter.renderMode = kCAEmitterLayerAdditive;
touchEmitter.emitterCells = [NSArray arrayWithObject:touch];
}
return self;
}
+ (Class) layerClass
{
//tell UIView to use the CAEmitterLayer root class
return [CAEmitterLayer class];
}
- (void) setEmitterPosition:(CGPoint)pos
{
touchEmitter.emitterPosition = pos;
}
- (void) toggleOn:(bool)on
{
touchEmitter.birthRate = on? 300 : 0;
}
@end
然後在我的遊戲視圖控制器類,在viewDidLoad中,我做到了這一點:
@implementation PlayViewController
{
//...
PrettyTouch* touch;
}
- (void)viewDidLoad
{
[super viewDidLoad];
//...
touch = [[PrettyTouch alloc]initWithFrame:self.view.frame];
touch.hidden = NO;
[self.view addSubview:touch];
//...
然後在我的UIPanGestureRecogniser功能我叫[touch toggleOn:YES];
當手勢開始和[touch setEmitterPosition:[gest locationInView:self.view]];
當函數被調用。 (手勢是UIPanGestureRecogniser *)。
我可能會丟失什麼,或者我需要做什麼來獲取粒子繪圖?
謝謝
只有使用[tag:xcode]標籤才能瞭解有關IDE的問題。謝謝! – Undo 2013-03-14 13:05:23