零基础学Cocos2d-X 3.0 - 04

忙完两个项目后,终于有时间继续学习Cocos2d-X 了。


常听人说,Cocos2d-X 有四个类是最常用的,包括:

Director 类----> 导演

Scene 类 -----> 场景

Layer 类 ------> 层

Sprite 类 ------> 精灵


稍微大概理解一下吧。


一个舞台只有一个导演在执导,所以这个是比较好理解的。

一个舞台会上演很多场场景戏,不同的场景会在导演的指示下进行切换、进行,这个也没什么问题。

一个场景可以放很多层,就正如你在开发iOS的时候,你会给一个self.view 添加很多 subview的道理一样,这个意会吧,我还不能言传。

一个舞台上有很多工作人员,他们可以是百般武艺,可以是变化无穷,太难形容了,直接叫工作人员做精灵好了。


从之前的代码可以看出,

导演 Director 类是一个单例

使用方式,如获取屏幕大小:

Director *pDirector = Director::getInstance();
Size visibleSize = pDirector->getVisibleSize();

或者是

Size visibleSize = Director::getInstance()->getVisibleSize();

好方便的啊~~~~~不愧是单例。



场景 Scene 类,在AppDelegate.cpp 里面,我们也见过,有时候想想,是不是和ViewController很像啊

Scene *scene = HelloWorld::createScene();
director->runWithScene(scene);

类似,我只是说类似而已 : )

ViewController *viewController = [[ViewController alloc] init];
self.window.rootViewController = viewController;

一个舞台可以由一个Scene 或多个Scene 组成,就好像iOS 应用可以由一个ViewCotnroller 或多个ViewController组成那样。


层 Layer 类,我感觉用iOS 的视图形容它也应该可以的吧。

你看一个Scene 可以由一个Layer 或多个Layer 组成,而ViewController 也可以由一个 View 或多个 View 组成,多像啊。

Layer 是很重要的,通常多个Layer去实现一个应用、游戏。



精灵 Sprite 类。就是游戏中一个对象,角色。

关于它的我们在稍后将会讲讲。



精灵 Sprite 的创建


    /// @{
    /// @name Creators

    /**
     * Creates an empty sprite without texture. You can call setTexture method subsequently.
     *
     * @return An autoreleased sprite object.
     */
    static Sprite* create();

    /**
     * Creates a sprite with an image filename.
     *
     * After creation, the rect of sprite will be the size of the image,
     * and the offset will be (0,0).
     *
     * @param   filename A path to image file, e.g., "scene1/monster.png"
     * @return  An autoreleased sprite object.
     */
    static Sprite* create(const std::string& filename);

    /**
     * Creates a sprite with an image filename and a rect.
     *
     * @param   filename A path to image file, e.g., "scene1/monster.png"
     * @param   rect     A subrect of the image file
     * @return  An autoreleased sprite object
     */
    static Sprite* create(const std::string& filename, const Rect& rect);

    /**
     * Creates a sprite with a Texture2D object.
     *
     * After creation, the rect will be the size of the texture, and the offset will be (0,0).
     *
     * @param   texture    A pointer to a Texture2D object.
     * @return  An autoreleased sprite object
     */
    static Sprite* createWithTexture(Texture2D *texture);

    /**
     * Creates a sprite with a texture and a rect.
     *
     * After creation, the offset will be (0,0).
     *
     * @param   texture    A pointer to an existing Texture2D object.
     *                      You can use a Texture2D object for many sprites.
     * @param   rect        Only the contents inside the rect of this texture will be applied for this sprite.
     * @param   rotated     Whether or not the rect is rotated
     * @return  An autoreleased sprite object
     */
    static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false);

    /**
     * Creates a sprite with an sprite frame.
     *
     * @param   spriteFrame    A sprite frame which involves a texture and a rect
     * @return  An autoreleased sprite object
     */
    static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame);

    /**
     * Creates a sprite with an sprite frame name.
     *
     * A SpriteFrame will be fetched from the SpriteFrameCache by spriteFrameName param.
     * If the SpriteFrame doesn't exist it will raise an exception.
     *
     * @param   spriteFrameName A null terminated string which indicates the sprite frame name.
     * @return  An autoreleased sprite object
     */
    static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);

    /// @}  end of creators group

从精灵 Sprite 类的头文件可以看到有哪些方法可以创建精灵。

