-- add background image
local bg = display.newSprite("bg2.png")
bg:move(display.center)
bg:addTo(self)
bg:setPosition3D(cc.Vertex3F(display.size.width/2,display.size.height/2,0))
bg:setGlobalZOrder(-1)
local size = display.size
local spobj = cc.Sprite3D:create("Sprite3DTest/boss.obj")
spobj:setTexture("Sprite3DTest/boss.png")
spobj:setScale(5)
self:addChild(spobj)
spobj:setPosition3D(cc.Vertex3F(size.width/2,size.height/2-400,0))
spobj:setRotation3D(cc.Vertex3F(-90,0,0))
local a1 = cc.MoveTo:create(4,cc.Vertex3F(size.width/2,size.height/2+400,0))
local a11 = cc.CallFunc:create(function() spobj:setRotation3D(cc.Vertex3F(90,0,0)) end)
local a111 = cc.MoveTo:create(4,cc.Vertex3F(size.width/2,size.height/2-400,0))
local a1111 = cc.CallFunc:create(function() spobj:setRotation3D(cc.Vertex3F(-90,0,0)) end)
spobj:runAction(cc.RepeatForever:create(cc.Sequence:create(a1,a11,a111,a1111)))
local spobj1 = cc.Sprite3D:create("Sprite3DTest/boss.obj")
spobj1:setTexture("Sprite3DTest/boss.png")
spobj1:setScale(5)
self:addChild(spobj1)
spobj1:setPosition3D(cc.Vertex3F(size.width/2-400,size.height/2,20))
spobj1:setRotation3D(cc.Vertex3F(0,90,0))
local a2 = cc.MoveTo:create(4,cc.Vertex3F(size.width/2+400,size.height/2,20))
local a21 = cc.CallFunc:create(function() spobj1:setRotation3D(cc.Vertex3F(0,-90,0)) end)
local a211 = cc.MoveTo:create(4,cc.Vertex3F(size.width/2-400,size.height/2,20))
local a2111 = cc.CallFunc:create(function() spobj1:setRotation3D(cc.Vertex3F(0,90,0)) end)
spobj1:runAction(cc.RepeatForever:create(cc.Sequence:create(a2,a21,a211,a2111)))
local spobj2 = cc.Sprite3D:create("Sprite3DTest/boss.obj")
spobj2:setTexture("Sprite3DTest/boss.png")
spobj2:setScale(5)
self:addChild(spobj2)
spobj2:setPosition3D(cc.Vertex3F(size.width/2+100,size.height/2,-400))
spobj2:setRotation3D(cc.Vertex3F(0,0,-30))
local a3 = cc.MoveTo:create(4,cc.Vertex3F(size.width/2+100,size.height/2,400))
local a31 = cc.CallFunc:create(function() spobj2:setRotation3D(cc.Vertex3F(0,00,30)) end)
local a311 = cc.MoveTo:create(4,cc.Vertex3F(size.width/2+100,size.height/2,-400))
local a3111 = cc.CallFunc:create(function() spobj2:setRotation3D(cc.Vertex3F(0,0,-30)) end)
spobj2:runAction(cc.RepeatForever:create(cc.Sequence:create(a3,a31,a311,a3111)))
----添加动态模型
self.spt=cc.Sprite3D:create("Sprite3DTest/tortoise.c3b")
self.spt:setScale(0.1)
self.spt:setPosition3D(cc.Vertex3F(size.width/2+220,size.height/2,10))
self:addChild(self.spt)
----创建3D动画
local animation = cc.Animation3D:create("Sprite3DTest/tortoise.c3b")
local animate = cc.Animate3D:create(animation,0,4)
self.spt:runAction(cc.RepeatForever:create(animate))
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 = self:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, self.spt)
self:scheduleUpdate(handler(self, self.update))