#include "HelloWorldScene.h"
using namespace cocos2d;
enum
{
kTagSprite = 1,
};
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
m_pSprite = CCSprite::create("banana.png");
// 把精灵放在正中间。
CCSize visibleSize = CCDirector::sharedDirector()->getWinSize();
m_pSprite->setPosition(ccp(visibleSize.width/2 , visibleSize.height/2 ));
// 将精灵放在场景中,设Z顺序为1
this->addChild(m_pSprite, 1);
setTouchEnabled(true);
//
// super init first
//
CC_BREAK_IF(! CCLayer::init());
//
// add your codes below...
//
// 1. Add a menu item with "X" image, which is clicked to quit the program.
// Create a "close" menu item with close icon, it's an auto release object.
CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
"CloseNormal.png",
"CloseSelected.png",
this,
menu_selector(HelloWorld::menuCloseCallback));
CC_BREAK_IF(! pCloseItem);
// Place the menu item bottom-right conner.
pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));
// Create a menu with the "close" menu item, it's an auto release object.
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
CC_BREAK_IF(! pMenu);
// Add the menu to HelloWorld layer as a child layer.
this->addChild(pMenu, 1);
// 2. Add a label shows "Hello World".
// Create a label and initialize with string "Hello World".
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
CC_BREAK_IF(! pLabel);
// Get window size and place the label upper.
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 50));
// Add the label to HelloWorld layer as a child layer.
this->addChild(pLabel, 1);
// 3. Add add a splash screen, show the cocos2d splash image.
CCSprite* pSprite = CCSprite::create("HelloWorld.png");
CC_BREAK_IF(! pSprite);
// Place the sprite on the center of the screen
pSprite->setPosition(ccp(size.width/2, size.height/2));
// Add the sprite to HelloWorld layer as a child layer.
this->addChild(pSprite, 0);
runChangeScore( );
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
void HelloWorld::runChangeScore( ){
CCLabelAtlas* pLabel = CCLabelAtlas::create("11", "fonts/tuffy_bold_italic-charmap.plist");
int temScore=atoi(pLabel->getString());
mScore=0;
int addScore=mScore + temScore;
char temStr[20]={};
if(abs(addScore)>10)
{
temScore+=addScore/10;
sprintf(temStr,"%06d",temScore);
pLabel->setString(temStr);
}
else if(abs(addScore)>2&&abs(addScore)<10)
{
temScore+=addScore/abs(addScore);
sprintf(temStr,"%06d",temScore);
pLabel->setString(temStr);
}
else
{
sprintf(temStr,"%06d",mScore);
pLabel->setString(temStr);
}
CCSize size = CCDirector::sharedDirector()->getWinSize();
pLabel->setPosition(ccp(size.width / 2, size.height - 50));
this->addChild(pLabel, 1);
}
void HelloWorld::onEnter()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, false);
// HelloWorld::onEnter();
}
void HelloWorld::onExit()
{
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->removeDelegate(this);
//HelloWorld::onExit();
}
bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
{
CCPoint sLocalPos = convertToNodeSpace(touch->getLocation());
CCRect sRC = CCRect(getPositionX() - getContentSize().width * getAnchorPoint().x,
getPositionY() - getContentSize().height * getAnchorPoint().y,
getContentSize().width, getContentSize().height);
sRC.origin = CCPointZero;
bool isTouched = sRC.containsPoint(sLocalPos);
if(isTouched)
{
CCLog("点中了!! x:%d y:%d", (int)sLocalPos.x, (int)sLocalPos.y);
return true;
}
else
{
CCLog("没点中了!! x:%d y:%d", (int)sLocalPos.x, (int)sLocalPos.y);
return false;
}
}
void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event)
{
m_pSprite->setPosition(ccp(touch->getLocation().x,10));
}
void HelloWorld::ccTouchEnded(CCTouch* pTouch, CCEvent* event)
{
}
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "Box2D/Box2D.h"
#include "SimpleAudioEngine.h"
USING_NS_CC;
using namespace std;
class HelloWorld : public cocos2d::CCLayer
{
public:
//跟随触点移动的精灵
CCSprite* m_pSprite;
// virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *pEvent);
// virtual void registerWithTouchDispatcher(void);
virtual void onEnter();
virtual void onExit();
virtual bool ccTouchBegan(CCTouch* touch, CCEvent* event);
virtual void ccTouchMoved(CCTouch* touch, CCEvent* event);
virtual void ccTouchEnded(CCTouch* touch, CCEvent* event);
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
void HelloWorld::runChangeScore();
//CCLabelTTF* pLabel [CCLabelTTF labelWithString:@"the string" fontName:@"Marker Felt" fontSize:21];
int mScore;
// there's no 'id' in cpp, so we recommand to return the exactly class pointer
static cocos2d::CCScene* scene();
// a selector callback
void menuCloseCallback(CCObject* pSender);
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
#endif // __HELLOWORLD_SCENE_H__