2013-11-03 15 views
9

Vì vậy, tôi đang cố gắng tạo một ứng dụng đơn giản bằng cách sử dụng phiên bản mới nhất của cocos2d-x và vì một lý do nào đó không thể kết nối với tôi. Dưới đây là lớp học của tôi:Không thể liên lạc để hoạt động trong ứng dụng cocos2d-x đa nền

class GameLayer : public cocos2d::Layer 
{ 
public: 
    static cocos2d::Layer* createLayer(); 
    void update(float dt); 
    virtual bool init(); 
    CREATE_FUNC(GameLayer); 
private: 
    bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); 
    void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); 
    void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event); 
}; 

cocos2d::Layer* GameLayer::createLayer() 
{ 
    GameLayer *layer = GameLayer::create(); 

    return layer; 
} 

bool GameLayer::init() 
{ 
    if (!cocos2d::Layer::init()) 
    { 
     return false; 
    } 

    this->schedule(schedule_selector(GameLayer::update)); 
    this->setTouchEnabled(true); 

    return true; 
} 

void GameLayer::update(float dt) 
{ 

} 

bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) 
{ 
    cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y); 
    return true; 
} 
void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event) 
{ 

} 
void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event) 
{ 

} 

tôi nhận thấy khi tôi gọi vào setTouchEnabled cuộc gọi mà một lá cờ nội bộ gọi _running được thiết lập để sai vì vậy nó không thực sự đăng ký các sự kiện liên lạc của tôi. Tuy nhiên tôi dường như không thể hiểu tại sao lại như thế. Tôi có gọi những thứ không chính xác hoặc sai thứ tự không?

Trả lời

19

Hiện tại, cocos2dx đang trải qua quá trình sửa chữa lớn thư viện và nhiều thứ đã thay đổi bao gồm đăng ký và tuyên truyền Chạm. Sau đây là cách nó hoạt động bây giờ:

GameLayer.h

class GameLayer : public cocos2d::Layer 
{ 
public: 
    static cocos2d::Layer* createLayer(); 
    void update(float dt); 
    virtual bool init(); 
    CREATE_FUNC(GameLayer); 

private: 
    virtual void onEnter(); 
    virtual void onExit(); 

    bool onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event); 
    void onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event); 
    void onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event); 
}; 

GameLayer.cpp

cocos2d::Layer* GameLayer::createLayer() 
{ 
    GameLayer *layer = GameLayer::create(); 

    return layer; 
} 

bool GameLayer::init() 
{ 
    if (!cocos2d::Layer::init()) 
    { 
     return false; 
    } 

    this->schedule(schedule_selector(GameLayer::update)); 

    return true; 
} 

void GameLayer::onEnter() 
{ 
    Layer::onEnter(); 

    // Register Touch Event 
    auto dispatcher = Director::getInstance()->getEventDispatcher(); 
    auto listener = EventListenerTouchOneByOne::create(); 

    listener->onTouchBegan = CC_CALLBACK_2(GameLayer::onTouchBegan, this); 
    listener->onTouchMoved = CC_CALLBACK_2(GameLayer::onTouchMoved, this); 
    listener->onTouchEnded = CC_CALLBACK_2(GameLayer::onTouchEnded, this); 

    dispatcher->addEventListenerWithSceneGraphPriority(listener, this); 
} 

void GameLayer::onExit() 
{ 
    // You don't need to unregister listeners here as new API 
    // removes all linked listeners automatically in CCNode's destructor 
    // which is the base class for all cocos2d DRAWING classes 

    Layer::onExit(); 
} 

void GameLayer::update(float dt) 
{ 

} 

bool GameLayer::onTouchBegan(cocos2d::Touch* touch, cocos2d::Event* event) 
{ 
    cocos2d::log("You touched %f, %f", touch->getLocationInView().x, touch->getLocationInView().y); 
    return true; 
} 

void GameLayer::onTouchMoved(cocos2d::Touch* touch, cocos2d::Event* event) 
{ 

} 

void GameLayer::onTouchEnded(cocos2d::Touch* touch, cocos2d::Event* event) 
{ 

} 

Hy vọng nó sẽ giúp!

+0

Có vẻ như 'registerWithTouchDispatcher' không còn được dùng nữa, và' getTouchDispatcher() 'không còn tồn tại trên' Director' bất kỳ ý tưởng gì tương đương bây giờ? –

+0

Vâng, API đã được cập nhật lại. :) Vui lòng xem câu trả lời cập nhật của tôi. – nomann

+0

Cảm ơn sự giúp đỡ, tôi đang sử dụng phiên bản 3.0 đã được chỉ trên trang chính của họ không phải những gì đã được trên github (mà thay đổi tất cả mọi thứ là tốt). Vì vậy, phiên bản của tôi không có eventListener. Tôi sẽ cố gắng nâng cấp lên những gì họ có trong github, tôi sẽ giữ cho bạn cập nhật nếu tôi có thể làm cho nó hoạt động. –

Các vấn đề liên quan