如何对从数组中提取并出现在屏幕上的sprite执行拖放操作?

时间:2014-02-11 12:21:43

标签: cocos2d-x

我想对这些图像执行拖放操作。

如何使用以下代码实现这一目标。

void storeLocation::changescene()
{

this->removeAllChildren();
//CCDirector::sharedDirector()->replaceScene(storeLocation::scene());

CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

//CCScene* scene=CCScene::create();
    storeLocation *layer = storeLocation::create();
    CCSprite *k=CCSprite::create("background.png");
    this->addChild(k,0);
    k->setPosition(ccp(visibleSize.width/2+ origin.x, visibleSize.height/2 + origin.y));
    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
                                    "CloseNormal.png",
                                    "CloseSelected.png",
                                    this,
                                    menu_selector(storeLocation::menuCloseCallback));

pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width ,
                            origin.y + pCloseItem->getContentSize().height/2));
pCloseItem->setScale(1.5);
// create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
pMenu->setPosition(CCPointZero);
this->addChild(pMenu, 1);

        this->addChild(pMenu, 1);
        this->setTouchEnabled(true);











int l=5;
int posx=0,posy=0;


int count=0,r,j=-1,i=0,flag=0;
int x=20;
int b[30],a[30];
while(count<=5)
{
    srand(time(0));

    r=rand()%x+1;
    flag=checktag(b,r,j);
    if(flag==1)
    {


    b[i]=r;
    i++;
    count++;
    j++;

    }

}

            int t;


CCObject* jt=NULL;

CCARRAY_FOREACH(images, jt)
    {
           // CCSize winSize = CCDirector::sharedDirector()->getWinSize();
            //float i=winSize.width;

        CCSprite *image = dynamic_cast<CCSprite*>(jt);
        t=image->getTag();
        for(i=0;i<l;i++)
        {
            if(t==b[i])
            {
                this->addChild(image);
        image->setPosition(ccp(100+posx,100));
        posx=posx+120;


            }}}

2 个答案:

答案 0 :(得分:1)

要在屏幕上将图像从一个点拖放到另一个点,您必须使用触摸委托方法

bool ccTouchBegan(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
void ccTouchMoved(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);
void ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent);

使用ccTouchBegan方法在用户触摸时检测图像,为此您可以将所有图像对象存储在一个数组中,并使用for循环检查触摸是否在任何图像的矩形中。

使用用户触摸移动图像在ccTouchMoved中触摸图像的更改位置(将触摸的图像对象保存在全局对象中)。

在ccTouchEnded方法中,您可以在删除图像时执行任何操作。

答案 1 :(得分:0)

捕获拖放事件的最简单方法是实现onTouchBegan和onTouchMoved以及onTouchEnded方法,如下所示:

auto listener = EventListenerTouchOneByOne::create();
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
listener->onTouchMoved = CC_CALLBACK_2(HelloWorld::onTouchMoved, this);
listener->onTouchEnded = CC_CALLBACK_2(HelloWorld::onTouchEnded, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, sprite);

bool HelloWorld::onTouchBegan(Touch* touch, cocos2d::Event* event){
    // this method is not needed but in order to implement the onTouchMoved you have to first implement onTouchBegan then the onTouchMoved
    return true;
}

void HelloWorld::onTouchMoved(Touch* touch, cocos2d::Event* event){
    if (sprite->getBoundingBox().containsPoint(touch->getLocation()))
    {
        sprite->setPosition(sprite->getPosition() + touch->getDelta());
    }
}
void HelloWorld::onTouchEnded(Touch* touch, cocos2d::Event* event){
    if (sprite->getBoundingBox().containsPoint(touch->getLocation()))
    {
        log("Sprite Drop Event");
    }
}