Example #1
0
        public void AwakeFromNib()
        {
            //Create the root layer
            this.rootLayer = CALayer.Layer;

            //Set the root layer's attributes
            this.rootLayer.Bounds = CGRect.CGRectMake (0, 0, 640, 480);
            IntPtr color = CGColor.CreateGenericRGB (0.0, 0.0, 0.0, 1);
            rootLayer.BackgroundColor = color;
            CGColor.Release (color);

            //Load the spark image for the particle
            String fileName = NSBundle.MainBundle.PathForResourceOfType ("tspark", "png");
            IntPtr dataProvider = CGDataProvider.CreateWithFilename (fileName);
            IntPtr img = CGImage.CreateWithPNGDataProvider (dataProvider, IntPtr.Zero, false, CGColorRenderingIntent.kCGRenderingIntentDefault);

            this.mortor = CAEmitterLayer.Layer.Retain<CAEmitterLayer>();
            this.mortor.EmitterPosition = CGPoint.CGPointMake (320, 0);
            this.mortor.RenderMode = CAEmitterLayer.kCAEmitterLayerAdditive;

            //Invisible particle representing the rocket before the explosion
            CAEmitterCell rocket = CAEmitterCell.EmitterCell.Retain<CAEmitterCell>();
            rocket.EmissionLongitude = Math.PI / 2;
            rocket.EmissionLatitude = 0;
            rocket.Lifetime = 1.6f;
            rocket.BirthRate = 1;
            rocket.Velocity = 400;
            rocket.VelocityRange = 100;
            rocket.YAcceleration = -250;
            rocket.EmissionRange = Math.PI / 4;
            color = CGColor.CreateGenericRGB (0.5, 0.5, 0.5, 0.5);
            rocket.Color = color;
            CGColor.Release (color);
            rocket.RedRange = 0.5f;
            rocket.GreenRange = 0.5f;
            rocket.BlueRange = 0.5f;

            //Name the cell so that it can be animated later using keypath
            rocket.Name = "rocket";

            //Flare particles emitted from the rocket as it flys
            CAEmitterCell flare = CAEmitterCell.EmitterCell;
            flare.ContentsPointer = img;
            flare.EmissionLongitude = (4 * Math.PI) / 2;
            flare.Scale = 0.4;
            flare.Velocity = 100;
            flare.BirthRate = 45;
            flare.Lifetime = 1.5f;
            flare.YAcceleration = -350;
            flare.EmissionRange = Math.PI / 7;
            flare.AlphaSpeed = -0.7f;
            flare.ScaleSpeed = -0.1;
            flare.ScaleRange = 0.1;
            flare.BeginTime = 0.01;
            flare.Duration = 0.7;

            //The particles that make up the explosion
            CAEmitterCell firework = CAEmitterCell.EmitterCell;
            firework.ContentsPointer = img;
            firework.BirthRate = 9999;
            firework.Scale = 0.6;
            firework.Velocity = 130;
            firework.Lifetime = 2;
            firework.AlphaSpeed = -0.2f;
            firework.YAcceleration = -80;
            firework.BeginTime = 1.5;
            firework.Duration = 0.1;
            firework.EmissionRange = 2 * Math.PI;
            firework.ScaleSpeed = -0.1;
            firework.Spin = 2;

            //Name the cell so that it can be animated later using keypath
            firework.Name = "firework";

            //preSpark is an invisible particle used to later emit the spark
            CAEmitterCell preSpark = CAEmitterCell.EmitterCell;
            preSpark.BirthRate = 80;
            preSpark.Velocity = firework.Velocity * 0.70;
            preSpark.Lifetime = 1.7f;
            preSpark.YAcceleration = firework.YAcceleration * 0.85;
            preSpark.BeginTime = firework.BeginTime - 0.2;
            preSpark.EmissionRange = firework.EmissionRange;
            preSpark.GreenSpeed = 100;
            preSpark.BlueSpeed = 100;
            preSpark.RedSpeed = 100;

            //Name the cell so that it can be animated later using keypath
            preSpark.Name = "preSpark";

            //The 'sparkle' at the end of a firework
            CAEmitterCell spark = CAEmitterCell.EmitterCell;
            spark.ContentsPointer = img;
            spark.Lifetime = 0.05f;
            spark.YAcceleration = -250;
            spark.BeginTime = 0.8;
            spark.Scale = 0.4;
            spark.BirthRate = 10;

            preSpark.EmitterCells = NSArray.ArrayWithObject (spark);
            rocket.EmitterCells = NSArray.ArrayWithObjects (flare, firework, preSpark, null);
            mortor.EmitterCells = NSArray.ArrayWithObject (rocket);
            this.rootLayer.AddSublayer (this.mortor);

            //Set the view's layer to the base layer
            theView.Layer = rootLayer;
            theView.WantsLayer = true;

            //Force the view to update
            theView.NeedsDisplay = true;
        }
