很久没有用过ControlButton这个空间了,不过进过引擎这么大幅度的改变,这个控件尽然还是
和2.23时大同小异,有点惊讶。
我觉得引擎带的ControlButton已经满足我们目前的使用要求,便没有对齐进行封装,所以,
直接在Scene中添加:
attackBtn = ControlButton::create("ATTACK", "fonts/Marker Felt.ttf", 30);
attackBtn->setPosition(Vec2(visibleSize.width-200, 100));
this->addChild(attackBtn, 7);
然后,我们可以选择对attackBtn添加监听,或者简单判isHighlighted属性即可。我就偷懒选择了后一种方法。
if(attackBtn->isHighlighted())
{
hero->AttackAnimation("attack1_animation.plist","attack1_animation.png","attack_",6,rocker->rocketRun);
log("-------------------_____________");
}
我是直接在HelloWorldScene中使用的,最后还是专门弄一个Scene出来的好
HelloWorld.h
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
#include "HRocker.h"
#include "Hero.h"
#include "Map.h"
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp, so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
HRocker* rocker;
Hero* hero;
virtual void update(float delta);
ControlButton* attackBtn;
private:
IMap* mymap;//地图
};
#endif // __HELLOWORLD_SCENE_H__
HelloWorld.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto 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 ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getVisibleSize();
Vec2 origin = Director::getInstance()->getVisibleOrigin();
attackBtn = ControlButton::create("ATTACK", "fonts/Marker Felt.ttf", 30);
attackBtn->setPosition(Vec2(visibleSize.width-200, 100));
this->addChild(attackBtn, 7);
//添加摇杆
rocker = HRocker::createHRocker("Direction_bt.png","Direction_bc.png",Point(150,90));//其中第一张图片为摇杆的按钮,第二张为背景
this->addChild(rocker,2);
rocker->startRocker(true);
//添加赵云精灵
hero=Hero::create();
hero->InitHeroSprite("zhoayun.png");
hero->setPosition(Vec2(200,200));
this->addChild(hero,1);
hero->setScale(3.0f);
//hero->SetAnimation("run_animation.plist", "run_animation.png", "run_", 8, false);//8表示plist中的图片数目,false表示脸朝右
hero->SetAnimation("attack1_animation.plist", "attack1_animation.png", "attack_", 6, false);
//启动updata事件
this->scheduleUpdate();
return true;
}
void HelloWorld::update(float delta)
{
switch(rocker->rocketDirection)
{
case 1:
hero->SetAnimation("run_animation.plist","run_animation.png",8,rocker->rocketRun);
hero->setPosition(Vec2(hero->getPosition().x+1,hero->getPosition().y));
break;
case 2:
hero->SetAnimation("run_animation.plist","run_animation.png",8,rocker->rocketRun);
hero->setPosition(Vec2(hero->getPosition().x, hero->getPosition().y+1));
break;
case 3:
hero->SetAnimation("run_animation.plist","run_animation.png",8,rocker->rocketRun);
hero->setPosition(Vec2(hero->getPosition().x-1,hero->getPosition().y));
break;
case 4:
hero->SetAnimation("run_animation.plist","run_animation.png",8,rocker->rocketRun);
hero->setPosition(Vec2(hero->getPosition().x,hero->getPosition().y-1));
break;
default:
hero->StopAnimation();
break;
}
if(attackBtn->isHighlighted())
{
hero->AttackAnimation("attack1_animation.plist","attack1_animation.png","attack_",6,rocker->rocketRun);
log("-------------------_____________");
}
}
最后在提醒一下,千万不要忘记将自己新建的资源文件夹添加到系统搜索目录。
操作方法,在AppDelegate::applicationDidFinishLaunching中添加
FileUtils::getInstance()->addSearchPath("attack");
FileUtils::getInstance()->addSearchPath("run");
attack,run是我在Resource下新建的资源文件夹
PS:多写博客,帮助自己,方便他人!