Particle emitter works in one app, but not another

357 Views Asked by At

I've been experiencing some difficulties with particle effects. Originally I posted a question about how the particle effects in my app were different when on an ios6 device.

Particle system looks different on iOS 6 and ios7

I Originally designed the app on an ios7 device and got the particles looking like I wanted, but on the ios6 device the particles were smaller and displaced.

I was told it was problem between ios6 and ios7 and that changing the media timing could fix it.

Ive tried everything, but nothing has worked.

Eventually I tried just creating a separate app and pasted in the code from this tutorial - http://weblog.invasivecode.com/post/45058779586/caemitterlayer-and-the-ios-particle-system-lets in the view did load. The particles work as they should, but when I put the same code into my current apps viewDidLoad. The particles were all over the place and not doing what they should be.

I'm wandering if there is something in my current app that Im missing, that is upsetting the particles. Can anyone suggest things to look for?

Originally the particle effects were subclassed and being called after adding a subview to my main view.

Here is my view controller .h code (I've taken out some irrelevant control object properties and outlets)

@interface HHViewController : UIViewController < HHScoreViewdelegate>
{

IBOutlet UIButton* btnStart;
IBOutlet UIButton* btnStop;
IBOutlet UIButton* btnMore;
IBOutlet UIButton* btnHelp;
IBOutlet UIButton* btnTessilaCoil;
IBOutlet UIImageView* imgAnimatedTessila; 
IBOutlet UIImageView* imgBubble;
IBOutlet UIImageView* imgSteam;
IBOutlet UIImageView* imgMachineLightsTop;
IBOutlet UIImageView* imgBackground;

NSTimer* StopSignTimer;
NSTimer* timer;
NSTimer* lightMachineTimer;
NSTimer* ComputerLightsTimer;

CGFloat AnimationDelay;

AVAudioPlayer* audioPlayer;
AVAudioPlayer* audioPlayer2; //Used for game end audio, so that it doesnt interupt last letter/number audio

int pageValue;

IBOutlet UIButton* btnSetting;
IBOutlet UIButton* btnIAP;

IBOutlet UIButton* btnNext;
IBOutlet UIButton* btnForward;
IBOutlet UIImageView* imgLightMachine;
IBOutlet UIImageView* imgComputerLights;

NSString* strletterName;

HHScoreView * scoreSelection;
More * MorePanelView;
InAppPurchase * InAppPurchaseView;

NSMutableArray* ButtonBackgroundImages;
NSMutableArray* AlphabetBubbles;
NSMutableArray* SavedBubblePositions;

NSTimer* bubbleBlinkingTimer;
int wrongbubbleTapCount;

IBOutlet UIImageView* imgAlphabetDisplay;
IBOutlet UILabel* lblNextOneToPop;

// Code from Particle Tutorial 
CAEmitterLayer *_myEmitter;
CAEmitterCell *_myCell;
// End code form Particle Tutorial    

}

// Code from Particle Tutorial
@property(nonatomic, retain) CAEmitterLayer *_myEmitter;
@property(nonatomic, retain) CAEmitterCell *_myCell;
// End code form Particle Tutorial

@property(nonatomic,strong) UILabel* lblNextOneToPop;

@property(nonatomic,strong) NSTimer* SteamTimer;

@property(nonatomic,strong) NSTimer* StopSignTimer;
@property(nonatomic,strong) NSTimer* timer;
@property(nonatomic,strong) NSTimer* lightMachineTimer;
@property(nonatomic,strong) NSTimer* ComputerLightsTimer;

@property(nonatomic,retain)IBOutlet UIButton* btnStart;
@property(nonatomic,retain)IBOutlet UIButton* btnStop;
@property(nonatomic,retain)IBOutlet UIButton* btnMore;
@property(nonatomic,retain)IBOutlet UIButton* btnHelp;
@property(nonatomic,retain)IBOutlet UIButton* btnSetting;
@property(nonatomic,retain)IBOutlet UIButton* btnTessilaCoil;
@property(nonatomic,retain)IBOutlet UIImageView* imgAnimatedTessila;
@property(nonatomic,retain)IBOutlet UIImageView* imgSteam;
@property(nonatomic,retain)IBOutlet UIImageView* imgMachineLightsTop;
@property(nonatomic,retain)IBOutlet UIImageView* imgBackground;
@property(nonatomic,retain)IBOutlet UIScrollView* scScrollView;
@property(nonatomic,retain)IBOutlet  IBOutlet UIButton* btnNext;
@property(nonatomic,retain)IBOutlet  IBOutlet UIButton* btnForward;
@property(nonatomic,retain)IBOutlet UIImageView* imgLightMachine;
@property(nonatomic,retain)IBOutlet UIImageView* imgComputerLights;

@property(nonatomic,retain)NSString* strletterName;
@property(assign)int allLettersEventCount;

@property(nonatomic,retain) NSArray* ButtonBackgroundImages;

@property Boolean StopButtonClicked;
@property Boolean BubbleAOnscreen;

@property (nonatomic,assign) BOOL cancelAll;

@property int pageValue;
@property int positionRotation;

-(IBAction)tapPiece:(UITapGestureRecognizer *)recognizer;
-(IBAction)actionStartSign:(id)sender;
-(IBAction)actionStopSign:(id)sender;
-(IBAction)actionMoreSign:(id)sender;
-(IBAction)actionHelpSign:(id)sender;
-(IBAction)actionTessilaCoil:(id)sender;

-(IBAction)actionbtnHelp:(id)sender;
-(IBAction)actionbtnSetting:(id)sender;
-(IBAction)actionbtnIAP:(id)sender;

+(HHViewController*)sharedManager;

- (IBAction)purchaseItem:(id)sender;

@property (strong, nonatomic) InAppPurchase *purchaseController;

@end

Here is the code from my viewcontroller .m where I added the code from the tutorial.

@interface HHViewController ()
{

}
@end

@implementation HHViewController

static int  OrderingSequence = 0;

@synthesize _myEmitter, _myCell;
/*
TeslarGlowEffect* ShowGlow;
SteamEffect* ShowSteam;
BubbleBurst* PopBubble;
 */
//TeslarTimerSparks* TeslarTimer;

- (void)viewDidLoad
{
[super viewDidLoad];

CFTimeInterval currentTime = [self.view.layer convertTime:CACurrentMediaTime() fromLayer:nil];

NSLog(@"Current media Timing = %f", currentTime);

// Code from tutorial
CAEmitterLayer *emitterLayer = [CAEmitterLayer layer]; // 1
emitterLayer.emitterPosition = CGPointMake(self.view.bounds.size.width / 2, self.view.bounds.origin.y); // 2
emitterLayer.emitterZPosition = 10; // 3
emitterLayer.emitterSize = CGSizeMake(self.view.bounds.size.width, 0); // 4
emitterLayer.emitterShape = kCAEmitterLayerSphere; // 5

CAEmitterCell *emitterCell = [CAEmitterCell emitterCell]; // 6
emitterCell.scale = 0.1; // 7
emitterCell.scaleRange = 0.2; // 8
emitterCell.emissionRange = (CGFloat)M_PI_2; // 9
emitterCell.lifetime = 5.0; // 10
emitterCell.birthRate = 10; // 11
emitterCell.velocity = 200; // 12
emitterCell.velocityRange = 50; // 13
emitterCell.yAcceleration = 250; // 14
emitterCell.contents = (id)[[UIImage imageNamed:@"Steam1.png"] CGImage]; // 15

emitterLayer.emitterCells = [NSArray arrayWithObject:emitterCell]; // 16

[self.view.layer addSublayer:emitterLayer]; // 17
//end code from tutorial

[[NSNotificationCenter defaultCenter] addObserver:self
                                                  selector:@selector(appWillEnterForegroundNotification:)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(appWillEnterBackgroundNotification:)
                                             name:UIApplicationDidEnterBackgroundNotification
                                           object:nil];


[btnStart addTarget:self action:@selector(actionStartSign:) forControlEvents:UIControlEventTouchUpInside];

//NSLog(@"OrderingSequence = %d",OrderingSequence );
imgAnimatedTessila.image = [UIImage imageNamed:KTESSILAIMAGE];
self.alphebetindex = 0;
wrongbubbleTapCount = 0;

//// Load a bunch of arrays used in the game.

[btnHelp addTarget:self action:@selector(actionbtnHelp:) forControlEvents:UIControlEventTouchUpInside];

[btnSetting addTarget:self action:@selector(actionbtnSetting:) forControlEvents:UIControlEventTouchUpInside];

NSString* strFirstTimeloaded = [[NSUserDefaults standardUserDefaults]valueForKey:@"FirstTime"];

if(strFirstTimeloaded == nil)
{
    // Show preference page if first time used.
    [[NSUserDefaults standardUserDefaults]setValue:@"English" forKey:@"Language"];
    LanguagesSelectView* selection = [[LanguagesSelectView alloc]initWithFrame:CGRectMake(0, 0, 1024, 748)];
    [self.view addSubview:selection];
    [[NSUserDefaults standardUserDefaults]setValue:@"FirstTime" forKey:@"FirstTime"];
}
 positionRotation = 0;

self.strletterName = @"29";
self.allLettersEventCount = 0;

[[NSUserDefaults standardUserDefaults] setValue:@"One" forKey:@"Mode"];

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(enableSetting) name:@"EnableSetting" object:nil];
//[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(LetterTapped:) name:@"LetterTapped" object:nil];

btnStop.hidden = YES;
btnNext.hidden = YES;
btnForward.hidden = YES;

[self addGestureRecognizersToView];

//Create timer for release of steam from bubble machine
SteamTimer = [NSTimer scheduledTimerWithTimeInterval:5.2 target:self selector:@selector(ShowSteamEffect) userInfo:nil repeats:YES];

//Set Font for lblNextOneToPop
[lblNextOneToPop setFont:[UIFont fontWithName:@"SF Slapstick Comic" size:110]];
lblNextOneToPop.hidden = YES;

}

