cocos2d-x-lua:API-sprite
来源:互联网 发布:淘宝平铺衣服修图技巧 编辑:程序博客网 时间:2024/05/16 16:55
精灵:
通过图片,纹理,帧来创建,通常精灵表示一张图片
--create bg
local bg = cc.Sprite:create("background.png")bg:setPosition(size.width/2,size.height/2)
layer:addChild(bg)
--create tree1
local tree1 = cc.Sprite:create("tree1.png",cc.rect(604,38,302,295))
tree1:setPosition(100,230)
layer:addChild(tree1,0)
--create tree2
local cacheTree = cc.Director:getInstance():getTextureCache():addImage("tree1.png")
local tree2 = cc.Sprite:create()
tree2:setTexture(cacheTree) --设置纹理
tree2:setTextureRect(cc.rect(73,72,182,270)) --设置裁剪区域
tree2:setPosition(360,200)
layer:addChild(tree2,0)
--[[
cc.rect(x,y,width,height),xy 是UI坐标,坐标原点在左上角,width和height 是裁剪的矩形的宽度和高度。
getTextureCache 创建纹理对象,通过Director的getTextureCache实例,TextureCache的addImage(“tree1.png”)函数可以创建纹理Texture2D对象,
tree1.png是纹理图片名。
local tree2 = cc.Sprite;create()创建了一个空的精灵对象,所以后面需要很多函数设置它的属性
]]--
--[[精灵优化:
精灵表(纹理图集)
纹理图集(Texture Atlas)也称精灵表(Sprite sheet),把许多小精灵组到一张大图中。
制作工具: Zwoptex和TexturePacker
生成2个文件:1是纹理图集,2是plist文件,纹理图集坐标
]]--
--create mountain
local cacheMountain = cc.Director:getInstance():getTextureCache():addImage("SpirteSheet.png")
local mountain1 = cc.Sprite:create()
mountain1:setTexture(cacheMountain)
mountain1:setTextureRect(cc.rect(2,391,934,388))
mountain1:setPosition(160,400)
layer:addChild(mountain1,0)
--create hero
local hero1 = cc.Sprite:create()
hero1:setTexture(cacheMountain)
hero1:setTextureRect(cc.rect(2,1706,391,327))
hero1:setPosition(800,200)
layer:addChild(hero1)
--[[精灵优化:
缓存分类:
纹理缓存,精灵帧缓存,东环缓存
精灵帧缓存(spriteFrameCache),使用plst文件,创建和纹理缓存大同小异,这种方法被舍弃了,大概是时候后必须手动remove的缘故
以后还有spritebathNode的方法来优化需要大量重复的调用,这个是主要使用的方法。
]]--
--[[
--重新创建hero1,使用spriteframecache,注释掉上面的hero1的创建
--定义一个变量来加载一次plist文件,创建更多的对象
cc.SpriteFrameCache:getInstance():addSpriteFramesWithFile("SpirteSheet.plist") --这里报错,查看test项目,发现这个方法名改成了addSpriteFrames,而且3.3中
--getSpriteFrameByName 不被支持做帧动画
hero1:setPosition(800,200)
layer:addChild(hero1)
]]--
补充:实现帧动画可以使用切割的方式一帧一帧的加载,c++实现如下:
//方式二,通过一张集合的图片来创建
//创建2D纹理
auto
texture = Director::getInstance()->getTextureCache()->addImage(
"dragon_animation.png"
);
//建立图片帧
auto
frame0 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*0, 132, 132));
auto
frame1 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*0, 132, 132));
auto
frame2 = SpriteFrame::createWithTexture(texture, Rect(132*2, 132*0, 132, 132));
auto
frame3 = SpriteFrame::createWithTexture(texture, Rect(132*3, 132*0, 132, 132));
auto
frame4 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*1, 132, 132));
auto
frame5 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*1, 132, 132));
auto
sprite2 = Sprite::createWithSpriteFrame(frame0);
sprite2->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
this
->addChild(sprite2);
--Lua 代码实现:使用plist文件来加载到animation:
--create function action
local function OnAction(menuItemSender)
if not isPlaying then
local animation = cc.Animation:create()
animation:addSpriteFrameWithFile("h1.png")
animation:addSpriteFrameWithFile("h2.png")
animation:addSpriteFrameWithFile("h3.png")
animation:addSpriteFrameWithFile("h4.png")
animation:setDelayPerUnit(0.15)
animation:setRestoreOriginalFrame(ture)
local action = cc.Animate:create(animation)
sprite:runAction(cc.RepeatForever:create(action))
isPlaying = true
- cocos2d-x-lua:API-sprite
- Cocos2d-x Lua中Sprite精灵类
- cocos2d-x 精灵 Sprite
- Cocos2d-x Sprite笔记
- cocos2d-x Sprite方法
- 【cocos2d-x 3.5】Lua动画API
- cocos2d-x-lua:API-Toggle菜单
- cocos2d-x-lua:API-scene和layer
- cocos2d-x-lua:API-瞬时动作
- cocos2d Sprite Lua处理实例
- quick-x lua sprite使用
- cocos2d-x 3.0sprite轮换
- cocos2d-x判断sprite点击
- cocos2d-x sprite setcolor 无效
- cocos2d-x判断sprite点击
- cocos2d-x 改变sprite图片
- quick-cocos2d-x 导出 C/C++ API 给 Lua 使用
- cocos2d-x 与 lua
- WP 中各个事件
- 算法与编程
- 关于值传递
- Spring-boot入门
- C#多线程参数传递
- cocos2d-x-lua:API-sprite
- C#多线程学习(一) 多线程的相关概…
- 软件 项目管理 考试注意
- 利用DirectX实现声音播放
- MVVM
- IT 面试题
- IT 面试题
- code-behind
- DataSet 与SqlDataAdapter之…