Make game with cocos2d-x: v3.0
file .h: //creat menu game and set color:
=============================================
file .h: //creat menu game and set color:
=============================================
file.cpp :#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
void Play(cocos2d::Ref* pSender);
void Highscores(cocos2d::Ref* pSender);
void Help(cocos2d::Ref* pSender);
void Exit(cocos2d::Ref* pSender);
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h"
#include <CCNode.h>
#include <ccTypes.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();
Point origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 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(Point(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(Point::ZERO);
this->addChild(menu, 1);
/////////////////////////////
// 3. add your codes below...
auto menuitem_1 = MenuItemFont::create("Play", CC_CALLBACK_1(HelloWorld::Play, this));
auto menuitem_2 = MenuItemFont::create("Highscores", CC_CALLBACK_1(HelloWorld::Highscores, this));
auto menuitem_3 = MenuItemFont::create("Help", CC_CALLBACK_1(HelloWorld::Help, this));
auto menuitem_4 = MenuItemFont::create("Exit", CC_CALLBACK_1(HelloWorld::Exit, this));
menuitem_1->setColor(ccc3(160, 32, 240)); menuitem_1->setFontSize(90);
menuitem_2->setColor(ccc3(255, 0, 0));
menuitem_3->setColor(ccc3(0, 0, 255));
menuitem_4->setColor(ccc3(84, 84, 84));
menuitem_1->setPosition(Point(visibleSize.width / 4, (visibleSize.height / 4) * 4));
menuitem_2->setPosition(Point((visibleSize.width / 4), (visibleSize.height / 4) * 3));
menuitem_3->setPosition(Point((visibleSize.width / 4), (visibleSize.height / 4) * 2));
menuitem_4->setPosition(Point((visibleSize.width / 4), (visibleSize.height / 4) * 1));
menu = Menu::create(menuitem_1,menuitem_2, menuitem_3, menuitem_4, NULL);
menu->alignItemsVertically();
this->addChild(menu);
//add background for game
auto bgImage = Sprite::create("background.png");
bgImage->setPosition(Point(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y));
// add background image at z-position = -1, bottom of all
// the z-position value is relative to parent
this->addChild(bgImage, -1);
// calculate the scaling factor to fill the screen size
float rX = visibleSize.width / bgImage->getContentSize().width;
float rY = visibleSize.height / bgImage->getContentSize().height;
// set the scaling factor to the background image
bgImage->setScaleX(rX);
bgImage->setScaleY(rY);
return true;
}
void HelloWorld::Play(Ref *pSender)
{
CCLOG("Play");
/**
auto game = Game::createScene();
Director::getInstance()->pushScene(game);*/
}
void HelloWorld::Highscores(Ref *pSender)
{
CCLOG("BallDemo");
}
void HelloWorld::Help(Ref *pSender)
{
CCLOG("AninDemo");
}
void HelloWorld::Exit(Ref *pSender)
{
CCLOG("Play");
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}
==============================================================================
No comments:
Post a Comment