Cocos2d-x中在一个CClayer上设置按钮控制另一个CClayer上的对象



Hi,推荐文件给你 "碰撞检测例子.zip" http://vdisk.weibo.com/s/J6Ld-


新建一个工程,之后再一个类HudLayer


HudLayer.h

#include "cocos2d.h"
USING_NS_CC;
class HudLayer:public CCLayer
{
public:
    CREATE_FUNC(HudLayer);
    bool init();
    
    //调用的方法
    void start();
};


HudLayer.cpp

bool HudLayer::init()
{
    bool pRet = false;
    do {
        CC_BREAK_IF(!CCLayer::init());
        //设置一个按钮
        CCMenuItemImage* start = CCMenuItemImage::create("Icon-72.png", "Icon-72.png", this, menu_selector(HudLayer::start));
        start->setPosition(ccp(100, 100));
        CCMenu* pMenu = CCMenu::create(start,NULL);
        this->addChild(pMenu);
        pRet = true;
    } while (0);
    return pRet;
}

void HudLayer::start()
{
    //发送一条消息调用相应的方法
    CCNotificationCenter::sharedNotificationCenter()->postNotification("change");
}


接下来,我们要对HelloWorldScene.h做如下修改

#include "HudLayer.h"
USING_NS_CC;
class HelloWorld : public cocos2d::CCLayerColor
{
public:
    // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer)
    virtual bool init();

    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    HelloWorld();
    CCArray* _arr;
    HudLayer* hud;
    void change();
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(HelloWorld);
};

 //注册一条消息,在HudLayer类中发送的消息
    CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(HelloWorld::change), "change", NULL);
    
    CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
    cache->addSpriteFramesWithFile("zzzz.plist");
    
    CCSpriteBatchNode* node = CCSpriteBatchNode::create("zzzz.pvr.ccz");
    this->addChild(node);
    
                                                            
    _arr = CCArray::create();
    _arr->retain();
    CCSprite* player = CCSprite::createWithSpriteFrameName("Player.png");
    player->setPosition(ccp(100, 100));
    node->addChild(player);
    _arr->addObject(player);
    hud = HudLayer::create();
    this->addChild(hud);
    
    return true;
}
void HelloWorld::change()
{
    CCSprite* player1 = (CCSprite*)_arr->objectAtIndex(0);
    CCMoveBy* moBy = CCMoveBy::create(1, ccp(0, 100));
    player1->runAction(moBy);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

杜甲同学

感谢打赏,我会继续努力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值