Quick-Cocos2d-x 学习手册(一):游戏元素

前端之家收集整理的这篇文章主要介绍了Quick-Cocos2d-x 学习手册(一):游戏元素前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

从2D引擎整体角度去看Quick-Cocos2d-x。第一篇是游戏元素,游戏中最多出现的图片、动画声音等元素。游戏开发贯穿始末的东西,必须从这里说起了。


UIImage

1
2
3
cc.ui.UIImage. new ( "40.png" )
:pos(display.cx,display.cy)
:addTo(self)


ScaleUIImage

1
2
3
4
cc.ui.UIImage. new ( "40.png" )
:setLayoutSize(80,80)
:pos(display.cx,display.cy)
:addTo(self)



Scale9UIImage

1
2
3
4
cc.ui.UIImage. new ( "slice30.png" ,{scale9= true })
:setLayoutSize(120,display.cy)
:addTo(self)


TTF文本

1
2
3
4
5
6
7
cc.ui.UILabel. new ({
text= "这是一行TTF文字" ,
size=16,
color=display.COLOR_BLACK,
})
:pos(display.cx,display.cy)
:addTo(self)


Atlas文本

1
2
3
4
5
6
atlasLabel=CCLabelAtlas:create(
"AtlasLabel" , "tuffy_bold_italic-charmap.png" ,
48,64,string.byte( '' )
)
:pos(display.left,display.cy)
self:addChild(atlasLabel)


Button

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@H_918_404@ 19
20
21
22
23
24
btn3_img={
normal= "80x40button_n.png" ,
pressed= "80x40button_h.png" ,
disabled= "80x40button_dis.png" ,
}
btn3=cc.ui.UIPushButton. new (btn3_img)
:setButtonLabel( "normal" ,ui.newTTFLabel({
text= "Button" ,
size=24,
color=ccc3(255,255,255),
}))
:setButtonLabel( "pressed" ,ui.newTTFLabel({
text= "Down" ,
size=24,
color=ccc3(255,
}))
:setButtonLabel( "disabled" ,ui.newTTFLabel({
@H_918_404@ text= "Disable" ,
size=24,
color=ccc3(200,200,200),
}))
:pos(display.cx,display.cy)
:addTo(self)


CheckBox

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
btn1_img={
off= "30check_off.png" ,
off_pressed= "30check_off.png" ,
off_disabled= "30check_off.png" ,
on= "30check_on.png" ,
on_pressed= "30check_on.png" ,
on_disabled= "30check_on.png" ,
}
btn1=cc.ui.UICheckBoxButton. new (btn1_img)
:setButtonLabelOffset(0,0)
:setButtonLabelAlignment(display.CENTER)
:setButtonSelected( false )--初始按下状态
:onButtonStateChanged(function(event)print( "CheckBox点击" )end)
:pos(display.cx,display.cy)
:addTo(self)


Radio Group

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
@H_918_404@ 19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
RAdio_BUTTON_IMAGES={
off= "30radio_off.png" ,
off_pressed= "30radio_off.png" ,
off_disabled= "30radio_dis.png" ,
on= "30radio_on.png" ,
on_pressed= "30radio_on.png" ,
on_disabled= "30radio_dis.png" ,
}
localgroup=cc.ui.UICheckBoxButtonGroup. new (display.TOP_TO_BOTTOM)
:addButton(cc.ui.UICheckBoxButton. new (RAdio_BUTTON_IMAGES)
:setButtonLabel(cc.ui.UILabel. new ({text= "option1" ,color=display.COLOR_BLACK}))
:setButtonLabelOffset(20,0)
:align(display.LEFT_CENTER))
:addButton(cc.ui.UICheckBoxButton. new (RAdio_BUTTON_IMAGES)
:setButtonLabel(cc.ui.UILabel. new ({text= "option2" ,color=display.COLOR_BLACK}))
:setButtonLabelOffset(20,0)
:align(display.LEFT_CENTER))
@H_918_404@ :addButton(cc.ui.UICheckBoxButton. new (RAdio_BUTTON_IMAGES)
:setButtonLabel(cc.ui.UILabel. new ({text= "option3" ,color=display.COLOR_BLACK}))
:setButtonLabelOffset(20,0)
:align(display.LEFT_CENTER))
:addButton(cc.ui.UICheckBoxButton. new (RAdio_BUTTON_IMAGES)
:setButtonLabel(cc.ui.UILabel. new ({text= "option4disabled" ,color=display.COLOR_BLACK}))
:setButtonEnabled( false )
:setButtonLabelOffset(20,0)
:align(display.LEFT_CENTER))
:setButtonsLayoutMargin(0,0)
:onButtonSelectChanged(function(event)
printf ( "Option%dselected,Option%dunselected" ,event.selected,event.last)
end)
:pos(display.cx,display.cy)
:addTo(self)
group:getButtonAtIndex(4):setButtonSelected( true )


HScroll

1
2
3
4
5
6
7
8
9
10
11
12
13
SLIDER_IMAGES={
bar= "120x30hscrollbar.png" ,
button= "60x30hscrollhandle.png" ,
}
cc.ui.UiSlider. new (display.LEFT_TO_RIGHT,SLIDER_IMAGES,{scale9= true })
:onSliderValueChanged(function(event)
printf (string.format( "value=%0.2f" ,event.value))
end)
:setSliderSize(200,30)
:setSliderValue(75)
:pos(display.cx,display.cy)
:addTo(self)


VScroll

1
2
3
4
5
6
7
8
9
10
11
12
13
SLIDER_IMAGES2={
bar= "30x120vscrollbar.png" ,
button= "30x60vscrollhandle.png" ,
}
cc.ui.UiSlider. new (display.TOP_TO_BOTTOM,SLIDER_IMAGES2,event.value))
end)
:setSliderSize(30,200)
:setSliderValue(75)
:align(display.CENTER,display.cx,display.cy)
:addTo(self)


