2018-01-29 16:25:32 +08:00
/****************************************************************************
Copyright ( c ) 2017 - 2018 Xiamen Yaji Software Co . , Ltd .
http : //www.cocos2d-x.org
Permission is hereby granted , free of charge , to any person obtaining a copy
of this software and associated documentation files ( the " Software " ) , to deal
in the Software without restriction , including without limitation the rights
to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
copies of the Software , and to permit persons to whom the Software is
furnished to do so , subject to the following conditions :
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software .
THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE .
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2013-02-19 15:38:30 +08:00
# include "HelloWorldScene.h"
2016-03-28 14:35:59 +08:00
# include "SimpleAudioEngine.h"
2013-02-19 15:38:30 +08:00
USING_NS_CC ;
2013-09-19 07:50:26 +08:00
Scene * HelloWorld : : createScene ( )
2013-02-19 15:38:30 +08:00
{
2016-12-21 13:49:59 +08:00
return HelloWorld : : create ( ) ;
2013-02-19 15:38:30 +08:00
}
2017-07-10 13:45:55 +08:00
// Print useful error message instead of segfaulting when files are not there.
static void problemLoading ( const char * filename )
{
printf ( " Error while loading: %s \n " , filename ) ;
printf ( " Depending on how you compiled you might have to add 'Resources/' in front of filenames in HelloWorldScene.cpp \n " ) ;
}
2013-02-19 15:38:30 +08:00
// on "init" you need to initialize your instance
bool HelloWorld : : init ( )
{
//////////////////////////////
// 1. super init first
2016-12-21 13:49:59 +08:00
if ( ! Scene : : init ( ) )
2013-02-19 15:38:30 +08:00
{
return false ;
}
2017-07-10 13:45:55 +08:00
2016-03-04 11:21:30 +08:00
auto visibleSize = Director : : getInstance ( ) - > getVisibleSize ( ) ;
2014-05-16 22:21:36 +08:00
Vec2 origin = Director : : getInstance ( ) - > getVisibleOrigin ( ) ;
2013-02-19 15:38:30 +08:00
/////////////////////////////
// 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
2013-09-19 07:50:26 +08:00
auto closeItem = MenuItemImage : : create (
" CloseNormal.png " ,
" CloseSelected.png " ,
CC_CALLBACK_1 ( HelloWorld : : menuCloseCallback , this ) ) ;
2017-07-13 09:53:03 +08:00
if ( closeItem = = nullptr | |
closeItem - > getContentSize ( ) . width < = 0 | |
closeItem - > getContentSize ( ) . height < = 0 )
2017-07-10 13:45:55 +08:00
{
problemLoading ( " 'CloseNormal.png' and 'CloseSelected.png' " ) ;
}
else
{
2017-07-13 09:53:03 +08:00
float x = origin . x + visibleSize . width - closeItem - > getContentSize ( ) . width / 2 ;
2017-07-17 15:53:57 +08:00
float y = origin . y + closeItem - > getContentSize ( ) . height / 2 ;
2017-07-13 09:53:03 +08:00
closeItem - > setPosition ( Vec2 ( x , y ) ) ;
2017-07-10 13:45:55 +08:00
}
2013-02-19 15:38:30 +08:00
// create menu, it's an autorelease object
2013-09-19 07:50:26 +08:00
auto menu = Menu : : create ( closeItem , NULL ) ;
2014-05-16 22:21:36 +08:00
menu - > setPosition ( Vec2 : : ZERO ) ;
2013-07-28 00:16:16 +08:00
this - > addChild ( menu , 1 ) ;
2013-02-19 15:38:30 +08:00
/////////////////////////////
// 3. add your codes below...
// add a label shows "Hello World"
// create and initialize a label
2017-07-10 13:45:55 +08:00
2014-10-17 17:02:22 +08:00
auto label = Label : : createWithTTF ( " Hello World " , " fonts/Marker Felt.ttf " , 24 ) ;
2017-07-10 13:45:55 +08:00
if ( label = = nullptr )
{
problemLoading ( " 'fonts/Marker Felt.ttf' " ) ;
}
else
{
// position the label on the center of the screen
label - > setPosition ( Vec2 ( origin . x + visibleSize . width / 2 ,
origin . y + visibleSize . height - label - > getContentSize ( ) . height ) ) ;
2013-02-19 15:38:30 +08:00
2017-07-10 13:45:55 +08:00
// add the label as a child to this layer
this - > addChild ( label , 1 ) ;
}
2013-02-19 15:38:30 +08:00
// add "HelloWorld" splash screen"
2013-09-19 07:50:26 +08:00
auto sprite = Sprite : : create ( " HelloWorld.png " ) ;
2017-07-10 13:45:55 +08:00
if ( sprite = = nullptr )
{
problemLoading ( " 'HelloWorld.png' " ) ;
}
else
{
// position the sprite on the center of the screen
sprite - > setPosition ( Vec2 ( visibleSize . width / 2 + origin . x , visibleSize . height / 2 + origin . y ) ) ;
2013-02-19 15:38:30 +08:00
2017-07-10 13:45:55 +08:00
// add the sprite as a child to this layer
this - > addChild ( sprite , 0 ) ;
}
2013-02-19 15:38:30 +08:00
return true ;
}
2014-02-20 11:31:49 +08:00
void HelloWorld : : menuCloseCallback ( Ref * pSender )
2013-02-19 15:38:30 +08:00
{
2016-06-08 10:55:02 +08:00
//Close the cocos2d-x game scene and quit the application
2013-07-12 11:50:36 +08:00
Director : : getInstance ( ) - > end ( ) ;
2013-02-19 15:38:30 +08:00
2016-06-08 10:55:02 +08:00
# if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
2013-02-19 15:38:30 +08:00
exit ( 0 ) ;
# endif
2017-07-10 13:45:55 +08:00
2016-06-08 10:55:02 +08:00
/*To navigate back to native iOS screen(if present) without quitting the application ,do not use Director::getInstance()->end() and exit(0) as given above,instead trigger a custom event created in RootViewController.mm as below*/
2017-07-10 13:45:55 +08:00
2016-06-08 10:55:02 +08:00
//EventCustom customEndEvent("game_scene_close_event");
//_eventDispatcher->dispatchEvent(&customEndEvent);
2017-07-10 13:45:55 +08:00
2013-02-19 15:38:30 +08:00
}