Here is one of my particle effects that was working on my ios7 iPad, but on the ios6 iPad, the effect looked totally different.

It was after long hrs trying to figure out why the ios7 and ios6 looked different that I tried the exercise of putting the tutorial code into a new app and then it into my own app. In my app the particles are totally different, but as they should be in the new app.

With respect to @matt's comments below. I know there must be something going on with my app, but I can't see it. I've looked all over the web and all through my code and I think I'm missing something.

All I want are three particles effects that appear at specific locations regardless of iOS version. One of them on user touch. The effect below is just one of them. I've tried the CAmediaTiming thing, but that didn't work.

SteamEffect.h

#import <UIKit/UIKit.h>

@interface SteamEffect : UIView

@end

The steamEffect.m looks like this.

#import "SteamEffect.h"

@implementation SteamEffect

{
CAEmitterLayer* SteamEmitter;
}

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {

    // Initialization code
    SteamEmitter = (CAEmitterLayer*) self.layer;
    //SteamEmitter.emitterPosition= CGPointMake(0, 0);
    //SteamEmitter.emitterSize = CGSizeMake(10,10);
    CAEmitterCell* Steam = [CAEmitterCell emitterCell];
    Steam.birthRate = 50.0f;
    Steam.spin = .6f;
    Steam.lifetime = 1.7f;
    Steam.alphaRange = 0.2f;
    Steam.alphaSpeed = 0.2f;
    Steam.contents = (id)[[UIImage imageNamed:@"Steam1.png"]CGImage];
    Steam.velocity = 30;
    Steam.velocityRange = 50.0f;
    Steam.emissionLongitude = -60.0f;
    Steam.emissionRange = M_1_PI;
    Steam.scale = .2f;
    Steam.scaleSpeed = .5f;
    Steam.yAcceleration = -200.0f;


    //SteamEmitter.renderMode =  kCAEmitterLayerBackToFront;//kCAEmitterLayerAdditive;
    //SteamEmitter.emitterShape = kCAEmitterLayerCircle;
    SteamEmitter.emitterCells = @[Steam];
    SteamEmitter.emitterPosition = CGPointMake(815, 445);

    //Check System Version
    NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];

    if ([[vComp objectAtIndex:0] intValue] == 7) {
        // iOS-6 code
        SteamEmitter.beginTime = CACurrentMediaTime();
    }

}
    return self;
}

