1. 程式人生 > >Cocos2d-x 3.1.1 lua-tests 開篇

Cocos2d-x 3.1.1 lua-tests 開篇

-- 引入資原始檔
require "Cocos2d"
require "Cocos2dConstants"
require "Opengl"
require "OpenglConstants"
require "StudioConstants"
require "GuiConstants"
require "src/helper"
require "src/testResource"
require "src/VisibleRect"

require "src/AccelerometerTest/AccelerometerTest"
require "src/ActionManagerTest/ActionManagerTest"
require "src/ActionsEaseTest/ActionsEaseTest"
require "src/ActionsProgressTest/ActionsProgressTest"
require "src/ActionsTest/ActionsTest"
require "src/AssetsManagerTest/AssetsManagerTest"
require "src/BugsTest/BugsTest"
require "src/ClickAndMoveTest/ClickAndMoveTest"
require "src/CocosDenshionTest/CocosDenshionTest"
require "src/CocoStudioTest/CocoStudioTest"
require "src/CurrentLanguageTest/CurrentLanguageTest"
require "src/DrawPrimitivesTest/DrawPrimitivesTest"
require "src/EffectsTest/EffectsTest"
require "src/EffectsAdvancedTest/EffectsAdvancedTest"
require "src/ExtensionTest/ExtensionTest"
require "src/FontTest/FontTest"
require "src/IntervalTest/IntervalTest"
require "src/KeypadTest/KeypadTest"
require "src/LabelTest/LabelTest"
require "src/LabelTestNew/LabelTestNew"
require "src/LayerTest/LayerTest"
require "src/MenuTest/MenuTest"
require "src/MotionStreakTest/MotionStreakTest"
require "src/NewEventDispatcherTest/NewEventDispatcherTest"
require "src/NodeTest/NodeTest"
require "src/OpenGLTest/OpenGLTest"
require "src/ParallaxTest/ParallaxTest"
require "src/ParticleTest/ParticleTest"
require "src/PerformanceTest/PerformanceTest"
require "src/RenderTextureTest/RenderTextureTest"
require "src/RotateWorldTest/RotateWorldTest"
require "src/Sprite3DTest/Sprite3DTest"
require "src/SpriteTest/SpriteTest"
require "src/SceneTest/SceneTest"
require "src/SpineTest/SpineTest"
require "src/Texture2dTest/Texture2dTest"
require "src/TileMapTest/TileMapTest"
require "src/TouchesTest/TouchesTest"
require "src/TransitionsTest/TransitionsTest"
require "src/UserDefaultTest/UserDefaultTest"
require "src/ZwoptexTest/ZwoptexTest"
require "src/LuaBridgeTest/LuaBridgeTest"
require "src/XMLHttpRequestTest/XMLHttpRequestTest"
require "src/PhysicsTest/PhysicsTest"

-- 行間距
local LINE_SPACE = 40

-- 當前位置
local CurPos = {x = 0, y = 0}
-- 開始位置
local BeginPos = {x = 0, y = 0}

