cocos2d-lua 左右移动广播条 跑马灯效果

前端之家收集整理的这篇文章主要介绍了cocos2d-lua 左右移动广播条 跑马灯效果前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

直接上代码

  1. local braodWidth = 150 --跑马灯的长度
  2. local label = cc.Label:createWithSystemFont("关于--------------hahhahdasdfwetf,asdlfawefasf","Microsoft YaHei",25)
  3. :setPosition(cc.p(0,0))
  4. :setAnchorPoint(cc.p(0,0))
  5. local labelWidth = label:getContentSize().width
  6. local time = 3 -- 这里可以根据label多长动态计算时间
  7. local scrollViewLayer = cc.Layer:create():setPosition(cc.p(0,0))
  8. scrollViewLayer:setContentSize(label:getContentSize())
  9.  
  10.  
  11. local scrollView1 = cc.ScrollView:create()
  12. if nil ~= scrollView1 then
  13. scrollView1:setViewSize(cc.size(braodWidth,100))
  14. scrollView1:setPosition(cc.p(display.cx,display.cy))
  15. scrollView1:setDirection(cc.SCROLLVIEW_DIRECTION_NONE )
  16. scrollView1:setClippingToBounds(true)
  17. scrollView1:setBounceable(true)
  18. scrollView1:setTouchEnabled(false)
  19. end
  20. scrollView1:addChild(label)
  21. self:addChild(scrollView1)
  22.  
  23.  
  24. if nil ~= scrollViewLayer_ then
  25. scrollView1:setContainer(scrollViewLayer)
  26. scrollView1:updateInset()
  27. end
  28.  
  29.  
  30. if labelWidth > braodWidth then
  31. local leftAction = cc.MoveBy:create(time,cc.p(braodWidth -labelWidth,0))
  32. local rightAction = cc.MoveBy:create(time,cc.p(labelWidth - braodWidth,0))
  33. local seqAction = cc.Sequence:create(leftAction,rightAction)
  34. label:runAction(cc.RepeatForever:create(seqAction))
  35. end

猜你在找的Cocos2d-x相关文章