Cocos2d-x 單點觸摸--讓精靈隨手指移動起來

來源:互聯網
上載者:User

轉載請註明出處:http://blog.csdn.net/oyangyufu/article/details/25656673

:


 

CCTouch類裝載了觸摸點的資訊,包括觸摸點的橫縱座標值和觸摸點的ID號,如擷取觸摸點轉GL座標:

CCPoint point = pTouch->getLocationInView();point = CCDirector::sharedDirector()->convertToGL(point);


建立觸摸事件流程:首先開啟setTouchEnabled(true), 然後重寫registerWithTouchDispatcher調用觸摸代理函數addTargetedDelegate允許布景層接收觸摸事件,再重寫ccTouchBegan、ccTouchMoved、ccTouchEnded、ccTouchCancelled函數

 

程式碼:

bool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !CCLayer::init() )    {        return false;    }        CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();    /////////////////////////////    // 2. add a menu item with "X" image, which is clicked to quit the program    //    you may modify it.    // add a "close" icon to exit the progress. it's an autorelease object    CCMenuItemImage *pCloseItem = CCMenuItemImage::create(                                        "CloseNormal.png",                                        "CloseSelected.png",                                        this,                                        menu_selector(HelloWorld::menuCloseCallback));    pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,                                origin.y + pCloseItem->getContentSize().height/2));    // create menu, it's an autorelease object    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);    pMenu->setPosition(CCPointZero);    this->addChild(pMenu, 1);setTouchEnabled(true);sp1 = CCSprite::create("cpp1.png");sp1->setScale(0.5f);sp1->setPosition(ccp(100, 200));this->addChild(sp1);    return true;}//觸摸移動void HelloWorld::ccTouchMoved(CCTouch* touch, CCEvent* event){if (iscontrol){CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);CCLOG("ccTouchMoved...x:%f y:%f", location.x, location.y);//移動時重新設定sprite座標float x = location.x-deltax;float y = location.y-deltay;sp1->setPosition(ccp(x, y));}}//觸摸開始點擊,計算該點座標與sprite座標差值bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event){CCPoint pos = sp1->getPosition();CCPoint location = touch->getLocationInView();location = CCDirector::sharedDirector()->convertToGL(location);//openGLCCLOG("ccTouchBegan...x:%f y:%f", location.x, location.y);if (location.x > 0 && location.x <960 &&location.y >0 && location.y < 640)//觸摸的矩形地區{iscontrol = true;//計算觸摸點與sprite的座標差值deltax = location.x-pos.x;deltay = location.y-pos.y;}return true;}//觸摸結束void HelloWorld::ccTouchEnded(CCTouch* touch, CCEvent* event){CCLOG("ccTouchEnded...");//iscontrol = false;}//註冊觸摸事件void HelloWorld::onEnter(){CCDirector* pDirector = CCDirector::sharedDirector();pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, true);CCLayer::onEnter();}void HelloWorld::onExit(){CCDirector* pDirector = CCDirector::sharedDirector();pDirector->getTouchDispatcher()->removeDelegate(this);CCLayer::onExit();}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.