如何在Cocos2dx中按下按钮后获取随机类/页面/场景

时间:2014-11-07 08:37:23

标签: cocos2d-x cocos2d-x-3.0

我目前在Cocos2dx项目中遇到过一件事,由于网上信息有限,我决定在这里问一下。

我想创建一个显示随机类/页面/场景的按钮。

看看其中一个页面/ class / scene的代码:

//
//  Som0+1.h
//  Konijn telt
//
//  Created by - on 30/10/14.
//
//

#include "cocos2d.h"
#include "LevelHelper2API.h"
class Som01plus : public LHScene
{
public:
    static Som01plus* create();

    Som01plus();
    virtual ~Som01plus();
    virtual bool initWithContentOfFile(const std::string& plistLevelFile);

    void Exitbutton();
    void Nextbutton();
    void Backbutton();
    void handleNodesAtLocation(Point location);

    bool onTouchBegan(Touch* touch, Event* event);

};

//
//  Som0+2.cpp
//  Konijn telt
//
//  Created by - on 30/10/14.
//
//

#include "Som02plus.h"
#include "Som01plus.h"
#include "Start.h"
#include "Som03plus.h"

USING_NS_CC;

Som02plus* Som02plus::create()
{
    Som02plus *ret = new Som02plus();
    if (ret && ret->initWithContentOfFile("projectfile/Som0+2.lhplist"))
    {
        ret->autorelease();
        return ret;
    }
    else
    {
        CC_SAFE_DELETE(ret);
        return nullptr;
    }
}
Som02plus::Som02plus()
{
    /*INITIALIZE YOUR CONTENT HERE*/
    /*AT THIS POINT NOTHING IS LOADED*/
}

Som02plus::~Som02plus()
{
    //nothing to release
}

bool Som02plus::initWithContentOfFile(const std::string& plistLevelFile)
{
    bool retValue = LHScene::initWithContentOfFile(plistLevelFile);

    /*INITIALIZE YOUR CONTENT HERE*/
    /*AT THIS POINT EVERYTHING IS LOADED AND YOU CAN ACCESS YOUR OBJECTS*/

    return retValue;

}
void Som02plus::Exitbutton()

{
    CCLOG("Exitbutton");
    Director::getInstance()->replaceScene(Start::create());
}

void Som02plus::Nextbutton()
{
    CCLOG("Nextbutton");
    Director::getInstance()->replaceScene(Som03plus::create());
}

void Som02plus::Backbutton()
{
    CCLOG("Backbutton");
    Director::getInstance()->replaceScene(Som01plus::create());
}

void Som02plus::handleNodesAtLocation(Point location)
{
    {
        LHSprite* button = (LHSprite*)this->getChildNodeWithName("exit");
        if(button && button->getBoundingBox().containsPoint(location))

        {
            this->Exitbutton();
        }

    }
    LHSprite* button = (LHSprite*)this->getChildNodeWithName("verder");
    if(button && button->getBoundingBox().containsPoint(location))
    {

        this->Nextbutton();

    }

    {
        LHSprite* button = (LHSprite*)this->getChildNodeWithName("terug");
        if(button && button->getBoundingBox().containsPoint(location))
        {
            this->Backbutton();

        }
    }}

bool Som02plus::onTouchBegan(Touch* touch, Event* event)
{
    Point location = touch->getLocation();

    this->handleNodesAtLocation(location);

    //dont forget to call super
    return LHScene::onTouchBegan(touch, event);
}

每个'场景'都有退出,后退和前进按钮..就像我说我要添加一个随机按钮。 请给我一些帮助。 (我的编码还不是很好,所以解决方案可能很简单)谢谢!

0 个答案:

没有答案