-- 定義一張表
local _allTests = {
  { isSupported = true,  name = "Accelerometer"          , create_func=             AccelerometerMain  },
  { isSupported = true,  name = "ActionManagerTest"      , create_func   =         ActionManagerTestMain  },
  { isSupported = true,  name = "ActionsEaseTest"        , create_func   =           EaseActionsTest      },
  { isSupported = true,  name = "ActionsProgressTest"    , create_func   =       ProgressActionsTest      },
  { isSupported = true,  name = "ActionsTest"            , create_func   =               ActionsTest      },
  { isSupported = true,  name = "AssetsManagerTest"      , create_func   =         AssetsManagerTestMain      },
  { isSupported = false,  name = "Box2dTest"              , create_func=                 Box2dTestMain  },
  { isSupported = false,  name = "Box2dTestBed"           , create_func=              Box2dTestBedMain  },
  { isSupported = true,  name = "BugsTest"               , create_func=              BugsTestMain      },
  { isSupported = false,  name = "ChipmunkAccelTouchTest" , create_func=    ChipmunkAccelTouchTestMain  },
  { isSupported = true,  name = "ClickAndMoveTest"       , create_func   =          ClickAndMoveTest      },
  { isSupported = true,  name = "CocosDenshionTest"      , create_func   =         CocosDenshionTestMain  },
  { isSupported = true,  name = "CocoStudioTest"         , create_func   =         CocoStudioTestMain  },
  { isSupported = false,  name = "CurlTest"               , create_func=                  CurlTestMain  },
  { isSupported = true,  name = "CurrentLanguageTest"    , create_func=   CurrentLanguageTestMain      },
  { isSupported = true,  name = "DrawPrimitivesTest"     , create_func=        DrawPrimitivesTest      },
  { isSupported = true,  name = "EffectsTest"            , create_func   =               EffectsTest      },
  { isSupported = true,  name = "EffectAdvancedTest"     , create_func   =        EffectAdvancedTestMain  },
  { isSupported = true,  name = "ExtensionsTest"         , create_func=        ExtensionsTestMain      },
  { isSupported = true,  name = "FontTest"               , create_func   =              FontTestMain      },
  { isSupported = true,  name = "IntervalTest"           , create_func   =              IntervalTestMain  },
  { isSupported = true,  name = "KeypadTest"             , create_func=                KeypadTestMain  },
  { isSupported = true,  name = "LabelTest"              , create_func   =                 LabelTest      },
  { isSupported = true,  name = "LabelTestNew"           , create_func   =                 LabelTestNew      },
  { isSupported = true,  name = "LayerTest"              , create_func   =                 LayerTestMain  },
  { isSupported = true,  name = "LuaBridgeTest"          , create_func   =        LuaBridgeMainTest },
  { isSupported = true,  name = "MenuTest"               , create_func   =                  MenuTestMain  },
  { isSupported = true,  name = "MotionStreakTest"       , create_func   =          MotionStreakTest      },
  { isSupported = false,  name = "MutiTouchTest"          , create_func=          MutiTouchTestMain     },
  { isSupported = true,  name = "NewEventDispatcherTest"  , create_func   =       NewEventDispatcherTest },
  { isSupported = true,  name = "NodeTest"               , create_func   =                  CocosNodeTest },
  { isSupported = true,   name = "OpenGLTest"             , create_func=          OpenGLTestMain     },
  { isSupported = true,  name = "ParallaxTest"           , create_func   =              ParallaxTestMain  },
  { isSupported = true,  name = "ParticleTest"           , create_func   =              ParticleTest      },
  { isSupported = true,  name = "PerformanceTest"        , create_func=           PerformanceTestMain  },
  { isSupported = true,  name = "PhysicsTest"            , create_func =          PhysicsTest  },
  { isSupported = true,  name = "RenderTextureTest"      , create_func   =         RenderTextureTestMain  },
  { isSupported = true,  name = "RotateWorldTest"        , create_func   =           RotateWorldTest      },
  { isSupported = true,  name = "SceneTest"              , create_func   =                 SceneTestMain  },
  { isSupported = true,  name = "SpineTest"              , create_func   =                 SpineTestMain  },
  { isSupported = false,  name = "SchdulerTest"           , create_func=              SchdulerTestMain  },
  { isSupported = false,  name = "ShaderTest"             , create_func=            ShaderTestMain      },
  { isSupported = true,  name = "Sprite3DTest"           , create_func   =                Sprite3DTest    },
  { isSupported = true,  name = "SpriteTest"             , create_func   =                SpriteTest      },
  { isSupported = false,  name = "TextInputTest"          , create_func=             TextInputTestMain  },
  { isSupported = true,  name = "Texture2DTest"          , create_func   =             Texture2dTestMain  },
  { isSupported = false,  name = "TextureCacheTest"       , create_func=      TextureCacheTestMain      },
  { isSupported = true,  name = "TileMapTest"            , create_func   =               TileMapTestMain  },
  { isSupported = true,  name = "TouchesTest"            , create_func   =               TouchesTest      },
  { isSupported = true,  name = "TransitionsTest"        , create_func   =           TransitionsTest      },
  { isSupported = true,  name = "UserDefaultTest"        , create_func=           UserDefaultTestMain  },
  { isSupported = true,  name = "XMLHttpRequestTest"     , create_func   =        XMLHttpRequestTestMain  },
  { isSupported = true,  name = "ZwoptexTest"            , create_func   =               ZwoptexTestMain  }
}