-(void)didMoveToSuperview
{
//1
[super didMoveToSuperview];
if (self.superview==nil) return;

[self performSelector:@selector(disableEmitterCell) withObject:nil afterDelay:0.5];
}

-(void)disableEmitterCell
{
[SteamEmitter setValue:@0 forKeyPath:@"birthRate"]; 

}

+ (Class) layerClass
{
//tell UIView to use the CAEmitterLayer root class
return [CAEmitterLayer class];
}

- (void) setEmitterPosition:(CGPoint)pos
{
SteamEmitter.emitterPosition = pos;
}
- (void) toggleOn:(bool)on
{
//SteamEmitter.birthRate = on? 300 : 0;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end

I called the above from my viewcontroller .m using the following. There is also a timer in the viewDidLoad above that calls this every 5 sec. Although that is probably not the way to do it. Probably better to add the view and start it emitting every 5 sec rather than adding a new view each time, but thats a different issue.

- (void)ShowSteamEffect
{
//Check System Version because NSAttributedString only works in ios6 and above.
NSArray *vComp = [[UIDevice currentDevice].systemVersion componentsSeparatedByString:@"."];


//Create view for Steam particle effect.
//CGRect SteamFrame = CGRectMake(790, 380, 100, 100); //ORIGINAL
CGRect SteamFrame = CGRectMake(0, 0, 0, 0); //(815, 455, 100, 100)
//Show Steam effect
ShowSteam = [[SteamEffect alloc]initWithFrame:SteamFrame];
ShowSteam.hidden = NO;

[self.view insertSubview:ShowSteam aboveSubview:imgComputerLights];

}
0

There are 0 best solutions below