-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelloWorldScene.cpp
112 lines (87 loc) · 3.44 KB
/
HelloWorldScene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include "HelloWorldScene.h"
#include "GameScene.h"
#include "UI/CocosGUI.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("mySprite.plist");
auto texture = Director::getInstance()->getTextureCache()->addImage("mySprite.pvr.ccz");
batchNode = SpriteBatchNode::createWithTexture(texture);
/////////////////////////////
// 2. add a menu item with "X" image, which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2 ,
origin.y + closeItem->getContentSize().height/2));
// create menu, it's an autorelease object
auto menu = Menu::create(closeItem, NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
auto label = Label::createWithTTF("TypeGame", "fonts/Marker Felt.ttf", 48);
// position the label on the center of the screen
label->setPosition(Vec2(origin.x + visibleSize.width/2,
origin.y + visibleSize.height - label->getContentSize().height));
//label->
// add the label as a child to this layer
this->addChild(label, 1);
auto sprite = Sprite::createWithSpriteFrameName("background.jpg");
float x = sprite->getContentSize().width;
float y = sprite->getContentSize().height;
sprite->setScale(800/x, 600/y);
// position the sprite on the center of the screen
sprite->setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
this->addChild(sprite, 0);
//UIbutton
auto btn =cocos2d::ui::Button::create();
btn->cocos2d::Node::setScale(0.5);
btn->setTitleText("Start");
btn->setTitleFontSize(60);
btn->setTitleColor(Color3B::BLUE);
btn->addClickEventListener([](Ref*){
auto game = Game::createScene();
Director::getInstance()->pushScene(game);
});
btn->cocos2d::Node::setPosition(Vec2(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y-50));
this->addChild(btn,1);
return true;
}
void HelloWorld::startGame(Ref* pSender)
{
auto game = Game::createScene();
Director::getInstance()->pushScene(game);
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}