斗地主-源码集锦

1.   软妹斗地主

http://www.hzg360.com/upload/7F6E7EA1360BFC09449628A0E267FE11_20190504125018.apk

 

 

 

#pragma once
#include "MySprite.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayer
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();
    
    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static CCScene* scene();

    
    
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
    
    virtual void onEnter();
    virtual void onExit();
    virtual void onEnterTransitionDidFinish();
	void onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event);
    
private:
	void pop();
	// a selector callback
    void popButtonCallback(Node *pNode);
    void startGame(CCObject* sender);
    void settingGame(CCObject* sender);
    void exitGame(CCObject* sender);
private:

	
    
};

 

 

 

#include "HelloWorldScene.h"
#include "GameScene.h"
#include "Tool.h"
#include "PopupLayer.h"
USING_NS_CC;
CCScene* HelloWorld::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	HelloWorld *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 ( !CCLayer::init() )
	{
		return false;
	}
	bool isRet = false;
	do 
	{
		if (!CCUserDefault::sharedUserDefault()->getBoolForKey("GameSettingXML")) {
			CCUserDefault::sharedUserDefault()->setBoolForKey("GameSettingXML", true);
			CCUserDefault::sharedUserDefault()->setIntegerForKey("PlayerScore", playerScore);
			CCUserDefault::sharedUserDefault()->setIntegerForKey("PlayerLevel", playerLevel);
			CCUserDefault::sharedUserDefault()->flush();
		}
		else
		{
			playerScore=CCUserDefault::sharedUserDefault()->getIntegerForKey("PlayerScore");
			playerLevel=CCUserDefault::sharedUserDefault()->getIntegerForKey("PlayerLevel");
		}

		CCSize size = CCDirector::sharedDirector()->getVisibleSize();

		MySprite* pSprite = MySprite::create("bk.png");	
		addChild(pSprite);
		pSprite->setAnchorPoint(ccp(0,0));

		pSprite = MySprite::create("toolbar_bg.png");	
		addChild(pSprite);	
		Size sizeS=pSprite->getContentSize();
		pSprite->setAnchorPoint(ccp(0.5,0));
		pSprite->setPosition(ccp(size.width-sizeS.width/2,size.height-20));
		pSprite->setRotation(180.0);	
		pSprite->setScaleY((size.height/2.0-20)/sizeS.height);

		pSprite = MySprite::create("toolbar_bg.png");	
		addChild(pSprite);
		pSprite->setAnchorPoint(ccp(0.5,0));
		pSprite->setPosition(ccp(size.width-sizeS.width/2,20));
		pSprite->setScaleY((size.height/2.0-20)/sizeS.height);

		pSprite = MySprite::create("menu_left_logo.png");
		addChild(pSprite);
		Size sizeL=pSprite->getContentSize();
		pSprite->setPosition(ccp(sizeL.width/2,size.height/2));

		/*
		auto spriteSelected = MySprite::create("game_start_btn_prs.png");
		Size sizeBtn=spriteSelected->getContentSize();
		auto spriteNormal = MySprite::create("game_start_btn.png");    

		auto startLabel = MenuItemSprite::create(spriteNormal, spriteSelected, nullptr, CC_CALLBACK_1(HelloWorld::startGame, this));
		startLabel->setPosition(ccp(size.width-sizeS.width/2, 3.5*size.height/5));

		spriteSelected = MySprite::create("game_set_btn_prs.png");
		spriteNormal = MySprite::create("game_set_btn.png");    

		auto settingLabel = MenuItemSprite::create(spriteNormal, spriteSelected, nullptr, CC_CALLBACK_1(HelloWorld::settingGame, this));
		settingLabel->setPosition(ccp(size.width-sizeS.width/2, 2.5*size.height/5)); */

		char path[100]={0};
		sprintf(path, "level_logo_%d.png", playerLevel);
		auto headPlayer=MySprite::create(path);
		headPlayer->setPosition(ccp(size.width-sizeS.width/2, 3.8*size.height/5));
		this->addChild(headPlayer,size.height,"HeadPlayer");

		auto spriteSelected = MySprite::create("game_start_btn_prs.png");
		Size sizeBtn=spriteSelected->getContentSize();
		auto spriteNormal = MySprite::create("game_start_btn.png");    

		auto startLabel = MenuItemSprite::create(spriteNormal, spriteSelected, nullptr, CC_CALLBACK_1(HelloWorld::startGame, this));
		startLabel->setPosition(ccp(size.width-sizeS.width/2, 2.5*size.height/5));

		spriteSelected = MySprite::create("game_exit_btn_prs.png");
		spriteNormal = MySprite::create("game_exit_btn.png");   

		auto exitLabel = MenuItemSprite::create(spriteNormal, spriteSelected, nullptr, CC_CALLBACK_1(HelloWorld::exitGame, this));
		exitLabel->setPosition(ccp(size.width-sizeS.width/2, 1.5*size.height/5));

		Menu* m_menu = Menu::create();
		m_menu->addChild(startLabel,1,1);
		//m_menu->addChild(settingLabel,1,2);
		m_menu->addChild(exitLabel,1,3);
		this->addChild(m_menu);
		m_menu->setPosition(ccp(0,0));

		setTouchEnabled(true);

		auto listener = EventListenerKeyboard::create();
		listener->onKeyReleased = CC_CALLBACK_2(HelloWorld::onKeyReleased, this);
		_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
		isRet = true;
	} while (0);
	return isRet;
}

