local size = cc.Director:getInstance():getWinSize()
local kTagTileMap = 1
local kTagSpriteBatchNode = 1
local kTagNode = 2
local kTagAnimation1 = 1
local kTagSpriteLeft = 2
local kTagSpriteRight = 2
local kTagSprite1 = 0
local kTagSprite2 = 1
local kTagSprite3 = 2
local kTagSprite4 = 3
local kTagSprite5 = 4
local kTagSprite6 = 5
local kTagSprite7 = 6
local kTagSprite8 = 7
local testLayer = nil
local entry = nil
--------------------------------------------
---- test sprite1
--------------------------------------------
local Sprite1 = {}
Sprite1.__index = Sprite1
function Sprite1.addNewSpriteWithCoords(layer, point)
local idx = math.floor(math.random() * 1400 / 100)--获得每个人物图的起始坐标
local x = math.floor(math.mod(idx,5) * 85)
local y = math.floor(idx / 5) * 121
local sprite = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(x,y,85,121) )--从区域中获取图像
layer:addChild( sprite )
sprite:setPosition( cc.p(point.x, point.y) )
local action = nil
local random = math.random()
cclog("random = " .. random)
if( random < 0.20 ) then
action = cc.ScaleBy:create(3, 2)
elseif(random < 0.40) then
action = cc.RotateBy:create(3, 360)
elseif( random < 0.60) then
action = cc.Blink:create(1, 3)
elseif( random < 0.8 ) then
action = cc.TintBy:create(2, 0, -255, -255)--时间 三原色 染色
else
action = cc.FadeOut:create(2)
end
local action_back = action:reverse()
local seq = cc.Sequence:create( action, action_back)
sprite:runAction( cc.RepeatForever:create(seq) )
end
function Sprite1.onTouchesEnd(touches, event)
for i = 1,table.getn(touches) do
local location = touches[i]:getLocation()
Sprite1.addNewSpriteWithCoords(Helper.currentLayer, location)
end
end
function Sprite1.create()
cclog("sprite1")
local layer = cc.Layer:create()
Helper.initWithLayer(layer)
Sprite1.addNewSpriteWithCoords(layer, cc.p(size.width/2, size.height/2))
local listener = cc.EventListenerTouchAllAtOnce:create()--创建多点触控事件监听
listener:registerScriptHandler(Sprite1.onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED )--注册监听回调函数
local eventDispatcher = layer:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)--在layer层中添加侦听器
Helper.titleLabel:setString("Sprite (tap screen)")
return layer
end
--------------------------------------------
---- test SpriteBatchNode
--------------------------------------------
local SpriteBatchNode1 = {}
SpriteBatchNode1.__index = SpriteBatchNode1
function SpriteBatchNode1.addNewSpriteWithCoords(layer, point)
local BatchNode = layer:getChildByTag( kTagSpriteBatchNode )
local idx = math.floor(math.random() * 1400 / 100)
local x = math.floor(math.mod(idx,5) * 85)
local y = math.floor(idx / 5) * 121
local sprite = cc.Sprite:createWithTexture(BatchNode:getTexture(), cc.rect(x,y,85,121) )
layer:addChild( sprite )
sprite:setPosition( cc.p(point.x, point.y) )
local action = nil
local random = math.random()
cclog("random = " .. random)
if( random < 0.20 ) then
action = cc.ScaleBy:create(3, 2)
elseif(random < 0.40) then
action = cc.RotateBy:create(3, 360)
elseif( random < 0.60) then
action = cc.Blink:create(1, 3)
elseif( random < 0.8 ) then
action = cc.TintBy:create(2, 0, -255, -255)
else
action = cc.FadeOut:create(2)
end
local action_back = action:reverse()
local seq = cc.Sequence:create( action, action_back)
sprite:runAction( cc.RepeatForever:create(seq) )
end
function SpriteBatchNode1.onTouchesEnd(touches,event)
for i = 1,table.getn(touches) do
local location = touches[i]:getLocation()
SpriteBatchNode1.addNewSpriteWithCoords(Helper.currentLayer, location)
end
end
function SpriteBatchNode1.create()
local layer = cc.Layer:create()
Helper.initWithLayer(layer)
local BatchNode = cc.SpriteBatchNode:create("Images/grossini_dance_atlas.png", 50)
layer:addChild(BatchNode, 0, kTagSpriteBatchNode)
SpriteBatchNode1.addNewSpriteWithCoords(layer, cc.p(size.width/2, size.height/2))
local listener = cc.EventListenerTouchAllAtOnce:create()
listener:registerScriptHandler(Sprite1.onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED )
local eventDispatcher = layer:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)------添加注册器的流程!!!!!!!
Helper.titleLabel:setString("SpriteBatchNode (tap screen)")
return layer
end
--------------------------------------
---- SpriteColorOpacity
--------------------------------------
local SpriteColorOpacity = {}
SpriteColorOpacity.__index = SpriteColorOpacity
SpriteColorOpacity.entry = nil
function SpriteColorOpacity.setLayerSprite(layer)
local sprite1 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*0, 121*1, 85, 121))
local sprite2 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*1, 121*1, 85, 121))
local sprite3 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*2, 121*1, 85, 121))
local sprite4 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*3, 121*1, 85, 121))
local sprite5 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*0, 121*1, 85, 121))
local sprite6 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*1, 121*1, 85, 121))
local sprite7 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*2, 121*1, 85, 121))
local sprite8 = cc.Sprite:create("Images/grossini_dance_atlas.png", cc.rect(85*3, 121*1, 85, 121))
local s = cc.Director:getInstance():getWinSize()
sprite1:setPosition( cc.p( (s.width/5)*1, (s.height/3)*1) )
sprite2:setPosition( cc.p( (s.width/5)*2, (s.height/3)*1) )
sprite3:setPosition( cc.p( (s.width/5)*3, (s.height/3)*1) )
sprite4:setPosition( cc.p( (s.width/5)*4, (s.height/3)*1) )
sprite5:setPosition( cc.p( (s.width/5)*1, (s.height/3)*2) )
sprite6:setPosition( cc.p( (s.width/5)*2, (s.height/3)*2) )
sprite7:setPosition( cc.p( (s.width/5)*3, (s.height/3)*2) )
sprite8:setPosition( cc.p( (s.width/5)*4, (s.height/3)*2) )
local action = cc.FadeIn:create(2)
local action_back = action:reverse()
local fade = cc.RepeatForever:create( cc.Sequence:create( action, action_back) )
local tintred = cc.TintBy:create(2, 0, -255, -255)
local tintred_back = tintred:reverse()
local red = cc.RepeatForever:create( cc.Sequence:create( tintred, tintred_back) )
local tintgreen = cc.TintBy:create(2, -255, 0, -255)
local tintgreen_back = tintgreen:reverse()
local green = cc.RepeatForever:create( cc.Sequence:create( tintgreen, tintgreen_back) )
local tintblue = cc.TintBy:create(2, -255, -255, 0)
local tintblue_back = tintblue:reverse()
local blue = cc.RepeatForever:create( cc.Sequence:create( tintblue, tintblue_back) )
sprite5:runAction(red)
sprite6:runAction(green)
sprite7:runAction(blue)
sprite8:runAction(fade)
layer:addChild(sprite1, 0, kTagSprite1)
layer:addChild(sprite2, 0, kTagSprite2)
layer:addChild(sprite3, 0, kTagSprite3)
layer:addChild(sprite4, 0, kTagSprite4)
layer:addChild(sprite5, 0, kTagSprite5)
layer:addChild(sprite6, 0, kTagSprite6)
layer:addChild(sprite7, 0, kTagSprite7)
layer:addChild(sprite8, 0, kTagSprite8)
layer:registerScriptHandler(SpriteColorOpacity.onEnterOrExit)--另一种注册监听方式
end
function SpriteColorOpacity.onEnterOrExit(tag)
if tag == "enter" then
SpriteColorOpacity.onEnter()
elseif tag == "exit" then
SpriteColorOpacity.onExit()
end
end
function SpriteColorOpacity.removeAndAddSprite(dt)
local sprite = Helper.currentLayer:getChildByTag(kTagSprite5)
sprite:retain()
Helper.currentLayer:removeChild(sprite, false)
Helper.currentLayer:addChild(sprite, 0, kTagSprite5)
sprite:release()
end
function SpriteColorOpacity.create()
local layer = cc.Layer:create()
Helper.initWithLayer(layer)
SpriteColorOpacity.setLayerSprite(layer)
layer:registerScriptHandler(SpriteColorOpacity.onEnterOrExit)
Helper.titleLabel:setString("Sprite: Color & Opacity")
return layer
end
--------------------------------------
---- SpriteFrameTest
--------------------------------------
local SpriteFrameTest = {}
SpriteFrameTest.__index = SpriteFrameTest
SpriteFrameTest.entry = nil
SpriteFrameTest.m_pSprite1 = nil
SpriteFrameTest.m_pSprite2 = nil
SpriteFrameTest.m_nCounter = 0
function SpriteFrameTest.onEnter()--帧动画的创建过程
local s = cc.Director:getInstance():getWinSize()
local cache = cc.SpriteFrameCache:getInstance()--得到缓存池
cache:addSpriteFrames("animations/grossini.plist")--创建精灵帧的方式:添加到缓存池
cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png")
cache:addSpriteFrames("animations/grossini_blue.plist", "animations/grossini_blue.png")
SpriteFrameTest.m_pSprite1 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")---从缓存里取出
SpriteFrameTest.m_pSprite1:setPosition( cc.p( s.width/2-80, s.height/2) )
local spritebatch = cc.SpriteBatchNode:create("animations/grossini.png")--创图片帧集 一次都渲染(可以理解成添加到了池里也)需要添加到图层里 他的儿子上上面创建的帧精灵
spritebatch:addChild(SpriteFrameTest.m_pSprite1)--
Helper.currentLayer:addChild(spritebatch)
local animFrames = {}
for i = 1,14 do
local frame = cache:getSpriteFrame( string.format("grossini_dance_%02d.png", i) )
animFrames[i] = frame
end
local animation = cc.Animation:createWithSpriteFrames(animFrames, 0.3)
SpriteFrameTest.m_pSprite1:runAction( cc.RepeatForever:create( cc.Animate:create(animation) ) )
SpriteFrameTest.m_pSprite1:setFlipX(false)
SpriteFrameTest.m_pSprite1:setFlipY(false)
SpriteFrameTest.m_pSprite2 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")--
SpriteFrameTest.m_pSprite2:setPosition( cc.p( s.width/2 + 80, s.height/2) )---
Helper.currentLayer:addChild(SpriteFrameTest.m_pSprite2)
local moreFrames = {}--创建连续的帧
for i = 1,14 do
local frame = cache:getSpriteFrame(string.format("grossini_dance_gray_%02d.png",i))
moreFrames[i] = frame
end
for i = 1,4 do
local frame = cache:getSpriteFrame(string.format("grossini_blue_%02d.png",i))
moreFrames[14 + i] = frame
end
--contact
for i = 1, 14 do
moreFrames[18 + i] = animFrames[i]
end
local animMixed = cc.Animation:createWithSpriteFrames(moreFrames, 0.3)--执行帧动画
SpriteFrameTest.m_pSprite2:runAction(cc.RepeatForever:create( cc.Animate:create(animMixed) ) )
SpriteFrameTest.m_pSprite2:setFlipX(false)
SpriteFrameTest.m_pSprite2:setFlipY(false)
performWithDelay(Helper.current
lua 之 sprite
最新推荐文章于 2023-07-28 16:54:51 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)