cocos2d-x 实现精灵拖动效果

#include "HelloWorldScene.h"


USING_NS_CC;


CCScene* HelloWorld::scene()

{

    // 'scene' is an autorelease object

    CCScene *scene = CCScene::create();

    

    // 'layer' is an autorelease object

    HelloWorld *layer = HelloWorld::create();


    // add layer as a child to scene

    scene->addChild(layer);


    // return the scene

    return scene;

}


// on "init" you need to initialize your instance

bool HelloWorld::init()

{

    //

    // 1. super init first

    if ( !CCLayer::init() )

    {

        return false;

    }

    

 

//    

    //创建一个节点

    CCNode *pNode = CCNode::create();

  

  //  pNode->setScale(0.5);

    //将节点作为子节点加入场景类中

   this->addChild(pNode, 0);

// 

    

    /

    // 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(CCDirector::sharedDirector()->getWinSize().width - 20,

                                20));


    // create menu, it's an autorelease object

    CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);

    pMenu->setPosition(CCPointZero);

    pNode->addChild(pMenu, 1);


    /

    // 3. add your codes below...


    // add a label shows "Hello World"

    // create and initialize a label

    

    CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);

    

    

    CCSize size = CCDirector::sharedDirector()->getWinSize();

    // position the label on the center of the screen

    pLabel->setPosition(ccp(size.width/2,

                            size.height - 20));


    // add the label as a child to this layer

    pNode->addChild(pLabel, 1);


    // add "HelloWorld" splash screen"

    CCSprite* pSprite = CCSprite::create("HelloWorld.png");


    // position the sprite on the center of the screen

    pSprite->setPosition(ccp(size.width/2 , size.height/2 ));


    // add the sprite as a child to this layer

   pNode->addChild(pSprite, 0);

  //    pNode->setPosition(ccp(50, 50));

    

    

    

    hero = CCSprite::create("grossini.png");

    

    hero->setPosition(ccp(size.width/2,size.height/2));

    

//    pNode->setScale(0.5);

    pNode->addChild(hero,1);


    setTouchEnabled(true);

    

    return true;

}



void HelloWorld::menuCloseCallback(CCObject* pSender)

{

    CCDirector::sharedDirector()->end();


#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)

    exit(0);

#endif

}



void HelloWorld::registerWithTouchDispatcher()

{

    CCDirector*pDirector = CCDirector::sharedDirector();

    pDirector->getTouchDispatcher()->addTargetedDelegate(this, 0, 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 - 42.5 && location.x < heropos.x + 42.5 && location.y > heropos.y - 60.5 && location.y < heropos.y + 60.5)

    {

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

    }

}


void HelloWorld::ccTouchEnded(CCTouch * touch, CCEvent * event)

{

    isControl = false;

}


void HelloWorld::ccTouchCancelled(CCTouch *touch, CCEvent *event)

{

    isControl = false;

}

转载于:https://my.oschina.net/jingzhe/blog/164376

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值