初学cocos2dx lua 最基本的知识
2015-10-08 14:52
459 查看
尊重版权,仅为个人使用,转载自:http://blog.csdn.net/w18767104183/article/details/21177589
最基本的层
[plain] view
plaincopy
function createInGameLayer()
local inGameLayer = cc.Layer:create()
return inGameLayer
end
最基本的场景
[plain] view
plaincopy
local sceneGame = cc.Scene:create()
sceneGame:addChild(createInGameLayer())
cc.Director:getInstance():runWithScene(sceneGame)
cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))
最基本的精灵
[plain] view
plaincopy
function createInGameLayer()
local inGameLayer = cc.Layer:create()
local bg = cc.Sprite:create("farm.jpg")
bg:setAnchorPoint(0,0)
inGameLayer:addChild(bg)
return inGameLayer
end
最基本的定时器
[plain] view
plaincopy
local function tick()
end
cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
最基本的触摸事件
[plain] view
plaincopy
local touchBeginPoint = nil
local function onTouchBegan(touch, event)
local location = touch:getLocation()
cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y)
touchBeginPoint = {x = location.x, y = location.y}
-- CCTOUCHBEGAN event must return true
--[[多点
for i = 1,table.getn(touches) do
local location = touches[i]:getLocation()
Sprite1.addNewSpriteWithCoords(Helper.currentLayer, location)
end
]]--
return true
end
local function onTouchMoved(touch, event)
local location = touch:getLocation()
cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y)
if touchBeginPoint then
local cx, cy = layerFarm:getPosition()
layerFarm:setPosition(cx + location.x - touchBeginPoint.x,
cy + location.y - touchBeginPoint.y)
touchBeginPoint = {x = location.x, y = location.y}
end
end
local function onTouchEnded(touch, event)
local location = touch:getLocation()
cclog("onTouchEnded: %0.2f, %0.2f", location.x, location.y)
touchBeginPoint = nil
spriteDog.isPaused = false
end
local listener = cc.EventListenerTouchOneByOne:create()
--local listener = cc.EventListenerTouchAllAtOnce:create() 多点
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
local eventDispatcher = layerFarm:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layerFarm)
最基本的音乐
[plain] view
plaincopy
--local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg")
local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true)
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)
local function menuCallbackOpenPopup()
-- loop test sound effect
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
menuPopup:setVisible(true)
end
最基本的加载图片
[plain] view
plaincopy
cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)
local texture0 = cc.Director:getInstance():getTextureCache():addImage( "Images/grossini_dance_atlas.png")
function LoadingScene.imageLoaded( pObj)
-- body
end
cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")
cc.Director:getInstance():getTextureCache():removeAllTextures()
cc.Director:getInstance():getTextureCache():removeUnusedTextures()
local cache = cc.SpriteFrameCache:getInstance()
cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png")
SpriteFrameTest.m_pSprite1 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")
最基础的动作
[plain] view
plaincopy
local function CallFucnCallback1()
end
local action = cc.Sequence:create(
cc.MoveBy:create(2, cc.p(200,0)),
cc.CallFunc:create(CallFucnCallback1) )
grossini:runAction(action)
最基础的字符格式化
[plain] view
plaincopy
string.format("grossini_dance_%02d.png", j + 1)
最基础的按钮
[plain] view
plaincopy
local start = cc.Sprite:createWithSpriteFrameName("start.png")
local startItem = cc.MenuItemSprite:create(start, start, start)
local function menuCallback(sender)
cclog("menuCallback...")
--tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(1)
end
startItem:registerScriptTapHandler(menuCallback)
startItem:setPosition(50,50)
local menu = cc.Menu:create()
menu:addChild(startItem)
menu:setPosition(0,0)
layer:addChild(menu)
最基本的层
[plain] view
plaincopy
function createInGameLayer()
local inGameLayer = cc.Layer:create()
return inGameLayer
end
最基本的场景
[plain] view
plaincopy
local sceneGame = cc.Scene:create()
sceneGame:addChild(createInGameLayer())
cc.Director:getInstance():runWithScene(sceneGame)
cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))
最基本的精灵
[plain] view
plaincopy
function createInGameLayer()
local inGameLayer = cc.Layer:create()
local bg = cc.Sprite:create("farm.jpg")
bg:setAnchorPoint(0,0)
inGameLayer:addChild(bg)
return inGameLayer
end
最基本的定时器
[plain] view
plaincopy
local function tick()
end
cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick, 0, false)
最基本的触摸事件
[plain] view
plaincopy
local touchBeginPoint = nil
local function onTouchBegan(touch, event)
local location = touch:getLocation()
cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y)
touchBeginPoint = {x = location.x, y = location.y}
-- CCTOUCHBEGAN event must return true
--[[多点
for i = 1,table.getn(touches) do
local location = touches[i]:getLocation()
Sprite1.addNewSpriteWithCoords(Helper.currentLayer, location)
end
]]--
return true
end
local function onTouchMoved(touch, event)
local location = touch:getLocation()
cclog("onTouchMoved: %0.2f, %0.2f", location.x, location.y)
if touchBeginPoint then
local cx, cy = layerFarm:getPosition()
layerFarm:setPosition(cx + location.x - touchBeginPoint.x,
cy + location.y - touchBeginPoint.y)
touchBeginPoint = {x = location.x, y = location.y}
end
end
local function onTouchEnded(touch, event)
local location = touch:getLocation()
cclog("onTouchEnded: %0.2f, %0.2f", location.x, location.y)
touchBeginPoint = nil
spriteDog.isPaused = false
end
local listener = cc.EventListenerTouchOneByOne:create()
--local listener = cc.EventListenerTouchAllAtOnce:create() 多点
listener:registerScriptHandler(onTouchBegan,cc.Handler.EVENT_TOUCH_BEGAN )
listener:registerScriptHandler(onTouchMoved,cc.Handler.EVENT_TOUCH_MOVED )
listener:registerScriptHandler(onTouchEnded,cc.Handler.EVENT_TOUCH_ENDED )
local eventDispatcher = layerFarm:getEventDispatcher()
eventDispatcher:addEventListenerWithSceneGraphPriority(listener, layerFarm)
最基本的音乐
[plain] view
plaincopy
--local bgMusicPath = CCFileUtils:getInstance():fullPathForFilename("background.ogg")
local bgMusicPath = cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
cc.SimpleAudioEngine:getInstance():playMusic(bgMusicPath, true)
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
cc.SimpleAudioEngine:getInstance():preloadEffect(effectPath)
local function menuCallbackOpenPopup()
-- loop test sound effect
local effectPath = cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
effectID = cc.SimpleAudioEngine:getInstance():playEffect(effectPath)
menuPopup:setVisible(true)
end
最基本的加载图片
[plain] view
plaincopy
cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)
local texture0 = cc.Director:getInstance():getTextureCache():addImage( "Images/grossini_dance_atlas.png")
function LoadingScene.imageLoaded( pObj)
-- body
end
cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")
cc.Director:getInstance():getTextureCache():removeAllTextures()
cc.Director:getInstance():getTextureCache():removeUnusedTextures()
local cache = cc.SpriteFrameCache:getInstance()
cache:addSpriteFrames("animations/grossini_gray.plist", "animations/grossini_gray.png")
SpriteFrameTest.m_pSprite1 = cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")
最基础的动作
[plain] view
plaincopy
local function CallFucnCallback1()
end
local action = cc.Sequence:create(
cc.MoveBy:create(2, cc.p(200,0)),
cc.CallFunc:create(CallFucnCallback1) )
grossini:runAction(action)
最基础的字符格式化
[plain] view
plaincopy
string.format("grossini_dance_%02d.png", j + 1)
最基础的按钮
[plain] view
plaincopy
local start = cc.Sprite:createWithSpriteFrameName("start.png")
local startItem = cc.MenuItemSprite:create(start, start, start)
local function menuCallback(sender)
cclog("menuCallback...")
--tolua.cast(ret:getParent(), "cc.LayerMultiplex"):switchTo(1)
end
startItem:registerScriptTapHandler(menuCallback)
startItem:setPosition(50,50)
local menu = cc.Menu:create()
menu:addChild(startItem)
menu:setPosition(0,0)
layer:addChild(menu)
相关文章推荐
- Cocos2D的平台检查宏
- Cocos2D的平台检查宏
- Cocos2D的平台检查宏
- Android和iOS中Cocos2D日志为什么会出现skip frames
- Android和iOS中Cocos2D日志为什么会出现skip frames
- Android和iOS中Cocos2D日志为什么会出现skip frames
- cocos2dx绑定lua并进行代码测试
- cocos2dx-lua v3 -sprit 精灵详解
- 移植Cocos2D到Android平台的原理
- 移植Cocos2D到Android平台的原理
- 移植Cocos2D到Android平台的原理
- cocos 游戏移植到 Windows10 之路 -- 环境搭建
- 火云开发课堂 - 《Shader从入门到精通》系列 第十五节:在Shader中对3D模型进行颜色设置
- 寒風的Cocos之旅之C++篇---drawNode用法
- cocos2d-x 持续动作笔记
- 寒風的Cocos之旅C++篇----Cocos2d-x应用代理
- cocos2dx2.2屏幕适配iphone6 plus
- Cocos2d项目建立后,Android平台和Win32平台注意以下几点
- cocos代码研究(14)Widget子类Button学习笔记
- cocos代码研究(13)Widget子类EditBox学习笔记