LoginSignup
5
5

More than 5 years have passed since last update.

cocos2dx lua雑なメモ

Posted at

cocos2dx luaを触ってみて
忘れないようにするための自分用メモ

src/config.lua

--[[
コメント通りで0だとprintInfoが出力されなくなる
releaseで行うと0になるようになっていない模様
]]
DEBUG = 2

--[[
src/cocos/init.lua
でrequireするファイルがかわってくる
そのためcocos2dx lua frameworkを使うかどうかこれにより使えないものが多々ありそう
たとえば、ccui.TouchEventTypeとかがnilになる。
eventtypeの条件をべてたで数値で書かないといけないのか代替案があるのか不明

]]
CC_USE_FRAMEWORK = true

--[[
ここも以前は#ifdefでDEBUGのときはと指定していたので
releaseビルドのときはfps=falseになるようにしたいけど。。
]]
CC_SHOW_FPS = true

--[[
調べていない。。
]]
CC_DISABLE_GLOBAL = true

--[[
縦にして画面にfitするように
ここれだけでは縦にならないconfig.jsonをいじる必要あり
]]
CC_DESIGN_RESOLUTION = { 
    width = 640,
    height = 960,
    autoscale = "EXACT_FIT",
}

config.json
{
    "init_cfg":{
       "isLandscape": false,
       "isWindowTop": false,
       "name": "lua",
       "width": 640,
       "height": 960,
       "entry": "src/main.lua",
       "consolePort": 6010,
       "uploadPort": 6020
    }, 
    "simulator_screen_size": [
        {   
            "title": "iPhone 3Gs (320x480)",
            "width": 320,
            "height": 480 
        },
        {   
            "title": "iPhone 4 (640x960)",
            "width": 640,
            "height": 960 
        },  
        {   
            "title": "iPhone 5 (640x1136)",
            "width": 640,
            "height":1136 
        },  
        {   
            "title": "iPad (768x1024)",
            "width": 768,
            "height": 1024
        },  
        {   
            "title": "iPad Retina (1536x2048)",
            "width": 1536,
            "height": 2048
        },  
        {   
            "title": "Android (480x800)",
            "width": 480,
            "height": 800 
        },  
        {   
            "title": "Android (480x854)",
            "width": 480,
            "height": 854 
        },
        {
            "title": "Android (720x1280)",
            "width": 720,
            "height": 1280
        },
        {
            "title": "Android (1080x1920)",
            "width": 1080,
            "height": 1920
        }
    ]
}

config.jsonのisLandscapeをfalseにすると縦にできる

lua frameworkはmvcとなっている

viewはcocos studioを使いたいので試してみたところ、
3.4だとcsbファイルの読み込みに失敗した
3.5だと今のところ読み込みは成功している
cppプロジェクトだとaddSearchPathの指定がないが、
luaプロジェクトだとsrc/main.luaにaddSearchPathにresがあるので
そのままres以下にリソースファイルをおいてあげれば読みこんでくれた。

cocos studioのBattleSceneを試しで読み込んでみる

src/app/views/MainScene.lua

local MainScene = class("MainScene", cc.load("mvc").ViewBase)

MainScene.RESOURCE_FILENAME = "BattleScene.csb"

return MainScene

ViewBaseにRESOURCE_FILENAMEがあったらresから読むらしい

RESOURCE_BINDINGというのもあるこれでtouchイベントをとれるみたいだけど

こんな風にかくのかよくわからない。。

kari.lua
MainScene.RESOURCE_BINDING = {
    nodeName = {
        events = {
            event = "touch"
            method = "onAAAA"

        }
    }
}

MainScene:onAAA(sender, eventType)
    -- if eventType == ccui.TouchEventType.ended then
    if eventType == 2 then
        printInfo("touch!!")
    end
end

とかかけるのかな。
lua力をつけないとわからない。。

onTouchどうもframeworkの↓みたい

src/cocos/framework/extends/UIWidget.lua
local Widget = ccui.Widget

function Widget:onTouch(callback)
    self:addTouchEventListener(function(sender, state)
        local event = {x = 0, y = 0}
        if state == 0 then
            event.name = "began"
        elseif state == 1 then
            event.name = "moved"
        elseif state == 2 then
            event.name = "ended"
        else
            event.name = "cancelled"
        end 
        event.target = sender
        callback(event)
    end)
    return self
end

だから下のようにかくのかも


MainScene:onAAA(event)
    if event.name == "ended" then
        printInfo("touch!!")
    end
end

cocos2dx でlua/javascriptとうぜんだよね記事があったけど
いまいちlua情報がないような。

javascriptがメジャーなのかな。。

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5