首页
人工智能
编程教程
编程导航
编程百科
编程问答
编程博文
编程实例
硬件设备
网络运营
软件教程
移动数码
办公软件
操作系统
网络运维
(12) 初学cocos2dx lua
Cocos2d-x
2019-05-18
最基本的层
[plain]
view plain
copy
functioncreateInGameLayer()
localinGameLayer=cc.Layer:create()
returninGameLayer
end
最基本的场景
localsceneGame=cc.Scene:create()
sceneGame:addChild(createInGameLayer())
cc.Director:getInstance():runWithScene(sceneGame)
cc.Director:getInstance():replaceScene(cc.TransitionFade:create(1,WelcomeScene.createScene()))
最基本的精灵
localbg=cc.Sprite:create("farm.jpg")
bg:setAnchorPoint(0,0)
inGameLayer:addChild(bg)
returninGameLayer
end
最基本的定时器
localfunctiontick()
end
cc.Director:getInstance():getScheduler():scheduleScriptFunc(tick,false)
最基本的触摸事件
localtouchBeginPoint=nil
localfunctiononTouchBegan(touch,event)
locallocation=touch:getLocation()
ccl
og("onTouchBegan:%0.2f,%0.2f",location.x,location.y)
touchBeginPoint={x=location.x,y=location.y}
--CCTOUCHBEGANeventmustreturntrue
--[[多点
fori=1,table.getn(
touches
)do
locallocation=
touches
[i]:getLocation()
Sprite1.addNewSpriteWithCoords(Helper.currentLayer,location)
]]--
returntrue
end
localfunctiononTouchMoved(touch,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">
ccl
og("onTouchMoved:%0.2f,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important">
iftouchBeginPointthen
localcx,cy=layerFarm:getPosition()
layerFarm:setPosition(cx+location.x-touchBeginPoint.x,
cy+location.y-touchBeginPoint.y)
localfunctiononTouchEnded(touch,event)
locallocation=touch:getLocation()
ccl
og("onTouchEnded:%0.2f,location.y)
touchBeginPoint=nil
spriteDog.isPaused=false
locallistener=cc.EventListenerTouchOneByOne:create()
--locallistener=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)
localevent
dis
patcher=layerFarm:getEvent
dis
patcher()
event
dis
patcher:addEventListenerWithSceneGra
PHP
riority(listener,layerFarm)
最基本的音乐
--localbgMusicPath=CCFileUtils:getInstance():fullPathForFilename("background.ogg")
localbgMusicPath=cc.FileUtils:getInstance():fullPathForFilename("background.mp3")
cc.SimpleAu
dio
Engine:getInstance():playMusic(bgMusicPath,true)
localeffectPath=cc.FileUtils:getInstance():fullPathForFilename("effect1.wav")
cc.SimpleAu
dio
Engine:getInstance():preloadEffect(effectPath)
localfunctionmenuCallbackOpenPopup()
--looptestsoundeffect
effectID=cc.SimpleAu
dio
Engine:getInstance():playEffect(effectPath)
menuPopup:setVisible(true)
最基本的加载
图片
cc.Director:getInstance():getTextureCache():addImageAsync("DartBlood.png",imageLoaded)
localtexture0=cc.Director:getInstance():getTextureCache():addImage("Images/grossini_dance_atlas.png")
functionLoadingScene.imageLoaded(pObj)
--body
cc.Director:getInstance():getTextureCache():removeTextureForKey("Images/grossinis_sister1-testalpha.png")
cc.Director:getInstance():getTextureCache():removeAllTextures()
cc.Director:getInstance():getTextureCache():removeUnusedTextures()
localcache=cc.SpriteFrameCache:getInstance()
cache:addSpriteFrames("animations/grossini_gray.plist","animations/grossini_gray.png")
SpriteFrameTest.m_pSprite1=cc.Sprite:createWithSpriteFrameName("grossini_dance_01.png")
最基础的动作
localfunctionCallFucnCallback1()
localaction=cc.Sequence:create(
cc.MoveBy:create(2,cc.p(200,0)),
cc.CallFunc:create(CallFucnCallback1))
grossini:runAction(action)
最基础的字符格式化
string.format("grossini_dance_%02d.png",j+1)
最基础的按钮
localstart=cc.Sprite:createWithSpriteFrameName("start.png")
localstartItem=cc.MenuItemSprite:create(start,start,start)
localfunctionmenuCallback(sender)
ccl
og("menuCallback...")
--tolua.cast(ret:getParent(),"cc.LayerMultiplex"):switchTo(1)
startItem:registerScriptTapHandler(menuCallback)
startItem:setPosition(50,50)
localmenu=cc.Menu:create()
menu:addChild(startItem)
menu:setPosition(0,0)
layer:addChild(menu)
相关文章
如何使用CCRenderTexture创建动态纹理 Cocos2d-x 2 1 4
本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
Cocos2D-x-3.0 编译(Win7)
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
Cocos2d-x 2 0 在Windows平台下的使用
Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
quick-cocos2d-x实例之挑战记忆极限设计文档
1. 来源 QuickV3sample项目中的2048样例游戏,以及最近《...
Cocos2d-x 3 X CMake MinGW版本编译运行
Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...