Example #2
0
        public void AwakeFromNib()
        {
            //Create the root layer
            rootLayer = CALayer.Layer;

            //Set the root layer's background color to black
            rootLayer.BackgroundColor = CGColor.GetConstantColor(CGColor.kCGColorBlack);

            //Create the fire emitter layer
            fireEmitter = CAEmitterLayer.Layer;
            fireEmitter.EmitterPosition = new CGPoint(225, 50);
            fireEmitter.EmitterMode = CAEmitterLayer.kCAEmitterLayerOutline;
            fireEmitter.EmitterShape = CAEmitterLayer.kCAEmitterLayerLine;
            fireEmitter.RenderMode = CAEmitterLayer.kCAEmitterLayerAdditive;
            fireEmitter.EmitterSize = new CGSize(0, 0);

            //Create the smoke emitter layer
            smokeEmitter = CAEmitterLayer.Layer;
            smokeEmitter.EmitterPosition = new CGPoint(225, 50);
            smokeEmitter.EmitterMode = CAEmitterLayer.kCAEmitterLayerPoints;

            //Create the fire emitter cell
            CAEmitterCell fire = CAEmitterCell.EmitterCell;
            fire.EmissionLongitude = (float) Math.PI;
            fire.BirthRate = 0;
            fire.Velocity = 80;
            fire.VelocityRange = 30;
            fire.EmissionRange = 1.1f;
            fire.YAcceleration = 200;
            fire.ScaleSpeed = 0.3f;
            IntPtr color = CGColor.CreateGenericRGB(0.8f, 0.4f, 0.2f, 0.10f);
            fire.Color = color;
            CGColor.Release(color);
            fire.ContentsPointer = CGImageNamed("fire.png");

            //Name the cell so that it can be animated later using keypaths
            fire.Name = "fire";

            //Add the fire emitter cell to the fire emitter layer
            fireEmitter.EmitterCells = NSArray.ArrayWithObject(fire);

            //Create the smoke emitter cell
            CAEmitterCell smoke = CAEmitterCell.EmitterCell;
            smoke.BirthRate = 11;
            smoke.EmissionLongitude = (float)(Math.PI / 2);
            smoke.Lifetime = 0;
            smoke.Velocity = 40;
            smoke.VelocityRange = 20;
            smoke.EmissionRange = (float)(Math.PI / 4);
            smoke.Spin = 1;
            smoke.SpinRange = 6;
            smoke.YAcceleration = 160;
            smoke.ContentsPointer = CGImageNamed("smoke.png");
            smoke.Scale = 0.1f;
            smoke.AlphaSpeed = -0.12f;
            smoke.ScaleSpeed = 0.7f;

            //Name the cell so that it can be animated later using keypaths
            smoke.Name = "smoke";

            //Add the smoke emitter cell to the smoke emitter layer
            smokeEmitter.EmitterCells = NSArray.ArrayWithObject(smoke);

            //Add the two emitter layers to the root layer
            rootLayer.AddSublayer(smokeEmitter);
            rootLayer.AddSublayer(fireEmitter);

            //Set the view's layer to the base layer
            view.Layer = rootLayer;
            view.WantsLayer = true;

            //Set the fire simulation to reflect the initial slider position
            this.SlidersChanged(this);

            //Force the view to update
            view.NeedsDisplay = true;
        }