/**********************分割线************************/

创建一个没有贴图的精灵

函数:

static Sprite* create();
使用:

    //创建空白的精灵
    Sprite *aSprite = Sprite::create();
    //为精灵加上贴图
    aSprite->setTexture("Icon-50.png");
    //为精灵设置坐标
    aSprite->setPosition(30, 160);
    //将精灵加入当前层
    this->addChild(aSprite);


/**********************分割线************************/

通过图像名创建一个精灵

函数:

static Sprite* create(const std::string& filename);

使用:

    //通过图像名创建精灵
    Sprite *bSprite = Sprite::create("Icon-50.png");
    //为精灵设置坐标
    bSprite->setPosition(95, 160);
    //将精灵加入当前层
    this->addChild(bSprite);

/**********************分割线************************/

通过图像名和指定切割该图像的范围创建精灵

函数:

static Sprite* create(const std::string& filename, const Rect& rect);

使用:

    //通过图像名和指定切割该图像的范围创建精灵
    Sprite *cSprite = Sprite::create("Icon-50.png", Rect(0, 0, 30, 30));
    //为精灵设置坐标
    cSprite->setPosition(150, 160);
    //将精灵加入当前层
    this->addChild(cSprite);

/**********************分割线************************/

通过Texture2D 对象创建精灵

函数:

static Sprite* createWithTexture(Texture2D *texture);

使用:

    //首先创建 Image对象
    Image *image = new Image();
    //为 Image对象 设置图像,通过指定路径
    image->initWithImageFile("Icon-50.png");
    //创建 Texture2D对象
    Texture2D *text = new Texture2D();
    //为 Texture2D对象 加入 图像内容
    text->initWithImage(image);
    //通过贴图创建精灵
    Sprite *dSprite = Sprite::createWithTexture(text);
    //为精灵设置坐标
    dSprite->setPosition(215, 160);
    //将精灵加入当前层
    this->addChild(dSprite);

/**********************分割线************************/

通过Texture2D 对象和指定切割该对象的范围创建精灵
函数:

static Sprite* createWithTexture(Texture2D *texture, const Rect& rect, bool rotated=false);

使用:

    //首先创建 Image对象
    Image *image2 = new Image();
    //为 Image对象 设置图像,通过指定路径
    image2->initWithImageFile("Icon-50.png");
    //创建 Texture2D对象
    Texture2D *text2 = new Texture2D();
    //为 Texture2D对象 加入 图像内容
    text2->initWithImage(image);
    //通过贴图和切割该贴图创建精灵
    Sprite *eSprite = Sprite::createWithTexture(text2, Rect(0, 0, 30, 30));
    //为精灵设置坐标
    eSprite->setPosition(280, 160);
    //将精灵加入当前层
    this->addChild(eSprite);

/**********************分割线************************/

利用另一帧创建一个精灵

函数:

static Sprite* createWithSpriteFrame(SpriteFrame *spriteFrame);
使用:

    //SpriteFrame对象,通过指定图像和切割该图像的参数创建
    SpriteFrame *frame = SpriteFrame::create("Icon-50.png", Rect(0, 0, 40, 30));
    //通过 SpriteFrame对象 创建精灵
    Sprite *fSprite = Sprite::createWithSpriteFrame(frame);
    //为精灵设置坐标
    fSprite->setPosition(345, 160);
    //将精灵加入当前层
    this->addChild(fSprite);


/**********************分割线************************/

利用帧缓存中一帧的名字创建一个精灵

函数:

static Sprite* createWithSpriteFrameName(const std::string& spriteFrameName);
使用:

    //通过 SpriteFrameName对象 创建精灵
    Sprite *gSprite = Sprite::createWithSpriteFrameName("Icon-50.png");
    //为精灵设置坐标
    gSprite->setPosition(410, 160);
    //将精灵加入当前层
    this->addChild(gSprite);

    //实现的源码分析
    //用 SpriteFrameName 的参数从 SpriteFrameCache 中获取 SpriteFrame对象
    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("testIcon.plist");




最后一个由于没有缓存就无法实现了,要用工具生成plist文件啥的。



终于写完了,虽然好短暂,但是还是好理解的,尤其后面那两个函数,看了很久啊~~~~~苦涩啊~~~~~

预告下一篇是讲精灵常用的函数。。。。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值