local TESTS_COUNT = table.getn(_allTests)

-- create scene 建立場景
local function CreateTestScene(nIdx)
  cc.Director:getInstance():purgeCachedData()
  local scene = _allTests[nIdx].create_func()
  return scene
end
-- create menu 建立選單
function CreateTestMenu()
  -- 選單層
  local menuLayer = cc.Layer:create()

  local function closeCallback()
    -- 結束執行,釋放正在執行的場景。 
    cc.Director:getInstance():endToLua()
  end
  
  -- 選單回撥
  local function menuCallback(tag)
    print(tag)
    local Idx = tag - 10000
    local testScene = CreateTestScene(Idx)
    if testScene then
      -- 切換場景
      cc.Director:getInstance():replaceScene(testScene)
    end
  end

  -- add close menu 新增關閉選單
  local s = cc.Director:getInstance():getWinSize()
  local CloseItem = cc.MenuItemImage:create(s_pPathClose, s_pPathClose)
  CloseItem:registerScriptTapHandler(closeCallback)
  CloseItem:setPosition(cc.p(s.width - 30, s.height - 30))
  
  local CloseMenu = cc.Menu:create()
  CloseMenu:setPosition(0, 0)
  CloseMenu:addChild(CloseItem)
  menuLayer:addChild(CloseMenu)
  -- 獲取目標平臺
  local targetPlatform = cc.Application:getInstance():getTargetPlatform()
  if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) then
    CloseMenu:setVisible(false)
  end

  -- add menu items for tests
  -- 新增選單項
  local MainMenu = cc.Menu:create()
  local index = 0
  local obj = nil
  for index, obj in pairs(_allTests) do
    -- 建立文字(obj.name 為文字名稱, s_arialPath為字型,24為字型大小)
    local testLabel = cc.Label:createWithTTF(obj.name, s_arialPath, 24)
    -- 設定錨點
    testLabel:setAnchorPoint(cc.p(0.5, 0.5))
    -- 建立選單項標籤
    local testMenuItem = cc.MenuItemLabel:create(testLabel)
    -- 如果此項不支援,則設定選單項不可用
    if not obj.isSupported then
      testMenuItem:setEnabled(false)
    end
    -- 註冊指令碼處理控制代碼
    testMenuItem:registerScriptTapHandler(menuCallback)
    -- 設定選單標籤顯示的位置,設定到居中的位置
    testMenuItem:setPosition(cc.p(s.width / 2, (s.height - (index) * LINE_SPACE)))
    -- 新增選單項到選單中去
    MainMenu:addChild(testMenuItem, index + 10000, index + 10000)
  end
  
  -- 設定選單內容大小
  MainMenu:setContentSize(cc.size(s.width, (TESTS_COUNT + 1) * (LINE_SPACE)))
  MainMenu:setPosition(CurPos.x, CurPos.y)
  -- 將選單新增到層中去
  menuLayer:addChild(MainMenu)

  -- handling touch events
  -- 處理觸控事件
  local function onTouchBegan(touch, event)
    -- 獲取觸控點的位置
    BeginPos = touch:getLocation()
    -- CCTOUCHBEGAN event must return true
    return true
  end

  -- 手指移動時觸發
  local function onTouchMoved(touch, event)
    local location = touch:getLocation()
    local nMoveY = location.y - BeginPos.y
    local curPosx, curPosy = MainMenu:getPosition()
    local nextPosy = curPosy + nMoveY
    local winSize = cc.Director:getInstance():getWinSize()
    if nextPosy < 0 then
      MainMenu:setPosition(0, 0)
      return
    end
    
    if nextPosy > ((TESTS_COUNT + 1) * LINE_SPACE - winSize.height) then
      MainMenu:setPosition(0, ((TESTS_COUNT + 1) * LINE_SPACE - winSize.height))
      return
    end
    
    -- 
    MainMenu:setPosition(curPosx, nextPosy)
    BeginPos = {x = location.x, y = location.y}
    CurPos = {x = curPosx, y = nextPosy}
  end
  
  -- 建立事件監聽器
  local listener = cc.EventListenerTouchOneByOne:create()
  -- 註冊事件監聽
  listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
  listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
  -- 獲取事件派發器
  local eventDispatcher = menuLayer:getEventDispatcher()
  eventDispatcher:addEventListenerWithSceneGraphPriority(listener, menuLayer)

  return menuLayer
