码迷,mamicode.com
首页 > 其他好文 > 详细

cocos2d 主角更随触屏走

时间:2014-09-06 16:01:13      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   ar   div   sp   log   

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 主角更随触屏走

标签:style   blog   color   os   io   ar   div   sp   log   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!