2014-05-20 18:09:04 +08:00
|
|
|
local size = cc.Director:getInstance():getWinSize()
|
2014-07-02 17:00:31 +08:00
|
|
|
local scheduler = cc.Director:getInstance():getScheduler()
|
2014-09-22 15:29:38 +08:00
|
|
|
local attributeNames =
|
|
|
|
{
|
|
|
|
"a_position",
|
|
|
|
"a_color",
|
|
|
|
"a_texCoord",
|
|
|
|
"a_texCoord1",
|
|
|
|
"a_texCoord2",
|
|
|
|
"a_texCoord3",
|
|
|
|
"a_texCoord4",
|
|
|
|
"a_texCoord5",
|
|
|
|
"a_texCoord6",
|
|
|
|
"a_texCoord7",
|
|
|
|
"a_normal",
|
|
|
|
"a_blendWeight",
|
|
|
|
"a_blendIndex",
|
|
|
|
}
|
2014-05-20 18:09:04 +08:00
|
|
|
|
|
|
|
----------------------------------------
|
|
|
|
----Sprite3DBasicTest
|
|
|
|
----------------------------------------
|
|
|
|
|
|
|
|
local Sprite3DBasicTest = {}
|
|
|
|
Sprite3DBasicTest.__index = Sprite3DBasicTest
|
|
|
|
|
|
|
|
function Sprite3DBasicTest.onTouchesEnd(touches, event)
|
|
|
|
for i = 1,table.getn(touches) do
|
|
|
|
local location = touches[i]:getLocation()
|
|
|
|
Sprite3DBasicTest.addNewSpriteWithCoords(Helper.currentLayer, location.x, location.y )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite3DBasicTest.addNewSpriteWithCoords(parent,x,y)
|
|
|
|
local sprite = cc.Sprite3D:create("Sprite3DTest/boss1.obj")
|
|
|
|
sprite:setScale(3.0)
|
|
|
|
sprite:setTexture("Sprite3DTest/boss.png")
|
|
|
|
|
|
|
|
parent:addChild(sprite)
|
|
|
|
sprite:setPosition(cc.p(x,y))
|
|
|
|
|
2014-06-19 14:05:38 +08:00
|
|
|
local random = math.random()
|
2014-05-20 18:09:04 +08:00
|
|
|
local action = nil
|
|
|
|
if random < 0.2 then
|
|
|
|
action = cc.ScaleBy:create(3,2)
|
|
|
|
elseif random < 0.4 then
|
|
|
|
action = cc.RotateBy:create(3, 360)
|
|
|
|
elseif random < 0.6 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 Sprite3DBasicTest.create()
|
|
|
|
local layer = cc.Layer:create()
|
|
|
|
Helper.initWithLayer(layer)
|
|
|
|
Helper.titleLabel:setString("Testing Sprite3D")
|
|
|
|
Helper.subtitleLabel:setString("Tap screen to add more sprites")
|
|
|
|
|
|
|
|
local listener = cc.EventListenerTouchAllAtOnce:create()
|
|
|
|
listener:registerScriptHandler(Sprite3DBasicTest.onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED )
|
|
|
|
|
|
|
|
local eventDispatcher = layer:getEventDispatcher()
|
|
|
|
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)
|
|
|
|
|
|
|
|
Sprite3DBasicTest.addNewSpriteWithCoords(layer, size.width / 2, size.height / 2)
|
|
|
|
return layer
|
|
|
|
end
|
|
|
|
|
2014-09-22 15:29:38 +08:00
|
|
|
----------------------------------------
|
|
|
|
----Sprite3DHitTest
|
|
|
|
----------------------------------------
|
|
|
|
local Sprite3DHitTest = {}
|
|
|
|
Sprite3DHitTest.__index = Sprite3DHitTest
|
|
|
|
|
|
|
|
function Sprite3DHitTest.create()
|
|
|
|
local layer = cc.Layer:create()
|
|
|
|
Helper.initWithLayer(layer)
|
|
|
|
Helper.titleLabel:setString("Testing Sprite3D Touch in 2D")
|
|
|
|
Helper.subtitleLabel:setString("Tap Sprite3D and Drag")
|
|
|
|
|
|
|
|
local sprite1 = cc.Sprite3D:create("Sprite3DTest/boss1.obj")
|
|
|
|
sprite1:setScale(4.0)
|
|
|
|
sprite1:setTexture("Sprite3DTest/boss.png")
|
|
|
|
sprite1:setPosition( cc.p(size.width/2, size.height/2) )
|
|
|
|
sprite1:runAction(cc.RepeatForever:create(cc.RotateBy:create(3, 360)))
|
|
|
|
layer:addChild(sprite1)
|
|
|
|
|
|
|
|
local sprite2 = cc.Sprite3D:create("Sprite3DTest/boss1.obj")
|
|
|
|
sprite2:setScale(4.0)
|
|
|
|
sprite2:setTexture("Sprite3DTest/boss.png")
|
|
|
|
sprite2:setPosition( cc.p(size.width/2, size.height/2) )
|
|
|
|
sprite2:setAnchorPoint(cc.p(0.5, 0.5))
|
|
|
|
sprite2:runAction(cc.RepeatForever:create(cc.RotateBy:create(3, -360)))
|
|
|
|
layer:addChild(sprite2)
|
|
|
|
|
|
|
|
local listener = cc.EventListenerTouchOneByOne:create()
|
|
|
|
listener:setSwallowTouches(true)
|
|
|
|
listener:registerScriptHandler(function (touch, event)
|
|
|
|
local target = event:getCurrentTarget()
|
|
|
|
local rect = target:getBoundingBox()
|
|
|
|
if cc.rectContainsPoint(rect, touch:getLocation()) then
|
|
|
|
print(string.format("sprite3d began... x = %f, y = %f", touch:getLocation().x, touch:getLocation().y))
|
|
|
|
target:setOpacity(100)
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
|
|
|
return false
|
|
|
|
end,cc.Handler.EVENT_TOUCH_BEGAN )
|
|
|
|
|
|
|
|
listener:registerScriptHandler(function (touch, event)
|
|
|
|
local target = event:getCurrentTarget()
|
|
|
|
local x,y = target:getPosition()
|
|
|
|
target:setPosition(cc.p(x + touch:getDelta().x, y + touch:getDelta().y))
|
|
|
|
end, cc.Handler.EVENT_TOUCH_MOVED)
|
|
|
|
|
|
|
|
listener:registerScriptHandler(function (touch, event)
|
|
|
|
local target = event:getCurrentTarget()
|
|
|
|
print("sprite3d onTouchEnd")
|
|
|
|
target:setOpacity(255)
|
|
|
|
end, cc.Handler.EVENT_TOUCH_ENDED)
|
|
|
|
|
|
|
|
local eventDispatcher = layer:getEventDispatcher()
|
|
|
|
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, sprite1)
|
|
|
|
eventDispatcher:addEventListenerWithSceneGraphPriority(listener:clone(), sprite2)
|
|
|
|
|
|
|
|
return layer
|
|
|
|
end
|
|
|
|
|
2014-06-19 14:05:38 +08:00
|
|
|
----------------------------------------
|
|
|
|
----Sprite3DWithSkinTest
|
|
|
|
----------------------------------------
|
|
|
|
local Sprite3DWithSkinTest = {}
|
|
|
|
Sprite3DWithSkinTest.__index = Sprite3DWithSkinTest
|
|
|
|
|
|
|
|
function Sprite3DWithSkinTest.onTouchesEnd(touches, event)
|
|
|
|
for i = 1,table.getn(touches) do
|
|
|
|
local location = touches[i]:getLocation()
|
|
|
|
Sprite3DWithSkinTest.addNewSpriteWithCoords(Helper.currentLayer, location.x, location.y )
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite3DWithSkinTest.addNewSpriteWithCoords(parent,x,y)
|
2014-07-02 17:00:31 +08:00
|
|
|
local sprite = cc.Sprite3D:create("Sprite3DTest/orc.c3b")
|
|
|
|
sprite:setScale(3)
|
|
|
|
sprite:setRotation3D({x = 0, y = 180, z = 0})
|
2014-06-19 14:05:38 +08:00
|
|
|
sprite:setPosition(cc.p(x, y))
|
|
|
|
parent:addChild(sprite)
|
|
|
|
|
2014-07-14 15:28:52 +08:00
|
|
|
local animation = cc.Animation3D:create("Sprite3DTest/orc.c3b")
|
2014-06-19 14:05:38 +08:00
|
|
|
if nil ~= animation then
|
|
|
|
local animate = cc.Animate3D:create(animation)
|
2014-07-14 15:28:52 +08:00
|
|
|
local inverse = false
|
|
|
|
if math.random() == 0 then
|
|
|
|
inverse = true
|
2014-06-19 14:05:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
local rand2 = math.random()
|
2014-07-14 15:28:52 +08:00
|
|
|
local speed = 1.0
|
|
|
|
|
2014-06-19 14:05:38 +08:00
|
|
|
if rand2 < 1/3 then
|
2014-07-14 15:28:52 +08:00
|
|
|
speed = animate:getSpeed() + math.random()
|
2014-06-19 14:05:38 +08:00
|
|
|
elseif rand2 < 2/3 then
|
2014-07-14 15:28:52 +08:00
|
|
|
speed = animate:getSpeed() - 0.5 * math.random()
|
|
|
|
end
|
|
|
|
|
|
|
|
if inverse then
|
|
|
|
animate:setSpeed(-speed)
|
|
|
|
else
|
|
|
|
animate:setSpeed(speed)
|
2014-06-19 14:05:38 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
sprite:runAction(cc.RepeatForever:create(animate))
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function Sprite3DWithSkinTest.create()
|
|
|
|
local layer = cc.Layer:create()
|
|
|
|
Helper.initWithLayer(layer)
|
|
|
|
Helper.titleLabel:setString("Testing Sprite3D for animation from c3t")
|
|
|
|
Helper.subtitleLabel:setString("Tap screen to add more sprite3D")
|
|
|
|
|
|
|
|
local listener = cc.EventListenerTouchAllAtOnce:create()
|
|
|
|
listener:registerScriptHandler(Sprite3DWithSkinTest.onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED )
|
|
|
|
|
|
|
|
local eventDispatcher = layer:getEventDispatcher()
|
|
|
|
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)
|
|
|
|
|
|
|
|
Sprite3DWithSkinTest.addNewSpriteWithCoords(layer, size.width / 2, size.height / 2)
|
|
|
|
return layer
|
|
|
|
end
|
|
|
|
|
2014-05-20 18:09:04 +08:00
|
|
|
|
2014-07-02 17:00:31 +08:00
|
|
|
----------------------------------------
|
|
|
|
----Animate3DTest
|
|
|
|
----------------------------------------
|
|
|
|
local State =
|
|
|
|
{
|
|
|
|
SWIMMING = 0,
|
|
|
|
SWIMMING_TO_HURT = 1,
|
|
|
|
HURT = 2,
|
|
|
|
HURT_TO_SWIMMING = 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
local Animate3DTest = {}
|
|
|
|
Animate3DTest.__index = Animate3DTest
|
|
|
|
|
|
|
|
function Animate3DTest.extend(target)
|
|
|
|
local t = tolua.getpeer(target)
|
|
|
|
if not t then
|
|
|
|
t = {}
|
|
|
|
tolua.setpeer(target, t)
|
|
|
|
end
|
|
|
|
setmetatable(t, Animate3DTest)
|
|
|
|
return target
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function Animate3DTest:onEnter()
|
|
|
|
|
|
|
|
self._hurt = nil
|
|
|
|
self._swim = nil
|
|
|
|
self._sprite = nil
|
|
|
|
self._moveAction = nil
|
|
|
|
self._transTime = 0.1
|
|
|
|
self._elapseTransTime = 0.0
|
|
|
|
|
|
|
|
local function renewCallBack()
|
|
|
|
self._sprite:stopActionByTag(101)
|
|
|
|
self._state = State.HURT_TO_SWIMMING
|
|
|
|
end
|
|
|
|
local function onTouchesEnd(touches, event )
|
|
|
|
for i = 1,table.getn(touches) do
|
|
|
|
local location = touches[i]:getLocation()
|
|
|
|
if self._sprite ~= nil then
|
|
|
|
local len = cc.pGetLength(cc.pSub(cc.p(self._sprite:getPosition()), location))
|
|
|
|
if len < 40 then
|
|
|
|
if self._state == State.SWIMMING then
|
|
|
|
self._sprite:runAction(self._hurt)
|
|
|
|
local delay = cc.DelayTime:create(self._hurt:getDuration() - 0.1)
|
|
|
|
local seq = cc.Sequence:create(delay, cc.CallFunc:create(renewCallBack))
|
|
|
|
seq:setTag(101)
|
|
|
|
self._sprite:runAction(seq)
|
|
|
|
self._state = State.SWIMMING_TO_HURT
|
|
|
|
end
|
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self:addSprite3D()
|
|
|
|
|
|
|
|
local listener = cc.EventListenerTouchAllAtOnce:create()
|
|
|
|
listener:registerScriptHandler(onTouchesEnd,cc.Handler.EVENT_TOUCHES_ENDED )
|
|
|
|
|
|
|
|
local eventDispatcher = self:getEventDispatcher()
|
|
|
|
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self)
|
|
|
|
|
|
|
|
local function update(dt)
|
|
|
|
if self._state == State.HURT_TO_SWIMMING then
|
|
|
|
self._elapseTransTime = self._elapseTransTime + dt
|
|
|
|
local t = self._elapseTransTime / self._transTime
|
|
|
|
|
|
|
|
if t >= 1.0 then
|
|
|
|
t = 1.0
|
|
|
|
self._sprite:stopAction(self._hurt)
|
|
|
|
self._state = State.SWIMMING
|
|
|
|
end
|
|
|
|
self._swim:setWeight(t)
|
|
|
|
self._hurt:setWeight(1.0 - t)
|
|
|
|
elseif self._state == State.SWIMMING_TO_HURT then
|
|
|
|
self._elapseTransTime = self._elapseTransTime + dt
|
|
|
|
local t = self._elapseTransTime / self._transTime
|
|
|
|
if t >= 1.0 then
|
|
|
|
t = 1.0
|
|
|
|
self._state = State.HURT
|
|
|
|
end
|
|
|
|
self._swim:setWeight(1.0 - t)
|
|
|
|
self._hurt:setWeight(t)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self:scheduleUpdateWithPriorityLua(update,0)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animate3DTest:onExit()
|
|
|
|
self._moveAction:release()
|
|
|
|
self._hurt:release()
|
|
|
|
self._swim:release()
|
|
|
|
self:unscheduleUpdate()
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animate3DTest:addSprite3D()
|
|
|
|
-- body
|
|
|
|
local fileName = "Sprite3DTest/tortoise.c3b"
|
|
|
|
local sprite = cc.Sprite3D:create(fileName)
|
|
|
|
sprite:setScale(0.1)
|
|
|
|
local winSize = cc.Director:getInstance():getWinSize()
|
|
|
|
sprite:setPosition(cc.p(winSize.width * 4.0 / 5.0, winSize.height / 2.0))
|
|
|
|
self:addChild(sprite)
|
|
|
|
|
|
|
|
self._sprite = sprite
|
|
|
|
|
2014-07-14 15:28:52 +08:00
|
|
|
local animation = cc.Animation3D:create(fileName)
|
2014-07-02 17:00:31 +08:00
|
|
|
if nil ~= animation then
|
|
|
|
local animate = cc.Animate3D:create(animation, 0.0, 1.933)
|
|
|
|
sprite:runAction(cc.RepeatForever:create(animate))
|
|
|
|
self._swim = animate
|
|
|
|
self._swim:retain()
|
|
|
|
self._hurt = cc.Animate3D:create(animation, 1.933, 2.8)
|
|
|
|
self._hurt:retain()
|
|
|
|
self._state = State.SWIMMING
|
|
|
|
end
|
|
|
|
|
|
|
|
self._moveAction = cc.MoveTo:create(4.0, cc.p(winSize.width / 5.0, winSize.height / 2.0))
|
|
|
|
self._moveAction:retain()
|
|
|
|
|
|
|
|
local function reachEndCallBack()
|
|
|
|
self._sprite:stopActionByTag(100)
|
|
|
|
local inverse = self._moveAction:reverse()
|
|
|
|
inverse:retain()
|
|
|
|
self._moveAction:release()
|
|
|
|
self._moveAction = inverse
|
|
|
|
local rot = cc.RotateBy:create(1.0, { x = 0.0, y = 180.0, z = 0.0})
|
|
|
|
local seq = cc.Sequence:create(rot, self._moveAction, cc.CallFunc:create(reachEndCallBack))
|
|
|
|
seq:setTag(100)
|
|
|
|
self._sprite:runAction(seq)
|
|
|
|
end
|
|
|
|
|
|
|
|
local seq = cc.Sequence:create(self._moveAction, cc.CallFunc:create(reachEndCallBack))
|
|
|
|
seq:setTag(100)
|
|
|
|
sprite:runAction(seq)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Animate3DTest.create()
|
|
|
|
local layer = Animate3DTest.extend(cc.Layer:create())
|
|
|
|
|
|
|
|
if nil ~= layer then
|
|
|
|
Helper.initWithLayer(layer)
|
|
|
|
Helper.titleLabel:setString("Testing Animate3D")
|
|
|
|
Helper.subtitleLabel:setString("Touch to beat the tortoise")
|
|
|
|
|
|
|
|
local function onNodeEvent(event)
|
|
|
|
if "enter" == event then
|
|
|
|
layer:onEnter()
|
|
|
|
elseif "exit" == event then
|
|
|
|
layer:onExit()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
layer:registerScriptHandler(onNodeEvent)
|
|
|
|
end
|
|
|
|
|
|
|
|
return layer
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2014-05-20 18:09:04 +08:00
|
|
|
function Sprite3DTest()
|
|
|
|
local scene = cc.Scene:create()
|
|
|
|
|
|
|
|
Helper.createFunctionTable =
|
|
|
|
{
|
|
|
|
Sprite3DBasicTest.create,
|
2014-09-22 15:29:38 +08:00
|
|
|
Sprite3DHitTest.create,
|
2014-06-19 14:05:38 +08:00
|
|
|
Sprite3DWithSkinTest.create,
|
2014-07-02 17:00:31 +08:00
|
|
|
Animate3DTest.create,
|
2014-05-20 18:09:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
scene:addChild(Sprite3DBasicTest.create())
|
|
|
|
scene:addChild(CreateBackMenuItem())
|
|
|
|
|
|
|
|
return scene
|
|
|
|
end
|