#include "BoardBg.h"
#define PEN_Tag_1 111
#define PEN_Tag_2 222
#define PEN_Tag_3 333
bool BoardBg::_isTouch1 = true;
bool BoardBg::_isTouch2 = true;
bool BoardBg::_isTouch3 = true;
CCScene * BoardBg::scene()
{
CCScene * s = CCScene::create();
CCLayer * layer = BoardBg::create();
s->addChild(layer);
return s;
}
bool BoardBg::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
//点击后pen移动
_PenMoveX = 5;
_PenMoveY = 10;
addBoardBg();//背景;
addPenSpr();//添加pen;
_drawPen = drawPaint::create();
addChild(_drawPen,100);
bRet = true;
} while (0);
return bRet;
}
void BoardBg::addBoardBg()//添加背景;
{
const char * mainBgName = "mainBg5.png";
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
_boradBg = CCSprite::create(mainBgName);
_boradBg->setAnchorPoint(ccp(0,0));
_boradBg->setScale(winSize.width / _boradBg->getContentSize().width);//屏幕适配;
addChild(_boradBg);
return;
}
void BoardBg::addPenSpr()//添加pen;
{
const char * penName = "pen.png";
_pen1 = CCSprite::create(penName);
_pen2 = CCSprite::create(penName);
_pen3 = CCSprite::create(penName);
_penSize = _pen1->getContentSize();
//_pen1->setPosition(ccp(_penSize.width / 2,_penSize.height / 2));
//_pen2->setPosition(ccp(_penSize.width / 2 * 2 + 10,_penSize.height / 2));
//_pen3->setPosition(ccp(_penSize.width / 2 * 3 + 10,_penSize.height / 2));
//按钮
pen1Item = CCMenuItemSprite::create(_pen1,_pen1,this,menu_selector(BoardBg::PenCallBack1));
pen1Item->setPosition(ccp(_penSize.width/2,_penSize.height/2));
pen1Item->setTag(PEN_Tag_1);
pen2Item = CCMenuItemSprite::create(_pen2,_pen2,menu_selector(BoardBg::PenCallBack2));
pen2Item->setPosition(ccp((_penSize.width / 2) * 2 + 20,_penSize.height / 2));
pen2Item->setTag(PEN_Tag_2);
pen3Item = CCMenuItemSprite::create(_pen3,_pen3,menu_selector(BoardBg::PenCallBack3));
pen3Item->setPosition(ccp((_penSize.width / 2) * 3 + 40,_penSize.height / 2));
pen3Item->setTag(PEN_Tag_3);
//menu
CCMenu * menu = CCMenu::create(pen1Item,pen2Item,pen3Item,NULL);
menu->setAnchorPoint(ccp(0,0));
menu->setPosition(ccp(20,0));
addChild(menu);
}
void BoardBg::RestorePen1()
{
pen1Item->setPosition(ccp(_penSize.width / 2,_penSize.height / 2));
_isTouch1 = true;
}
void BoardBg::RestorePen2()
{
pen2Item->setPosition(ccp((_penSize.width / 2) * 2 + 20,_penSize.height / 2));
_isTouch2 = true;
}
void BoardBg::RestorePen3()
{
pen3Item->setPosition(ccp((_penSize.width / 2) * 3 + 40,_penSize.height / 2));
_isTouch3 = true;
}
//回调,getTag获取
void BoardBg::PenCallBack1(CCObject* sender)
{
CCSprite * spr = (CCSprite*)sender;
spr->getChildByTag(PEN_Tag_1);
if (_isTouch1)
{
_drawPen->_isTouchDraw = true;
spr->setPosition(ccp(spr->getPositionX() - _PenMoveX,spr->getPositionY() + _PenMoveY));
_drawPen->_penBold = 5;
RestorePen2();
RestorePen3();
}
if (!_isTouch1)
{
_drawPen->_isTouchDraw = false;
spr->setPosition(ccp(spr->getPositionX() + _PenMoveX,spr->getPositionY() - _PenMoveY));
}
_isTouch1 = !_isTouch1;
}
void BoardBg::PenCallBack2(CCObject* sender)
{
CCSprite * spr = (CCSprite*)sender;
spr->getChildByTag(PEN_Tag_2);
if (_isTouch2)
{
_drawPen->_isTouchDraw = true;
spr->setPosition(ccp(spr->getPositionX() - _PenMoveX,spr->getPositionY() + _PenMoveY));
_drawPen->_penBold = 7;
RestorePen1();
RestorePen3();
}
if (!_isTouch2)
{
_drawPen->_isTouchDraw = false;
spr->setPosition(ccp(spr->getPositionX() + _PenMoveX,spr->getPositionY() - _PenMoveY));
}
_isTouch2 = !_isTouch2;
}
void BoardBg::PenCallBack3(CCObject* sender)
{
CCSprite * spr = (CCSprite*)sender;
spr->getChildByTag(PEN_Tag_3);
if (_isTouch3)
{
_drawPen->_isTouchDraw = true;
spr->setPosition(ccp(spr->getPositionX() - _PenMoveX,spr->getPositionY() + _PenMoveY));
_drawPen->_penBold = 9;
RestorePen1();
RestorePen2();
}
else
{
_drawPen->_isTouchDraw = false;
spr->setPosition(ccp(spr->getPositionX() + _PenMoveX,spr->getPositionY() - _PenMoveY));
}
_isTouch3 = !_isTouch3;
}
原文链接:https://www.f2er.com/cocos2dx/345156.html