`

cocos2d 总结:三 CCSprite

 
阅读更多

CCSprite

 

1: CCSpriteBatchNode   ???????(可能是多个CCSprite)

 

CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithFile:@"bullet.png"];

 

CCArray* bullets = [batch  children];

 

2:用多个单独的文件创建动画 CCAnimation, CCSpriteFrame

 

 

+(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay

{

    // load the animation frames as textures and create the sprite frames

    NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];

    for (int i = 0; i < frameCount; i++)

    {

       // Assuming all animation files are named "nameX.png" with X being a consecutive number starting with 0.

       NSString* file = [NSString stringWithFormat:@"%@%i.png", name, i];

       CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];

 

       // Assuming that image file animations always use the whole image for each animation frame.

       CGSize texSize = texture.contentSize;

       CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);

       CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];

      

       [frames addObject:frame];

    }

   

    // create an animation object from all the sprite animation frames

    return [CCAnimation animationWithName:name delay:delay frames:frames];

}

 

//调用方式

CCAnimate* animate = [CCAnimate

actionWithAnimation:[self animationWithFile:@"ship-anim" //图片文件

frameCount:5 //帧数

delay:0.08f]//持续时间

];

 

3:*.plist 文件创建动画

 

1// Creates an animation from single files.

 

 

-(id) initWithShipImage

{

    // Load the Texture Atlas sprite frames, this also loads the Texture with the same name.

    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];

    [frameCache addSpriteFramesWithFile:@"ship-and-bullet.plist"];

 

    // Loading the Ship's sprite using a sprite frame name (eg the filename)

    if ((self = [super initWithSpriteFrameName:@"ship.png"]))

    {

       // load the ship's animation frames as textures and create a sprite frame

       NSMutableArray* frames = [NSMutableArray arrayWithCapacity:5];

       for (int i = 0; i < 5; i++)

       {

           NSString* file = [NSString stringWithFormat:@"ship-anim%i.png", i];

           CCSpriteFrame* frame = [frameCache spriteFrameByName:file];

           [frames addObject:frame];

       }

      

       // create an animation object from all the sprite animation frames

       CCAnimation* anim = [CCAnimation animationWithName:@"move" delay:0.08f frames:frames];

      

       // add the animation to the sprite (optional)

       //[self addAnimation:anim];

      

       // run the animation by using the CCAnimate action

       CCAnimate* animate = [CCAnimate actionWithAnimation:anim];

       CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];

       [self runAction:repeat];

      

      

       [self scheduleUpdate];

    }

    return self;

}

 

// Creates an animation from single files.

+(CCAnimation*) animationWithFile:(NSString*)name frameCount:(int)frameCount delay:(float)delay

{

    // load the animation frames as textures and create the sprite frames

    NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];

    for (int i = 0; i < frameCount; i++)

    {

       // Assuming all animation files are named "nameX.png" with X being a consecutive number starting with 0.

       NSString* file = [NSString stringWithFormat:@"%@%i.png", name, i];

       CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];

 

       // Assuming that image file animations always use the whole image for each animation frame.

       CGSize texSize = texture.contentSize;

       CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);

       CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect offset:CGPointZero];

      

       [frames addObject:frame];

    }

   

    // create an animation object from all the sprite animation frames

    return [CCAnimation animationWithName:name delay:delay frames:frames];

}

 

 

2) Creates an animation from sprite frames.

 

-(id) initWithShipImage

{

    // Load the Texture Atlas sprite frames, this also loads the Texture with the same name.

    CCSpriteFrameCache* frameCache = [CCSpriteFrameCache sharedSpriteFrameCache];

    [frameCache addSpriteFramesWithFile:@"ship-and-bullet.plist"];

 

    // Loading the Ship's sprite using a sprite frame name (eg the filename)

    if ((self = [super initWithSpriteFrameName:@"ship.png"]))

    {

       // create an animation object from all the sprite animation frames

       CCAnimation* anim = [CCAnimation animationWithFrame:@"ship-anim" frameCount:5 delay:0.08f];

      

       // add the animation to the sprite (optional)

       //[self addAnimation:anim];

      

       // run the animation by using the CCAnimate action

       CCAnimate* animate = [CCAnimate actionWithAnimation:anim];

       CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];

       [self runAction:repeat];

      

      

       [self scheduleUpdate];

    }

    return self;

}

 

 

// Creates an animation from sprite frames.

+(CCAnimation*) animationWithFrame:(NSString*)frame frameCount:(int)frameCount delay:(float)delay

{

    // load the ship's animation frames as textures and create a sprite frame

    NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];

    for (int i = 0;

分享到:
评论

相关推荐

    cocos2d 总结:四 背景的循环移动

    总结,Cocos2d中的背景循环移动涉及到精灵的创建、位置调整以及定时器的使用。通过巧妙地结合这些基本元素,可以创造出各种动态的背景效果,提升游戏的视觉体验。对于开发者来说,熟练掌握这些技巧是构建高品质2D...

    Cocos2d-x 自定义可接受处理触摸信息精灵类

    总结来说,这个例子展示了如何在Cocos2d-x中自定义精灵类来处理触摸事件。通过继承`CCSprite`并实现触摸事件回调函数,我们可以让精灵根据触摸状态执行不同的动作。同时,通过创建场景并设置触摸监听,我们确保了...

    Cocos2D游戏之旅(二):主角血条的实现

    cocos2d::CCSprite* backgroundBar; cocos2d::CCSprite* healthFill; public: void updateHealth(int health, int maxHealth); }; ``` 在`updateHealth`方法中,我们可以根据健康值的比例调整血量条精灵的宽度...

    USING_NS_CC的理解1

    总结来说,`USING_NS_CC` 是Cocos2d-x中用来简化访问 `cocos2d` 命名空间的一个宏,它等同于 `using namespace cocos2d;`。使用这个宏可以避免在代码中频繁地写 `cocos2d::`,但应谨慎使用,以防止潜在的命名冲突。...

    cocos2d-android jar包全套.zip

    在开发过程中,你可以利用Cocos2d提供的各种组件,如`CCScene`(场景)、`CCSprite`(精灵)、`CCLabel`(文本标签)等,创建游戏对象和交互逻辑。同时,别忘了Cocos2d支持事件处理,如触摸事件和键盘事件,这使得...

    cocos2dx 精灵触摸事件的实现

    总结一下,Cocos2d-x中为精灵实现触摸事件涉及以下步骤: 1. 创建自定义精灵类,继承自`cc::Sprite`。 2. 重写`onTouchBegan`、`onTouchMoved`和`onTouchEnded`方法。 3. 在场景中设置事件监听器,并将精灵作为目标...

    使用大纲2

    CCSprite *sprite = CCSprite::create(String::createWithFormat("%03d.png", nPage + 1)-&gt;getCString()); pPage-&gt;addChild(sprite, 0, kPageBack); return true; } ``` 4. **处理页面点击事件** 当用户点击...

    cocos2d-x泡泡龙例——VS版

    总结来说,使用cocos2d-x和Visual Studio 2010开发泡泡龙游戏,需要掌握cocos2d-x的基本组件和编程模型,理解游戏逻辑的实现,熟悉Visual Studio的项目配置,并具备一定的图形和算法知识。通过实践,开发者不仅可以...

    cocos2d-x游戏源码

    《cocos2d-x游戏源码解析》 Cocos2d-x是一款强大的开源游戏开发框架,主要用于构建2D游戏、演示程序和其他图形交互应用。它基于C++,同时提供了Lua和JavaScript的绑定,使得开发者可以选择自己熟悉的语言进行游戏...

    跳一跳cocos2D

    我们可以通过`CCSprite::create()`函数加载图片资源,然后在场景(Scene)中添加这些精灵。 2. **运动与物理模拟**:“跳一跳”中的角色跳跃依赖于物理引擎。Cocos2D内置了Box2D物理引擎,我们可以创建物理世界,...

    cocos2d-x学习笔记(5)-- CCScene场景的切换(带过度效果).rar

    在cocos2d-x游戏开发中,场景(CCScene)是构成游戏的基本元素之一,它代表着游戏中的一个独立状态或阶段。"cocos2d-x学习笔记(5)-- CCScene场景的切换(带过度效果)"这个主题聚焦于如何在游戏过程中平滑地从一个场景...

    cocos2d精灵教程

    (译)cocos2d精灵教程:第三部分.pdf (译)cocos2d精灵教程:第二部分.pdf (译)cocos2d菜单教程:第一部分.pdf (译)cocos2d菜单教程:第三部分(完).pdf (译)cocos2d菜单教程:第二部分.pdf

    android cocos 2D范例

    Cocos2D提供了一些工具类,如`CCSprite`用于显示图片,`CCTextureCache`用于管理纹理,以及`CCEffectNode`用于添加特效等。 8. **动画和帧序列**: Cocos2D支持帧动画,可以用来创建角色的动作或游戏效果。开发者...

    cocos2d-3.0 api文档

    1. CCSprite:是最基本的图形元素,可以显示图片并执行动画。 2. CCLabel:用于创建文本标签,支持多种字体和格式。 3. CCNode:是所有图形对象的基类,包含位置、缩放、旋转等属性。 4. CCTexture2D:处理图像纹理...

    cocos2d-android

    《cocos2d-android:开启你的Android 2D游戏开发之旅》 在移动游戏开发领域,Cocos2d-x是一个非常流行的开源框架,它为开发者提供了便捷的2D游戏开发工具。而cocos2d-android,正是这个框架的Android版本,专为...

    知易cocos2d源代码

    【知易cocos2d源代码】是一套完整的Cocos2D框架源码,专为初学者设计,便于他们入门并学习iPhone游戏开发。Cocos2D是一个流行的游戏开发框架,广泛应用于iOS、Android以及Mac OS X等平台,它简化了2D游戏的构建过程...

    我自己的cocos2d学习资料源码

    Cocos2d中的`CCSprite`可以用来创建子弹和敌人的图形表示,而发射子弹的动画可以通过`CCAction`实现。子弹的运动轨迹可能由简单的物理规则(如直线运动)或复杂的物理引擎模拟。敌人检测通常涉及遍历游戏世界中的...

    cocos2dx 3.x实现精灵色相的修改

    Debug模式下,需要注释掉 CCGLProgramState.h apply() 方法中 switch语句default分支下的断言 调用方法 CSpriteWithHue *sprite = CCSpriteWithHue::create("HelloWorld.png"); sprite-&gt;setHue(4.6);...

    cocos2d-android.zip_android_android Cocos2d.jar_cocos2d android.

    《Cocos2d-android.jar:Android游戏开发的关键组件》 在Android游戏开发领域,Cocos2d-x是一个广泛使用的开源游戏引擎,它基于C++,并提供了多种语言接口,包括Java,使得开发者能够轻松地创建跨平台的游戏。本文...

Global site tag (gtag.js) - Google Analytics