cocos2d-x 3.1.1 学习笔记[2]Sprite 精灵

Sprite应该是用到最多的一个类吧。无法想像一个游戏没有精灵将怎样进行愉快的玩耍。

Sprite继承于Node 和 TextureProtocol.

Sprite是一个2d的图像。

Sprite能够由一个图像创建,或者截取图片中一个矩形创建。

为了优化Sprite的渲染,请遵守下面几点最佳方法。

把你的Sprite都放在同一个精灵表里面

在创建Sprite的时候使用同样的混合方法

批处理会自己主动批处理你的Sprite(使用一个OpenGL call 画出全部的Sprite)

为了获得额外的5%-10%或很多其它的渲染,你能够把SpriteBatchNode来创建精灵。

可是有着下面的限制。

Alias/Antialias属性属于SpriteBatchNode,所以你不能单独的设置aliased属性。

混合方法属性属于SpriteBatchNode,所以你不能单独的设置混合方法属性。

ParallaxNode不支持。可是能够通过代理Sprite来模拟。

Sprite仅仅能把其它的Sprite(或者Sprite的子类)当作子类

默认的anchorPoint为(0,0);

词汇解释

AnchorPoint:精灵的中心点。

创建精灵的七种方法

    //1.  创建一个神马都没有的精灵放到场景里面
//    sp = Sprite::create();
    //2.  依据文件名称创建一个精灵
//    sp = Sprite::create("splash.png");
    //3.  从图片中截取一个矩形来作为精灵
//    sp = Sprite::create("splash.png", Rect(0, 0, 50, 50));
    //4.  依据SpriteFrame创建一个精灵
//    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("home.plist");
//    auto spriteFrame =  SpriteFrameCache::getInstance()->getSpriteFrameByName("icon_touxiangkuang_0.png");
//    sp = Sprite::createWithSpriteFrame(spriteFrame);
    //5.  依据FrameName创建一个精灵
//    SpriteFrameCache::getInstance()->addSpriteFramesWithFile("home.plist");
//    sp = Sprite::createWithSpriteFrameName("icon_touxiangkuang_0.png");
    //6.  根绝TextTure创建一个精灵
//    auto f_sp = Sprite::create("splash.png");
//    sp = Sprite::createWithTexture(f_sp->getTexture());
    //7.  依据TextTure中截取一个矩形来创建精灵
//    auto f_sp = Sprite::create("splash.png");
//    sp = Sprite::createWithTexture(f_sp->getTexture(), Rect(0, 0, 300, 300));



 /*
     *   ---------------------------------------------------------------
     *      Sprite的属性
     *   ---------------------------------------------------------------
     */
    sp = Sprite::create("card_bg_big_26.jpg");
    sp->setAnchorPoint(Vec2(0.5, 0.5));
    sp->setPosition(Vec2(320, 480));
//    //顺时针旋转三十度
//    sp->setRotation(30);
//    //设置是否可见
//    sp->setVisible(true);
//    //设置rgb 255 255 255 为原画
//    sp->setColor(Color3B(0, 255,0));
//    //总体缩放0.8
//    sp->setScale(0.8);
//    //是否关于x翻转
//    sp->setFlippedX(true);
//    //是否关于y翻转
//    sp->setFlippedY(true);
//    //绕x轴旋转三十度
//    sp->setSkewX(30);
//    //绕y轴旋转三十度
//    sp->setSkewY(30);
    
    //获取这个对象的矩形
    Rect rect = sp->getBoundingBox();
    //设置这个精灵的tag
    sp->setTag(520);
    //设置精灵的新的textture
//    sp->setTexture("HelloWorld.png");
//    sp->setTexture(cocos2d::Texture2D *texture);
    //使用TextTure中的矩形
//    sp->setTextureRect(Rect(0, 0, 30, 30));
    
    sp->setGlobalZOrder(1);
    sp->setLocalZOrder(1);
    
    
//    sp->setDirty(true);
//    sp->setContentSize(Size(100, 100));
    
    CCInteger* num = CCInteger::create(5);
    sp->setUserData(num);
    log("UserData is %d",((CCInteger *)sp->getUserData())->getValue());


原文地址:https://www.cnblogs.com/lcchuguo/p/5060255.html