MCPcopy Create free account
hub / github.com/axmolengine/axmol / init

Method init

templates/cpp/Source/MainScene.cpp:42–143  ·  view source on GitHub ↗

on "init" you need to initialize your instance

Source from the content-addressed store, hash-verified

40
41// on "init" you need to initialize your instance
42bool MainScene::init()
43{
44 //////////////////////////////
45 // 1. super init first
46 if (!Scene::init())
47 {
48 return false;
49 }
50
51 auto visibleSize = _director->getVisibleSize();
52 auto origin = _director->getVisibleOrigin();
53 auto safeArea = _director->getSafeAreaRect();
54 auto safeOrigin = safeArea.origin;
55
56 /////////////////////////////
57 // 2. add a menu item with "X" image, which is clicked to quit the program
58 // you may modify it.
59
60 // add a "close" icon to exit the progress. it's an autorelease object
61 auto closeItem = MenuItemImage::create("CloseNormal.png", "CloseSelected.png",
62 AX_CALLBACK_1(MainScene::menuCloseCallback, this));
63
64 if (closeItem == nullptr || closeItem->getContentSize().width <= 0 || closeItem->getContentSize().height <= 0)
65 {
66 problemLoading("'CloseNormal.png' and 'CloseSelected.png'");
67 }
68 else
69 {
70 float x = safeOrigin.x + safeArea.size.width - closeItem->getContentSize().width / 2;
71 float y = safeOrigin.y + closeItem->getContentSize().height / 2;
72 closeItem->setPosition(Vec2(x, y));
73 }
74
75 // create menu, it's an autorelease object
76 auto menu = Menu::create(closeItem, NULL);
77 menu->setPosition(Vec2::ZERO);
78 this->addChild(menu, 1);
79
80 /////////////////////////////
81 // 3. add your codes below...
82
83 // Some templates (uncomment what you need)
84 _touchListener = EventListenerTouchAllAtOnce::create();
85 _touchListener->onTouchesBegan = AX_CALLBACK_2(MainScene::onTouchesBegan, this);
86 _touchListener->onTouchesMoved = AX_CALLBACK_2(MainScene::onTouchesMoved, this);
87 _touchListener->onTouchesEnded = AX_CALLBACK_2(MainScene::onTouchesEnded, this);
88 _eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
89
90 //_mouseListener = EventListenerMouse::create();
91 //_mouseListener->onMouseMove = AX_CALLBACK_1(MainScene::onMouseMove, this);
92 //_mouseListener->onMouseUp = AX_CALLBACK_1(MainScene::onMouseUp, this);
93 //_mouseListener->onMouseDown = AX_CALLBACK_1(MainScene::onMouseDown, this);
94 //_mouseListener->onMouseScroll = AX_CALLBACK_1(MainScene::onMouseScroll, this);
95 //_eventDispatcher->addEventListenerWithSceneGraphPriority(_mouseListener, this);
96
97 _keyboardListener = EventListenerKeyboard::create();
98 _keyboardListener->onKeyPressed = AX_CALLBACK_2(MainScene::onKeyPressed, this);
99 _keyboardListener->onKeyReleased = AX_CALLBACK_2(MainScene::onKeyReleased, this);

Callers 1

runMethod · 0.45

Calls 14

createFunction · 0.85
scheduleUpdateFunction · 0.85
problemLoadingFunction · 0.70
initFunction · 0.50
Vec2Function · 0.50
getVisibleSizeMethod · 0.45
getVisibleOriginMethod · 0.45
getSafeAreaRectMethod · 0.45
getContentSizeMethod · 0.45
setPositionMethod · 0.45
addChildMethod · 0.45

Tested by

no test coverage detected