How to perform drag and drop operation on sprite fetched from array and present on screen ?

北城以北 提交于 2019-12-11 18:13:10

问题


I want to perform drag and drop operation on these image.

How can i make it possible with the following code.

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;


            }}}

回答1:


To drag and drop images from one point to another on screen you have to use touch delegate methods

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

Detect image on user touch in ccTouchBegan method, for this you can store all image objects in a array and check if touch is in rect of any image by using for loop.

To move the image with user touch change position of touched image(save touched image object in a global object) in ccTouchMoved.

And in ccTouchEnded method do whatever you want to do on droping image.




回答2:


the easiest way to catch drag and drop events is by implementing the onTouchBegan and onTouchMoved and onTouchEnded methods like this:

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");
    }
}


来源:https://stackoverflow.com/questions/21701720/how-to-perform-drag-and-drop-operation-on-sprite-fetched-from-array-and-present

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!