复习cocos2dx + lua (一) 精灵的创建

以下为个人学习记录和网上资料收集,有不对的地方可以提下我会更改的:

一 .精灵的创建

1.用图片直接创建精灵(图片的大小就是精灵的大小)

             local  sp = cc.sprite:create(imagepath);

            sp:setContentSize(cc.size(width, height));

            sp:setPosition(cc.p(x, y));

             //imagepath:资源路径下的绝对路径

2.用图片直接创建精灵(创建并直接设置精灵的大小)

            local sp = cc.sprite:create(imagepath, CCRect(x, y, width, height));

3.通过帧缓存创建(在反复使用精灵对象的时候可以减少内存消耗)

            CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(plistpath);

            local sp = cc.Sprite:createWithSpriteFrameName:create(图片名字);

4.批量创建精灵

            CCSpriteFrameCache:sharedSpriteFrameCache():addSpriteFramesWithFile(plistpath);

            local batch = CCSpriteBatchNode:create(图集地址或单个图片地址.png);

            self:addChild(batch);

            local  sp = cc.Sprite:createWithSpriteFrameName(想创建的精灵的图片名字.png);

            batch:addChild(sp);

猜你喜欢

转载自blog.csdn.net/lvyan1994/article/details/81673024