void HelloWorld::startGame(CCObject* sender)
{
	static bool first=true;
	if(first){
		CCTransitionSlideInL * transition=CCTransitionSlideInR::create(0.2f,GameScene::scene());
		CCDirector::sharedDirector()->pushScene(transition);
		first=false;
	}else
		CCDirector::sharedDirector()->popScene();
}
void HelloWorld::settingGame(CCObject* sender)
{
}
void HelloWorld::exitGame(CCObject* sender)
{
	CCUserDefault::sharedUserDefault()->setBoolForKey("GameSettingXML", true);
	CCUserDefault::sharedUserDefault()->setIntegerForKey("PlayerScore", playerScore);
	CCUserDefault::sharedUserDefault()->setIntegerForKey("PlayerLevel", playerLevel);
	CCUserDefault::sharedUserDefault()->flush();
	pop();
}
void HelloWorld:: onEnter()
{
	CCLayer::onEnter();
}
void HelloWorld:: onExit()
{
	CCUserDefault::sharedUserDefault()->setBoolForKey("GameSettingXML", true);
	CCUserDefault::sharedUserDefault()->setIntegerForKey("PlayerScore", playerScore);
	CCUserDefault::sharedUserDefault()->setIntegerForKey("PlayerLevel", playerLevel);
	CCUserDefault::sharedUserDefault()->flush();

	CCLayer::onExit();
}
void HelloWorld:: onEnterTransitionDidFinish()
{
	CCLayer::onEnterTransitionDidFinish();
}
void HelloWorld::popButtonCallback(Node *pNode){
	if(pNode->getTag() == 0){
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
		market();	
#endif
	}
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS||CC_TARGET_PLATFORM ==CC_PLATFORM_WIN32)
	exit(0);
#endif
}
void HelloWorld::pop(){
	static bool first=true;
	if(first){
		Dictionary* strings = Dictionary::createWithContentsOfFile("strings.xml");
		const char * str;
		Size size=CCDirector::sharedDirector()->getVisibleSize();
		PopupLayer* pl = PopupLayer::create("background.png",Size(400,200));
		str=((String *)strings->objectForKey("exitgame"))->getCString();
		pl->setTitle(str);
		str=((String *)strings->objectForKey("sureexit"))->getCString();
		pl->setContentText(str, 20, 60, 120);
		pl->setCallbackFunc(this, callfuncN_selector(HelloWorld::popButtonCallback));//设置按钮回调
		str=((String *)strings->objectForKey("pingjia"))->getCString();
		pl->addButton("btn_blue.png", "btn_green.png", str, 0);
		str=((String *)strings->objectForKey("buping"))->getCString();
		pl->addButton("btn_blue.png", "btn_green.png", str, 1);
		this->addChild(pl,size.height);
		first=false;
	}
}
void HelloWorld::onKeyReleased(EventKeyboard::KeyCode keyCode, Event* event) {
	switch(keyCode){ 
		//监听返回键       
	case EventKeyboard::KeyCode::KEY_ESCAPE:
		pop();
		break;         
		//监听menu键       
	case EventKeyboard::KeyCode::KEY_MENU:         
		break;     
	}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

展开阅读全文

没有更多推荐了,返回首页