上篇文章我们有了坦克,但是没有手柄,无法控制坦克。
1.这篇我们编写虚拟手柄来控制坦克。头文件大致内容如下:
- #define RES_PADDLE_LEFT "paddle/left.png"
- #define RES_PADDLE_LEFT_PRESS "paddle/left_press.png"
- #define RES_PADDLE_RIGHT "paddle/right.png"
- #define RES_PADDLE_RIGHT_PRESS "paddle/right_press.png"
- #define RES_PADDLE_UP "paddle/top.png"
- #define RES_PADDLE_UP_PRESS "paddle/top_press.png"
- #define RES_PADDLE_DOWN "paddle/buttom.png"
- #define RES_PADDLE_DOWN_PRESS "paddle/buttom_press.png"
- #define RES_PADDLE_FIRE "paddle/fire.png"
- #define RES_PADDLE_FIRE_PRESS "paddle/fire_press.png"
- class Panel : public CCLayer
- {
- public:
- virtual bool init();
- CREATE_FUNC(Panel);
- protected:
- Paddle* m_pPaddleUp;
- Paddle* m_pPaddleDown;
- Paddle* m_pPaddleLeft;
- Paddle* m_pPaddleRight;
- Paddle* m_pPaddleFire;
- };
上面定义了上下左右按钮的图片,还记得我们写关卡选择按钮的Paddle吧,
下面定义了五个Paddle分别对应上下左右开火按钮。
2.我们在初始化函数init中,加载按钮图片,然后设置按钮图片对应的坐标。
代码如下:
- bool Panel::init()
- {
- CCLayer::init();
- //加载手柄按钮所需的图片
- CCTexture2D* paddleTexture = CCTextureCache::sharedTextureCache()->addImage(RES_PADDLE_UP);
- m_pPaddleUp = Paddle::paddleWithTexture(paddleTexture);
- paddleTexture = CCTextureCache::sharedTextureCache()->addImage(RES_PADDLE_DOWN);
- m_pPaddleDown = Paddle::paddleWithTexture(paddleTexture);
- paddleTexture = CCTextureCache::sharedTextureCache()->addImage(RES_PADDLE_LEFT);
- m_pPaddleLeft = Paddle::paddleWithTexture(paddleTexture);
- paddleTexture = CCTextureCache::sharedTextureCache()->addImage(RES_PADDLE_RIGHT);
- m_pPaddleRight = Paddle::paddleWithTexture(paddleTexture);
- paddleTexture = CCTextureCache::sharedTextureCache()->addImage(RES_PADDLE_FIRE);
- m_pPaddleFire = Paddle::paddleWithTexture(paddleTexture);
- CCSize size = getContentSize();
- //设置手柄上下左右按钮以及开火按钮的位置
- int localX[5] = { size.width*4.5f / 26, size.width*4.5f / 26, size.width*2.3f / 26,
- size.width*6.7f / 26, size.width*22.0f / 26 };
- int localY[5] = { size.height*6.7 / 26, size.height*2.3 / 26, size.height*4.5f / 26,
- size.height*4.5f / 26, size.height*5.0 / 26 };
- Paddle* paddle[] = { m_pPaddleUp, m_pPaddleDown, m_pPaddleLeft, m_pPaddleRight, m_pPaddleFire };
- for (int i = 0; i<sizeof(paddle) / sizeof(paddle[0]); ++i)
- {
- addChild(paddle[i]);
- paddle[i]->setPosition(ccp(localX[i], localY[i]));
- CCSize szPaddle = paddle[i]->getContentSize();
- paddle[i]->setScale(size.height * 4.5f / 26 / szPaddle.height);
- }
- return true;
- }
3.最后再写一个函数获取按钮的状态,这样就知道当前按下了那个按钮:
在之前我们定义一组命令,按到那个按钮就返回什么命令:
- enum enumOrder
- {
- cmdNothing,
- cmdGoUP,
- cmdGoDown,
- cmdGoLeft,
- cmdGoRight,
- cmdFire
- };
4.再添加一个enumOrder getOrder();函数,实现如下:
- enumOrder Panel::getOrder()
- {
- enumOrder order = cmdNothing;
- if (m_pPaddleUp->getstate() == kPaddleStateGrabbed)
- order = cmdGoUP;
- else if (m_pPaddleDown->getstate() == kPaddleStateGrabbed)
- order = cmdGoDown;
- else if (m_pPaddleLeft->getstate() == kPaddleStateGrabbed)
- order = cmdGoLeft;
- else if (m_pPaddleRight->getstate() == kPaddleStateGrabbed)
- order = cmdGoRight;
- else if (m_pPaddleFire->getstate() == kPaddleStateGrabbed)
- order = cmdFire;
- return order;
- }
5,在ChoiceScene场景中添加成员变量Panel* mLayerPanel;,
然后在 ChoiceScene的init函数下面加上两行代码即可显示虚拟手柄:
- //添加虚拟手柄的显示
- mLayerPanel = Panel::create();
- addChild(mLayerPanel, 3);
显示效果图如下:
完整代码下载地址: