CCTouch的单点触摸与精灵拖动(1)

目的:用CCTouch实现单点触摸和精灵拖拽

过程:1、在.h头文件中定义

CCSprite *selSprite;
   CCArray *movableSprite;
   bool ccTouchBegan(CCTouch *touch,CCEvent *event);
   void selectSpriteForTouch(CCPoint touchLocation);


2、.m文件中重写 init( ) 方法如下:

bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !CCLayer::init() )
{
return false;
}

CCSprite *background = CCSprite::create("background.png");
background->setAnchorPoint(CCPointZero);
background->setPosition(CCPointZero);
background->setScale(3);//背景图片放大3倍
this->addChild(background);

movableSprite = CCArray::create();
CCSprite *Player = CCSprite::create("Player.png");
CCSprite *Projectile = CCSprite::create("Projectile.png");
CCSprite *Target = CCSprite::create("Target.png");

movableSprite->addObject(Player);
movableSprite->addObject(Projectile);
movableSprite->addObject(Target);

for (int i = 0; i < movableSprite->count(); i++) {
CCSprite *sprite = (CCSprite *)movableSprite->objectAtIndex(i);
sprite->setPosition(ccp(300 + 200*i, 300));
sprite->setScale(2.0);
this->addChild(sprite);
}

movableSprite->retain();

CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);//Dont forget this!!!

return true;
}


3、实现头文件定义的两个方法

bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event){

CCPoint touchLocation = this->convertTouchToNodeSpace(touch);

this ->selectSpriteForTouch(touchLocation);

 

return true;

}
void HelloWorld::selectSpriteForTouch(cocos2d::CCPoint touchLocation){

CCSprite *newSprite = NULL;

for (int  i = 0; movableSprite->count(); i++) {

CCSprite *sprite = (CCSprite *)movableSprite->objectAtIndex(i);

if (sprite->boundingBox().containsPoint(touchLocation)) {

newSprite = sprite;

break;

}

}

if (newSprite != selSprite) {

if (selSprite != NULL) {

selSprite->stopAllActions();

}

CCRotateBy *rotLeft = CCRotateBy::create(0.1, -50);

CCRotateBy *center = CCRotateBy::create(0.1, 0);

CCRotateBy *rotRight = CCRotateBy::create(0.1, 50);

CCFiniteTimeAction *rotSeq = CCSequence::create(rotLeft,center,rotRight,center,NULL);

newSprite->runAction(CCRepeatForever::create((CCActionInterval *)rotSeq));

selSprite = newSprite;

}

}


运行结果如下:(单你点击精灵的时候精灵会在0.1秒内左右来回50角晃动)

note:该程序有一个bug,单你多次来回点击精灵的时候,,精灵晃动停止后会旋转一定的角度,,我也不知道为什么,刚学习有很多不懂,先留着,以后再回来看!

精灵拖动:

代码如下:

#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"

using namespace cocos2d;
using namespace CocosDenshion;

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;
    }
    
    CCSprite *background = CCSprite::create("background.png");
    background->setAnchorPoint(CCPointZero);
    background->setPosition(CCPointZero);
    background->setScale(3);//背景图片放大3倍
    this->addChild(background);
    

    movableSprite = CCArray::create();
    CCSprite *Player = CCSprite::create("Player.png");
    CCSprite *Projectile = CCSprite::create("Projectile.png");
    CCSprite *Target = CCSprite::create("Target.png");
    
    movableSprite->addObject(Player);
    movableSprite->addObject(Projectile);
    movableSprite->addObject(Target);
    
    for (int i = 0; i < movableSprite->count(); i++) {
        CCSprite *sprite = (CCSprite *)movableSprite->objectAtIndex(i);
        sprite->setPosition(ccp(300 + 200*i, 300));
        sprite->setScale(2.0);
        this->addChild(sprite);
    }
    
    movableSprite->retain();
    
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this, 0, true);
    
    return true;
}

bool HelloWorld::ccTouchBegan(cocos2d::CCTouch *touch, cocos2d::CCEvent *event){
    CCPoint touchLocation = this->convertTouchToNodeSpace(touch);
    this ->selectSpriteForTouch(touchLocation);

    return true;
}

void HelloWorld::ccTouchMoved(cocos2d::CCTouch *touch, cocos2d::CCEvent *event){
    CCPoint touchLocation = this->convertTouchToNodeSpace(touch);
    CCPoint oldTouchLocation = touch->getPreviousLocationInView();
    oldTouchLocation = CCDirector::sharedDirector()->convertToGL(oldTouchLocation);//转换成openGL的坐标
    oldTouchLocation = this->convertToNodeSpace(oldTouchLocation);//转换成屏幕坐标
    CCPoint tranlation = ccpSub(touchLocation, oldTouchLocation);//相对坐标(相对位移 ),计算两者之差
    this->panForTranslation(tranlation);
}

void HelloWorld::panForTranslation(cocos2d::CCPoint translation){
    CCPoint newPos = ccpAdd(selSprite->getPosition(), translation);//原有位置+相对位移
    selSprite->setPosition(newPos);
}


void HelloWorld::selectSpriteForTouch(cocos2d::CCPoint touchLocation){
    CCSprite *newSprite = NULL;
    for (int  i = 0; movableSprite->count(); i++) {
        CCSprite *sprite = (CCSprite *)movableSprite->objectAtIndex(i);
        if (sprite->boundingBox().containsPoint(touchLocation)) {
            newSprite = sprite;
            break;
        }
    }
    
    if (newSprite != selSprite) {
        if (selSprite != NULL) {
            selSprite->stopAllActions();
        }
        CCRotateBy *rotLeft = CCRotateBy::create(0.1, -50);
        CCRotateBy *center = CCRotateBy::create(0.1, 0);
        CCRotateBy *rotRight = CCRotateBy::create(0.1, 50);
        CCFiniteTimeAction *rotSeq = CCSequence::create(rotLeft,center,rotRight,center,NULL);
        newSprite->runAction(CCRepeatForever::create((CCActionInterval *)rotSeq));
        selSprite = newSprite;
    }
}



void HelloWorld::menuCloseCallback(CCObject* pSender)
{
    CCDirector::sharedDirector()->end();

#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
    exit(0);
#endif
}


运行结果如下:


你可能感兴趣的:(cocos2d-x,CCTouch)