end

我們就可以發現,這裡就是介面中選單的實現,mainMenu中還引入了其他檔案,比如testResource.lua

相關推薦

Cocos2d-x-3.15.1結合Android Studio2.3.3和Visual Studio2013開發使用簡單說明

開發前的幾點必要說明: 1、預設您已下載並解壓了cocos2dx,並且安裝Android Studio和VS。 2、版本:cocos2dx從3.15版本開始全面支援Android Studio,可以使用Android Studio 2.3+ 編輯、編譯和除錯C++程式碼

mac下cocos2d-x 3.8.1使用 Android Studio除錯工程總結

隨著Google對Eclipse放棄,轉而大力推薦研發Android Studio這款工具。我輩又怎能落人之後呢? cocos在最近的版本中也推出了android studio版本工程檔案(proj.android-studio)。 但是想法是美好的,現實卻

cocos2d-x 3.15.1 Win10 tolua++環境配置以及遇到的坑

1.      Python版本 V2.7.x,最新為v2.7.13 2.      安裝pip pip 9.0.1 https://pypi.python.org/pypi/pip 3.      Python的安裝路徑如果帶有空格,則需要安裝到一個沒有空格的地方 例

cocos2d-x 3.5】Lua觸控事件

程式碼1 local listenner = cc.EventListenerTouchOneByOne:create() listenner:registerScriptHandler(function(touch, event) local location

Cocos2d-x 3.1.1 lua-tests 開篇

-- 引入資原始檔 require "Cocos2d" require "Cocos2dConstants" require "Opengl" require "OpenglConstants" require "StudioConstants" require "GuiConstants" require

Cocos2d-x 3 1 1 Lua示例 ActionEaseTest(動作)

cc.p(60, s.height * 4 / 5))  -- 初始化層  Helper.initWithLayer(layer)  return layerend-------------------------------------  SpriteEase------------------------

cocos2d-x-3.1 國際化strings.xml解決亂碼問題 (coco2d-x 學習筆記四)

source ron 文件 亂碼問題 resource -s type fileutil ani 今天寫程序的時候發現輸出文字亂碼,盡管在實際開發中把字符串寫在代碼裏是不好的做法。可是有時候也是為了方便,遇到此問題第一時間在腦子裏面聯想到android下的strings

Cocos2d-x 3.1 環境搭建和建立project

Cocos2d-x 3.x改版了非常多,之前搭過一次環境,可是沒截圖。這次趁著重灌電腦,一邊搭建一邊截圖。此博文僅僅是為了記錄而不是為了教學,所以很多其它講的是搭建過程。本文基本上參考這篇部落格:http://www.idcfree.com/article-1488-1.html,這篇部落

Ubuntu 18.04.1 LTS安裝cocos2d-x 3.17簡記

sha ash gtk3 ole root 進入目錄 .py dep cmake 準備工作 下載最新3.17穩定版cocos2d-x: http://www.cocos2d-x.org/download 下載Android Studio,更新到最新版3.2.1。 在And