文件Sprite

1
2
3
4
5
localsprite1=display.newSprite( "walkBomb.png" )
sprite1:setPosition(display.cx,display.cy)
sprite1:setAnchorPoint(0.5,0.5)
sprite1:setScale(1)
self:addChild(sprite1)



AtlasSprite

1
2
3
4
display.addSpriteFramesWithFile( "enemy.plist" , "enemy.png" )
localspriteAtlas=display.newSprite( "#beeBomb.png" )
spriteAtlas:setPosition(display.cx,display.cy)
self:addChild(spriteAtlas)


Sprite Sheet

1
2
3
4
5
localsprite=display.newSprite( "#role01.png" ):pos(display.cx,display.cy)
self:addChild(sprite)
localframes=display.newFrames( "roled.png" ,1,10, true )
localanimation=display.newAnimation(frames,0.3/10)
sprite:runAction(CCRepeatForever:create(CCAnimate:create(animation)))


图元Rectangle

1
2
3
localred=display.newRect(80,80,{fill= true ,color=ccc4f(255,0.5)})
red:setPosition(display.cx,display.cy)
self:addChild(red)


图元Polygon

1
2
3
4
5
localpoints={{0,0},{80,80},{160,}
localpolygon=display.newPolygon(points,0.5)})
polygon:setClose( true )
polygon:setPosition(display.cx,display.cy)
self:addChild(polygon)


图元Circle

1
2
3
localcircle=display.newCircle(80,0.5)})
circle:setPosition(display.cx,display.cy)
self:addChild(circle)



Cocos Studio Armature

1
2
3
4
5
6
7
8
display.addSpriteFramesWithFile( "DemoPlayer/DemoPlayer0.plist" , "DemoPlayer/DemoPlayer0.png" )
CCArmatureDataManager:sharedArmatureDataManager():addArmatureFileInfo( "DemoPlayer/DemoPlayer.ExportJson" )
localarmature=CCArmature:create( "DemoPlayer" )
armature:getAnimation():playWithIndex(0)
armature:setScale(0.2)
armature:setAnchorPoint(ccp(0.5,0.5))
armature:setPosition(ccp(display.cx,display.cy))
self:addChild(armature)


DragonBone

1
2
3
4
5
6
7
8
9
10
11
12
13
localmanager=CCArmatureDataManager:sharedArmatureDataManager()
manager:addArmatureFileInfo( "Dragon.png" , "Dragon.plist" , "Dragon.xml" )
localdragon=CCArmature:create( "Dragon" )
self.animationNames={ "stand" , "walk" , "jump" , "fall" }
localaniName=self.animationNames[math.random(1,4)]
localanimation=dragon:getAnimation()
animation:setSpeedScale(24/60)--Flashfpsis24,cocos2d-xis60
animation:play(aniName)
dragon:setPosition(display.cx,display.cy-200)
dragon:setScale(math.random(50,100)/100)
self:addChild(dragon)


内置Particle

1
2
emitter=CCParticleRain:create()
self:addChild(emitter)


音效

1
audio.playSound( "sfx/ConFlipSound.mp3" )



来源网址:http://www.lolofinil.com/2014/06/26/icocos_01/

原文链接:https://www.f2er.com/cocos2dx/344772.html

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