首页 > 其他 > 详细

cocos2d 主角更随触屏走

时间:2014-09-06 16:01:13      阅读:189      评论:0      收藏:0      [点我收藏+]
bool HelloWorld::init()
{
    if ( !CCLayer::init() )
    {
        return false;
    }
    
    CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    hero= CCSprite::create("1.png");
    hero->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
    this->addChild(hero, 0);

    CCDirector* pDirector=CCDirector::sharedDirector();
    pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true);

    return true;
}

bool HelloWorld::ccTouchBegan(CCTouch* touch,CCEvent* event)
{
    CCPoint heropos=hero->getPosition();
    CCPoint location=touch->getLocationInView();
    location=CCDirector::sharedDirector()->convertToGL(location);
    if(location.x>heropos.x-32&&location.x<heropos.x+32&&
        location.y>heropos.y-32&&location.y<heropos.y+32)
    {
        isControl=true;
        deltax=location.x-heropos.x;
        deltay=location.y-heropos.y;
    }
    return true;
}

void HelloWorld::ccTouchMoved(CCTouch* touch,CCEvent* event)
{
    if(isControl)
    {
        CCPoint location=touch->getLocationInView();
        location=CCDirector::sharedDirector()->convertToGL(location);
        float x=location.x-deltax;
        float y=location.y-deltay;
        hero->setPosition(ccp(x,y));
    }
}

 

cocos2d 主角更随触屏走

原文:http://www.cnblogs.com/yufenghou/p/3959434.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!