private void ParseVersion1(PlistDictionary animations)
        {
            CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache;

            foreach (var pElement in animations)
            {
                PlistDictionary animationDict = pElement.Value.AsDictionary;

                PlistArray frameNames = animationDict["frames"].AsArray;
                float      delay      = animationDict["delay"].AsFloat;
                //CCAnimation* animation = NULL;

                if (frameNames == null)
                {
                    CCLog.Log(
                        "cocos2d: CCAnimationCache: Animation '{0}' found in dictionary without any frames - cannot add to animation cache.",
                        pElement.Key);
                    continue;
                }

                var frames = new List <CCAnimationFrame>(frameNames.Count);

                foreach (PlistObjectBase pObj in frameNames)
                {
                    string        frameName   = pObj.AsString;
                    CCSpriteFrame spriteFrame = frameCache.SpriteFrameByName(frameName);

                    if (spriteFrame == null)
                    {
                        CCLog.Log(
                            "cocos2d: CCAnimationCache: Animation '{0}' refers to frame '%s' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.",
                            pElement.Key, frameName);
                        continue;
                    }

                    var animFrame = new CCAnimationFrame();
                    animFrame.InitWithSpriteFrame(spriteFrame, 1, null);
                    frames.Add(animFrame);
                }

                if (frames.Count == 0)
                {
                    CCLog.Log(
                        "cocos2d: CCAnimationCache: None of the frames for animation '{0}' were found in the CCSpriteFrameCache. Animation is not being added to the Animation Cache.",
                        pElement.Key);
                    continue;
                }
                else if (frames.Count != frameNames.Count)
                {
                    CCLog.Log(
                        "cocos2d: CCAnimationCache: An animation in your dictionary refers to a frame which is not in the CCSpriteFrameCache. Some or all of the frames for the animation '{0}' may be missing.",
                        pElement.Key);
                }

                CCAnimation animation = new CCAnimation(frames, delay, 1);

                SharedAnimationCache.AddAnimation(animation, pElement.Key);
            }
        }
        private void ParseVersion2(PlistDictionary animations)
        {
            CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache;

            foreach (var pElement in animations)
            {
                string          name          = pElement.Key;
                PlistDictionary animationDict = pElement.Value.AsDictionary;

                int  loops = animationDict["loops"].AsInt;
                bool restoreOriginalFrame = animationDict["restoreOriginalFrame"].AsBool;

                PlistArray frameArray = animationDict["frames"].AsArray;

                if (frameArray == null)
                {
                    CCLog.Log(
                        "cocos2d: CCAnimationCache: Animation '{0}' found in dictionary without any frames - cannot add to animation cache.",
                        name);
                    continue;
                }

                // Array of AnimationFrames
                var array = new List <CCAnimationFrame>(frameArray.Count);

                foreach (PlistObjectBase pObj in frameArray)
                {
                    PlistDictionary entry = pObj.AsDictionary;

                    string        spriteFrameName = entry["spriteframe"].AsString;
                    CCSpriteFrame spriteFrame     = frameCache.SpriteFrameByName(spriteFrameName);

                    if (spriteFrame == null)
                    {
                        CCLog.Log(
                            "cocos2d: CCAnimationCache: Animation '{0}' refers to frame '{1}' which is not currently in the CCSpriteFrameCache. This frame will not be added to the animation.",
                            name, spriteFrameName);

                        continue;
                    }

                    float           delayUnits = entry["delayUnits"].AsFloat;
                    PlistDictionary userInfo   = entry["notification"].AsDictionary;

                    var animFrame = new CCAnimationFrame();
                    animFrame.InitWithSpriteFrame(spriteFrame, delayUnits, userInfo);

                    array.Add(animFrame);
                }

                float delayPerUnit = animationDict["delayPerUnit"].AsFloat;
                var   animation    = new CCAnimation(array, delayPerUnit, (uint)loops);

                animation.RestoreOriginalFrame = restoreOriginalFrame;

                SharedAnimationCache.AddAnimation(animation, name);
            }
        }
