How to create a fortune wheel game with cocos2d for cross platform.

Hi 

This is the game looks like fortune wheel. Here there is a pointer to indicate score. The score will be displayed at middle of the screen.

AppDelegate.h

#ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_

#include “cocos2d.h”

/**
@brief The cocos2d Application.

The reason for implement as private inheritance is to hide some interface call by CCDirector.
*/
class AppDelegate : private cocos2d::CCApplication
{
public:
AppDelegate();
virtual ~AppDelegate();

/**
@brief Implement CCDirector and CCScene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();

/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();

/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};

#endif // _APP_DELEGATE_H_

 AppDelegate.cpp

#include “AppDelegate.h”
#include “HelloWorldScene.h”

USING_NS_CC;

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();

pDirector->setOpenGLView(pEGLView);

// turn on display FPS
pDirector->setDisplayStats(true);

// set FPS. the default value is 1.0/60 if you don’t call this
pDirector->setAnimationInterval(1.0 / 60);

// create a scene. it’s an autorelease object
CCScene *pScene = HelloWorld::scene();

// run
pDirector->runWithScene(pScene);

return true;
}

// This function will be called when the app is inactive. When comes a phone call,it’s be invoked too
void AppDelegate::applicationDidEnterBackground() {
CCDirector::sharedDirector()->stopAnimation();

// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}

// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
CCDirector::sharedDirector()->startAnimation();

// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}

 

HelloWorldScene.h

#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__

#include “cocos2d.h”
using namespace cocos2d;

class HelloWorld : public cocos2d::CCLayer
{

private:
CCSize visibleSize;
CCPoint origin;
CCSprite* pSprite;
CCLabelTTF* sLabel;
//float anglediff;
//float deltang;
//float newAng;
float curPosition=0;
public:
// Here’s a difference. Method ‘init’ in cocos2d-x returns bool, instead of returning ‘id’ in cocos2d-iphone
virtual bool init();

// there’s no ‘id’ in cpp, so we recommend returning the class instance pointer
static cocos2d::CCScene* scene();

// a selector callback
void menuCloseCallback(CCObject* pSender);

//to rotate wheel
virtual void ccTouchesBegan(CCSet* pTouches, CCEvent* event);
// virtual void ccTouchesMoved(CCSet* pTouches, CCEvent* event);
// virtual void ccTouchesEnded(CCSet* pTouches, CCEvent* event);

void rotate(CCSprite *sprite, float angle);

//display a small message

void reset(float ang);

// implement the “static node()” method manually
CREATE_FUNC(HelloWorld);
};

#endif // __HELLOWORLD_SCENE_H__

HelloWorldScene.cpp

#include “HelloWorldScene.h”
#include “math.h”
#include “iostream”
using namespace std;
using namespace cocos2d;

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;
}

visibleSize = CCDirector::sharedDirector()->getVisibleSize();
origin = CCDirector::sharedDirector()->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
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
“CloseNormal.png”,
“CloseSelected.png”,
this,
menu_selector(HelloWorld::menuCloseCallback));

pCloseItem->setPosition(ccp(origin.x + visibleSize.width – pCloseItem->getContentSize().width/2 ,
origin.y + pCloseItem->getContentSize().height/2));

// create menu, it’s an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);

/////////////////////////////
// 3. add your codes below…

// add a label shows “Hello World”
// create and initialize a label

CCLabelTTF* pLabel = CCLabelTTF::create(“Touch Screen to Rotate Wheel”, “Arial”, 14);

// position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height – pLabel->getContentSize().height*22));

// add the label as a child to this layer
this->addChild(pLabel, 0);

//add “Arrow” splash screen
CCSprite* aSprite = CCSprite::create(“arrow.png”);
//position the sprite on the center of the screen
aSprite->setPosition(ccp(origin.x+visibleSize.width/2, origin.y +visibleSize.height – aSprite->getContentSize().height*0.5));

this->addChild(aSprite,0);
// add “HelloWorld” splash screen”
pSprite = CCSprite::create(“background.png”);

// position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
pSprite->setAnchorPoint(ccp(0.5, 0.5));

// add the sprite as a child to this layer
this->addChild(pSprite, 0);

sLabel = CCLabelTTF::create(“”, “Arial”, 20);

// position the label on the center of the screen – sLabel->getContentSize().height
sLabel->setPosition(ccp(origin.x + visibleSize.width/2,
origin.y + visibleSize.height/2));

// add the label as a child to this layer

this->addChild(sLabel, 1);
// sLabel->setVisible(false);

//listen for touches
this->setTouchEnabled(true);

// CCLog(“this is inside init method”);
// display();

return true;
}

void HelloWorld::ccTouchesBegan(CCSet* pTouches, CCEvent* event) {
CCTouch *touch = (CCTouch *)pTouches->anyObject();
float ang=(double)rand()/(RAND_MAX)*2500;
// float angle=255;
CCActionInterval* rotate = CCRotateBy::create(2.0f ,ang);
// CCLog(“angle is %f”,ang);
// CCAction* repeatRotate = CCRepeatForever::create( rotate );
pSprite->runAction(rotate);
curPosition=curPosition+ang;
while(curPosition >= 360){
curPosition =curPosition -360;
}
CCLog(“curPosition is %f”,curPosition);
// ang =round(ang);
if( curPosition <=9){
sLabel->setString(“Score is LOSE”);
}else if( curPosition > 9 && curPosition <20){
sLabel->setString(“Score is 300”);
}else if(curPosition >=20 && curPosition <=35.5){
sLabel->setString(“Score is 400”);
}else if(curPosition >35.5 && curPosition <=50){
sLabel->setString(“Score is 600”);
}else if(curPosition >50 && curPosition <=66.7){
sLabel->setString(“Score is BANK”);
}else if(curPosition >66.7 && curPosition <=80){
sLabel->setString(“Score is 900”);
}else if(curPosition >80 && curPosition <=96){
sLabel->setString(“Score is 000”);
}else if(curPosition >96 && curPosition <=110 ){
sLabel->setString(“Score is 500”);
}else if(curPosition >110 && curPosition <=122){
sLabel->setString(“Score is 900”);
}else if(curPosition >122 && curPosition <=140){
sLabel->setString(“Score is 300”);
}else if(curPosition >140 && curPosition <=157){
sLabel->setString(“Score is 400”);
}else if(curPosition >157 && curPosition <=170){
sLabel->setString(“Score is 550”);
}else if(curPosition >170 && curPosition <=183){
sLabel->setString(“Score is 800”);
}else if(curPosition >183 && curPosition <=200.67){
sLabel->setString(“Score is 500”);
}else if(curPosition >200.67 && curPosition <=216.75){
sLabel->setString(“Score is 300”);
}else if(curPosition >216.75 && curPosition <=234){
sLabel->setString(“Score is 600”);
}else if(curPosition >234 && curPosition <=243){
sLabel->setString(“Score is 300”);
}else if(curPosition >243 && curPosition <=262){
sLabel->setString(“Score is 5000”);
}else if(curPosition >262 && curPosition <=275 ){
sLabel->setString(“Score is 600”);
}else if(curPosition >275 && curPosition <=291.5){
sLabel->setString(“Score is 300”);
}else if(curPosition >291.5 && curPosition <=306.7){
sLabel->setString(“Score is 700”);
}else if(curPosition >306.7 && curPosition <=320.5){
sLabel->setString(“Score is 450”);
}else if(curPosition >320.5 && curPosition <=336.2){
sLabel->setString(“Score is 350”);
}else if(curPosition >336.2 && curPosition <=350){
sLabel->setString(“Score is 800”);
}else{
sLabel->setString(“Score is LOSE”);

}

}

void HelloWorld::reset(float ang){
CCActionInterval* rotate = CCRotateBy::create(0.5f ,-ang);
pSprite->runAction(rotate);
}

void HelloWorld::menuCloseCallback(CCObject* pSender)

{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
CCMessageBox(“You pressed the close button. Windows Store Apps do not implement a close button.”,”Alert”);
#else
CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
#endif
}

 

we need these two diagrams to create this game.

background.png

Image

 

arrow.png

Image

Leave a comment