zoukankan      html  css  js  c++  java
  • cocos2dx CCLayer上精灵的点击判断的问题

    今天发现的问题,记录下,对cocos2d坐标转换的理解还不透彻,看来有必要去学习下OpenGL的基础知识了。

    //使用的2dx是老版本.

    1. 平时在CCLayer上放置的CCSprite,判断是否被点击到的,我一般这样做:

    //on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        bool bRet = false;
        do 
        {
            CC_BREAK_IF(! CCLayer::init());
    		
            //Get window size. 
            CCSize size = CCDirector::sharedDirector()->getWinSize();
    		
            //pSprite is a CCSprite for test.
            pSprite = CCSprite::spriteWithFile("fjut.png");
            CC_BREAK_IF(! pSprite);
    		
            //Place the sprite on the center of the screen	
            pSprite->setPosition(ccp(size.width/2, size.height/2));
    		
            this->addChild(pSprite, 0);
            bRet = true;
        } while (0);
    	
        this->setIsTouchEnabled(true);
        return bRet;
    }
    void HelloWorld::registerWithTouchDispatcher()
    {
        CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, -1988, true);
    }
    
    static CCRect getRect(CCNode* pNode)
    {
        CCRect rc;
        rc.origin = pNode->getPosition();
        rc.size = pNode->getContentSize();
        rc.origin.x -= rc.size.width*0.5;
        rc.origin.y -= rc.size.height*0.5;
        return rc;
    }
    
    void HelloWorld::ccTouchEnded(CCTouch* pTouch, CCEvent* event)
    {
        CCPoint touchLocation = convertTouchToNodeSpace(pTouch);
        if(CCRect::CCRectContainsPoint(getRect(pSprite), touchLocation))
        {
    	    printf("我被点中了!\n");
        }
    }
    
    bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
    {
        return true;
    }

    2. 要是在CCLayer1上放置CCLayer2(有偏移),再向CCLayer2上放置CCSprite的点击判断需要计算偏移量:

    eg:

    //on "init" you need to initialize your instance
    bool HelloWorld::init()
    {
        bool bRet = false;
        do 
        {
            CC_BREAK_IF(! CCLayer::init());
            bRet = true;
        } while (0);
    	
        this->setIsTouchEnabled(true);
    	
        this->testLayer();
    	
        return bRet;
    }
    
    void HelloWorld::testLayer()
    {
        CCLayer* ly = CCLayer::node();
        /*x, y轴各偏移100*/
        ly->setPosition(ccp(100, 100));
    
        //layer2 add to layer1
        this->addChild(ly);
    
        pSprite = CCSprite::spriteWithFile("fjut.png");
        pSprite->setPosition(ccp(20, 20));
        ly->addChild(pSprite, 0);
    }
    
    void HelloWorld::registerWithTouchDispatcher()
    {
        CCTouchDispatcher::sharedDispatcher()->addTargetedDelegate(this, -1988, true);
    }
    
    static CCRect getRect(CCNode* pNode)
    {
        CCRect rc;
        rc.origin = pNode->getPosition();
        rc.size = pNode->getContentSize();
        /*x, y轴各偏移100*/
        rc.origin.x -= rc.size.width*0.5 - 100;//!!!!
        rc.origin.y -= rc.size.height*0.5 - 100;//!!!!
        return rc;
    }
    
    void HelloWorld::ccTouchEnded(CCTouch* pTouch, CCEvent* event)
    {
        CCPoint touchLocation = convertTouchToNodeSpace(pTouch);
        if(CCRect::CCRectContainsPoint(getRect(pSprite), touchLocation))
        {
    	    printf("我被点中了!\n");
        }
    }
    
    bool HelloWorld::ccTouchBegan(CCTouch* touch, CCEvent* event)
    {
        return true;
    }
  • 相关阅读:
    The Problem Came Up when we upgrade from KingDee K3 to SAP R3(FI module)
    小代码 OpenFileDialog() Windows下Upload & rewrite 文件
    [Note]CodeSmith 4.0 Lesson One
    正则表达式 运算 常见
    SAP function overview & instruction
    CodeSmith NetTier 保存主从表实例,当然,至于3级关联的问题还是类似做法,依靠Relationship做级联更新
    CodeSmith NetTier模板生成的代码框架用法 (转)
    [Notes] Demo The practice about how to using SAP XI components to build up the mapping bridge cross the application layer
    6260/7610/6670/3230 蓝牙 共享PC 宽带 无须代理
    [ALE in SAP]BAPI Introduction
  • 原文地址:https://www.cnblogs.com/fjut/p/2725260.html
Copyright © 2011-2022 走看看