예제 #3
0
        protected virtual CCSpriteFrame ParsePropTypeSpriteFrame(CCNode node, CCNode parent, CCBReader reader, string propertyName)
        {
            string spriteSheet = reader.ReadCachedString();
            string spriteFile  = reader.ReadCachedString();

            CCSpriteFrame spriteFrame = null;

            if (spriteFile.Length != 0)
            {
                if (spriteSheet.Length == 0)
                {
                    CCTexture2D texture = CCTextureCache.SharedTextureCache.AddImage(CCFileUtils.RemoveExtension(spriteFile));
                    var         bounds  = new CCRect(0, 0, texture.ContentSize.Width, texture.ContentSize.Height);
                    spriteFrame = new CCSpriteFrame(texture, bounds);
                }
                else
                {
                    CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache;

                    // Load the sprite sheet only if it is not loaded
                    if (!reader.LoadedSpriteSheet.Contains(spriteSheet))
                    {
                        frameCache.AddSpriteFramesWithFile(spriteSheet);
                        reader.LoadedSpriteSheet.Add(spriteSheet);
                    }

                    spriteFrame = frameCache.SpriteFrameByName(spriteFile);
                }

                if (reader.AnimatedProperties.Contains(propertyName))
                {
                    reader.AnimationManager.SetBaseValue(spriteFrame, node, propertyName);
                }
            }

            return(spriteFrame);
        }
예제 #4
0
        private CCBKeyframe ReadKeyframe(CCBPropType type)
        {
            var keyframe = new CCBKeyframe();

            keyframe.Time = ReadFloat();

            var    easingType = (CCBKeyframeEasing)ReadInt(false);
            float  easingOpt  = 0;
            object value      = null;

            if (easingType == CCBKeyframeEasing.CubicIn ||
                easingType == CCBKeyframeEasing.CubicOut ||
                easingType == CCBKeyframeEasing.CubicInOut ||
                easingType == CCBKeyframeEasing.ElasticIn ||
                easingType == CCBKeyframeEasing.ElasticOut ||
                easingType == CCBKeyframeEasing.ElasticInOut)
            {
                easingOpt = ReadFloat();
            }
            keyframe.EasingType = easingType;
            keyframe.EasingOpt  = easingOpt;

            if (type == CCBPropType.Check)
            {
                value = new CCBValue(ReadBool());
            }
            else if (type == CCBPropType.Byte)
            {
                value = new CCBValue(ReadByte());
            }
            else if (type == CCBPropType.Color3)
            {
                byte r = ReadByte();
                byte g = ReadByte();
                byte b = ReadByte();

                var c = new CCColor3B(r, g, b);
                value = new CCColor3BWapper(c);
            }
            else if (type == CCBPropType.Degrees)
            {
                value = new CCBValue(ReadFloat());
            }
            else if (type == CCBPropType.ScaleLock || type == CCBPropType.Position)
            {
                float a = ReadFloat();
                float b = ReadFloat();

                value = new List <CCBValue>
                {
                    new CCBValue(a),
                    new CCBValue(b)
                };
            }
            else if (type == CCBPropType.SpriteFrame)
            {
                string spriteSheet = ReadCachedString();
                string spriteFile  = ReadCachedString();

                CCSpriteFrame spriteFrame;

                if (String.IsNullOrEmpty(spriteSheet))
                {
                    CCTexture2D texture = CCTextureCache.SharedTextureCache.AddImage(CCFileUtils.RemoveExtension(spriteFile));
                    var         bounds  = new CCRect(0, 0, texture.ContentSize.Width, texture.ContentSize.Height);
                    spriteFrame = new CCSpriteFrame(texture, bounds);
                }
                else
                {
                    CCSpriteFrameCache frameCache = CCSpriteFrameCache.SharedSpriteFrameCache;

                    // Load the sprite sheet only if it is not loaded
                    if (!mLoadedSpriteSheets.Contains(spriteSheet))
                    {
                        frameCache.AddSpriteFramesWithFile(spriteSheet);
                        mLoadedSpriteSheets.Add(spriteSheet);
                    }

                    spriteFrame = frameCache.SpriteFrameByName(spriteFile);
                }
                value = spriteFrame;
            }

            keyframe.Value = value;

            return(keyframe);
        }