Cocos2d-x 3.1 環境搭建和建立工程

Cocos2d-x 3.x改版了很多,之前搭過一次環境,但是沒截圖,這次趁著重灌電腦,一邊搭建一邊截圖。此博文只是為了記錄而不是為了教學,所以更多講的是搭建過程。本文基本上參考這篇部落格:http://www.idcfree.com/article-1488-1.html,

Cocos2d-x 3.1 一步步做螢幕適配

本文並不想講關於螢幕適配的概念或者大道理,如果還不瞭解cocos2d-x螢幕適配的,請先看這篇文章:http://www.cocoachina.com/gamedev/cocos/2014/0516/8451.html。本文有一些內容和圖片是引用這篇文章的。看了那麼多網上關

cocos2d-x 2.2.1及android4.3下顯示webview

由於文筆有限,寫的不好,大家多見諒,以後堅持寫,提高自己的語言表達溝通能力。在Cocos2dxActivity中新增一個方法: public LinearLayout onCreateLayout(Cocos2dxGLSurfaceView sufaceView){ Li

cocos2d-x 3.x遊戲開發學習筆記(1)--mac下配置cocos2d-x 3.x開發環境

原文:http://blog.csdn.net/likendsl/article/details/34617725 開啟使用者目錄下.bash_profile檔案,配置環境  [python] view plaincopyprint? vim ~/.b

cocos2d-x新手學習之Helloworld(第三篇)[版本:cocos2d-x-3.1.1]

1.建立新專案 開啟CMD,輸入命令: cocos new Helloworld -p com.hello -l cpp -d E:\WorkSpace\cocos2d-x\l2\ 開始建立新專案,如圖: 建立完後,提示成功,如圖:   專案檔案目錄如下:  

cocos2d-x(vs2012)環境搭建(第一篇)[版本:cocos2d-x-3.1.1]

1.下載資源 2.安裝環境 按"下一步的方式"安裝vs2012和python,安裝完成後,把python的要目錄加到環境變數在中. 把cocos2d-x包解壓到自己要放的裡放,把"[cocos2d-x root]\tools\cocos2d-console\bin\

Cocos2d-x 3.1.1 學習筆記(一)關聯程式邏輯與CocosStudio匯出檔案

專案開始啟動,Team leaer要求我先做簡單的事情,熟悉一下背景音樂和音效的函式。 由於Cocos2d-x的版本更新,網路上我找到的教程不太適合我配置的Cocos2d-x的版本,所以我把我實現的過程寫一下。我使用的是cocos2d-x-3.1.1 個人認為,Cocos

cocos2d-x 3.1.1 學習筆記[6]ScrollView 可以拖動的Layer

//建立一個scroolview ui::ScrollView* scrollView = ui::ScrollView::create(); scrollView->setBounceEnabled(true); //移動的方向是任意

cocos2d-x-3.3-019-碰撞檢測1-矩形區域是否相交

原文同步釋出於我的wiki,檢視原文或更新請移步: 點選開啟連結 原理 Cocos裡面的每個Node都有一個和它對應的矩形區域,通過api getBoundingBox()得到。碰撞監測就是判斷兩個物體的矩形區域是否有交疊,相應的API是intersectsRect。

Cocos2d-x 3.2 Lua示例 ClickAndMoveTest(點選移動測試)

--[[ ClickAndMoveTest.lua 點選與移動 ]]-- -- 獲取螢幕尺寸 local size = cc.Director:getInstance():getWinSize() local layer = nil -- 層 local kTagSprite = 1 --精靈標記 loc

Cocos2d-x 3.2版本以上LUA指令碼熱更新(動態更新)解決方案

 部落格地址:http://blog.csdn.net/qq446569365 能夠進行熱更新,是Lua指令碼的最大優勢,通過熱更新能夠解決諸多問題。例如App Store的稽核,不用每次都提交版本,等待稽核了,直接通過熱更新更新遊戲邏輯和素材即可。只有在進行大版本更