Example #1
0
int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches)
{
    int ret = 0;
    do 
    {
        CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
        CC_BREAK_IF(NULL == pScriptHandlerEntry);
        int nScriptHandler = pScriptHandlerEntry->getHandler();
        CC_BREAK_IF(0 == nScriptHandler);
        
        cleanStack();
        lua_pushinteger(m_state, eventType);
        lua_newtable(m_state);

        CCDirector* pDirector = CCDirector::sharedDirector();
        CCSetIterator it = pTouches->begin();
        CCTouch* pTouch;
        int n = 1;
        while (it != pTouches->end())
        {
            pTouch = (CCTouch*)*it;
            CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView());
            lua_pushnumber(m_state, pt.x);
            lua_rawseti(m_state, -2, n++);
            lua_pushnumber(m_state, pt.y);
            lua_rawseti(m_state, -2, n++);
            ++it;
        }

        ret = executeFunctionByHandler(nScriptHandler, 2);
    } while (0);

    return ret;
}
Example #2
0
void AccelerometerTest::didAccelerate(CCAcceleration* pAccelerationValue)
{
//     double fNow = pAccelerationValue->timestamp;
// 
//     if (m_fLastTime > 0.0)
//     {
//         CCPoint ptNow = convertToUI
//     }
// 
//     m_fLastTime = fNow;

    CCDirector* pDir = CCDirector::sharedDirector();

    /*FIXME: Testing on the Nexus S sometimes m_pBall is NULL */
    if ( m_pBall == NULL ) {
        return;
    }

    CCSize ballSize  = m_pBall->getContentSize();

    CCPoint ptNow  = m_pBall->getPosition();
    CCPoint ptTemp = pDir->convertToUI(ptNow);

    ptTemp.x += pAccelerationValue->x * 9.81f;
    ptTemp.y -= pAccelerationValue->y * 9.81f;

    CCPoint ptNext = pDir->convertToGL(ptTemp);
    FIX_POS(ptNext.x, (VisibleRect::left().x+ballSize.width / 2.0), (VisibleRect::right().x - ballSize.width / 2.0));
    FIX_POS(ptNext.y, (VisibleRect::bottom().y+ballSize.height / 2.0), (VisibleRect::top().y - ballSize.height / 2.0));
    m_pBall->setPosition(ptNext);
}
Example #3
0
void GameScene::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent){
    
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCPoint touchPoint = pDirector->convertToGL(pTouch->getLocationInView());
  
    CCNode* pCard = this->getChildByTag(nextNumber);
    if (!pCard)
    {
        return;
    }
    
    CCRect cardRect = pCard->boundingBox();
    if (cardRect.containsPoint(touchPoint)) {
        CCSprite* pNewCard = CCSprite::create("card_backside.png");
        pNewCard->setPosition(pCard->getPosition());
        this->addChild(pNewCard);
        
        pCard->removeFromParentAndCleanup(true);
        
        if (nextNumber >= 25)
        {
            this->unschedule(schedule_selector(GameScene::measureGametime));
            showHighScoreLabel();
            return;
        }
        nextNumber++;
    }
    
    //CCLog("x: %f, y:%f", touchPoint.x, touchPoint.y);
}
void GameLayer::ccTouchesBegan(CCSet* pTouch, CCEvent* pEvent){
	
	//check if the backIcon is pressed
	CCTouch *touch = (CCTouch*)pTouch->anyObject();
	CCPoint location = touch->getLocation();
	CCDirector* pDirector = CCDirector::sharedDirector();
	CCSize winSize = pDirector->getWinSize();
	float icon_radius = backIcon->getContentSize().height / 2;
	//distance from icon target to center of the sprite
	float distanceSQ = ccpDistanceSQ(location, backIcon->getPosition());
	if (distanceSQ <= icon_radius * icon_radius){
		//change the icon to pressed
		//MapLayer::getMap()->unlockNextLevel();
		pDirector->popScene();
		return;
	}
	//back button checking ended
	
	
	//gesture detection initiate
	ccp_last = touch->getLocationInView();
	ccp_last = pDirector->convertToGL(ccp_last);

	b_click = true;
	b_rect = false;
	b_circle = false;
	count_order = 0;
	gd_direction = kGDirectionNo;

	for (int i = 0; i < 4; i++){
		cache_deirectionshape[i] = false;
	}
	//gesture detection ended
}
void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
    for(CCSetIterator it = pTouches->begin(); it != pTouches->end(); it++)
    {
        CCTouch* touch = (CCTouch*)(*it);
        if(touch == NULL)
            break;

        CCDirector* director = CCDirector::sharedDirector();
        CCPoint p = touch->getLocationInView();
        
        CCPoint touchPosition = director->convertToGL(p);
        

        CCSize size =  m_rightItem->getContentSize();
        CCPoint position = m_rightItem->getPosition();
        CCRect rect = CCRectMake(position.x - size.width/2, position.y - size.height/2, size.width, size.height);
        
        m_isPressedRight = rect.containsPoint(touchPosition);
        
        if(!m_isPressedRight) {
            CCSize size =  m_leftItem->getContentSize();
            CCPoint position = m_leftItem->getPosition();
            CCRect rect = CCRectMake(position.x - size.width/2, position.y - size.height/2, size.width, size.height);
            m_isPressedLeft = rect.containsPoint(touchPosition);
        }

        
       
    }
    
    //m_isPressedLeft = m_leftItem->co

}
Example #6
0
void StartLayer::update(float dt)
{
    CCSprite* title = (CCSprite*)this->getChildByTag(10000);
    CCDirector* pDir = CCDirector::sharedDirector();
    CCSize winSize   = pDir->getWinSize();
    
    //判断小球精灵是否有效。
    if ( title == NULL ) {
        return;
    }
    //取得小球的图像区域大小。
    CCSize ballSize  = title->getContentSize();
    //取得小球的当前位置。
    CCPoint ptNow  = title->getPosition();
    //将当前位置转换成界面坐标系的位置。
    CCPoint ptTemp = pDir->convertToUI(ptNow);
    //由收到的速度乘以一个系数后来影响位置。
    ptTemp.x += posChange.x;
    ptTemp.y -= posChange.y;
    //再转换为OPENGL坐标系的位置。貌似有点麻烦,其实直接在上面X,Y的加减上做上正确的方向即可。
    CCPoint ptNext = pDir->convertToGL(ptTemp);
    //限定位置的X,Y的有效范围,等于小球边缘始终在窗口内。
    FIX_POS(ptNext.x, (ballSize.width / 2.0), (winSize.width - ballSize.width / 2.0));
    FIX_POS(ptNext.y, (ballSize.height / 2.0), (winSize.height - ballSize.height / 2.0));
    //将位置传给小球。
    title->setPosition(ptNext);
    

}
/* Touch Moved */
void ControlLayer::ccTouchMoved(cocos2d::CCTouch *ptouch, cocos2d::CCEvent *pEvent) {
    // Log
    CCLog("touch Moved");
    
    // Get Tap Point
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCPoint touchPoint = pDirector->convertToGL(ptouch->getLocationInView());
    
    // Move to Particle from Tap Position 
    streak->setPosition(touchPoint);

}
/* Touch Moved */
void ControlLayer::ccTouchMoved(cocos2d::CCTouch *ptouch, cocos2d::CCEvent *pEvent) {
    // Log
    CCLog("touch Moved");
    
   
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCPoint touchPoint = pDirector->convertToGL(ptouch->getLocationInView());
    
    // タップ位置へパーティクルを移動
    streak->setPosition(touchPoint);
    
}
Example #9
0
int CCLuaEngine::executeLayerTouchesEvent(CCLayer* pLayer, int eventType, CCSet *pTouches)
{
    CCTouchScriptHandlerEntry* pScriptHandlerEntry = pLayer->getScriptTouchHandlerEntry();
    if (!pScriptHandlerEntry) return 0;
    int nHandler = pScriptHandlerEntry->getHandler();
    if (!nHandler) return 0;
    
    switch (eventType)
    {
        case CCTOUCHBEGAN:
            m_stack->pushString("began");
            break;
            
        case CCTOUCHMOVED:
            m_stack->pushString("moved");
            break;
            
        case CCTOUCHENDED:
            m_stack->pushString("ended");
            break;
            
        case CCTOUCHCANCELLED:
            m_stack->pushString("cancelled");
            break;
            
        default:
            return 0;
    }

    CCDirector* pDirector = CCDirector::sharedDirector();
    lua_State *L = m_stack->getLuaState();
    lua_newtable(L);
    int i = 1;
    for (CCSetIterator it = pTouches->begin(); it != pTouches->end(); ++it)
    {
        CCTouch* pTouch = (CCTouch*)*it;
        CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView());
        lua_pushnumber(L, pt.x);
        lua_rawseti(L, -2, i++);
        lua_pushnumber(L, pt.y);
        lua_rawseti(L, -2, i++);
        lua_pushinteger(L, pTouch->getID());
        lua_rawseti(L, -2, i++);
    }
    int ret = m_stack->executeFunctionByHandler(nHandler, 2);
    m_stack->clean();
    return ret;
}
void ShootingGameScene::didAccelerate(CCAcceleration* pAccelerationValue) {
	CCDirector* pDir = CCDirector::sharedDirector();
	if (_aim == NULL)
		return;
	CCSize aimsize = _aim->getContentSize();

	CCPoint aimNow = _aim->getPosition();
	CCPoint aimTemp = pDir->convertToUI(aimNow);

	aimTemp.x += pAccelerationValue->x * 50.0f;
	aimTemp.y -= pAccelerationValue->y * 50.0f;//9.81f;

	CCPoint aimNext = pDir->convertToGL(aimTemp);
	FIX_POS(aimNext.x,(aimsize.width/2.0),(winsize.width  - aimsize.width/2.0f));
	FIX_POS(aimNext.y,(aimsize.height/2.0),(winsize.height  - aimsize.height/2.0f));

	_aim->setPosition(aimNext);
}
Example #11
0
void Director1::ccTouchesEnded(CCSet * touches, CCEvent* event)
{
    CCSetIterator it;
    CCTouch* touch;

    for( it = touches->begin(); it != touches->end(); it++) 
    {
        touch = (CCTouch*)(*it);

        if(!touch)
            break;
		CCPoint a = touch->locationInView();

        CCDirector *director = CCDirector::sharedDirector();
		CCPoint b = director->convertToUI(director->convertToGL(a));
		CCLog("(%d,%d) == (%d,%d)", (int) a.x, (int)a.y, (int)b.x, (int)b.y );
	}
}
/* Touch Began */
bool ControlLayer::ccTouchBegan(cocos2d::CCTouch *ptouch, cocos2d::CCEvent *pEvent) {
    
    // Log
    CCLog("touch Began");
    
    /* Device Size */
    CCSize winSize = CCDirector::sharedDirector()->getWinSize();
    
    
    // Touch Point
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCPoint touchPoint = pDirector->convertToGL(ptouch->getLocationInView());
    
    CCLog("touch->x:%d y:%d from ControlLayer", touchPoint.x, touchPoint.y);
    
    /* Touch */
    MultiSceneLayer::sharedLayer()->gameLayer()->playTouch(ptouch, pEvent);
    
    return true;
}
Example #13
0
int CCLuaEngine::executeTouchesEvent(int nHandler, int eventType, CCSet *pTouches)
{
    lua_pushinteger(m_state, eventType);
    lua_newtable(m_state);

    CCDirector* pDirector = CCDirector::sharedDirector();
    CCSetIterator it = pTouches->begin();
    CCTouch* pTouch;
    int n = 1;
    while (it != pTouches->end())
    {
        pTouch = (CCTouch*)*it;
        CCPoint pt = pDirector->convertToGL(pTouch->locationInView());
        lua_pushnumber(m_state, pt.x);
        lua_rawseti(m_state, -2, n++);
        lua_pushnumber(m_state, pt.y);
        lua_rawseti(m_state, -2, n++);
        ++it;
    }

    return executeFunctionByHandler(nHandler, 2);
}
Example #14
0
int CCLuaEngine::executeNodeTouchesEvent(CCNode* pNode, int eventType, CCSet *pTouches, int phase)
{
    m_stack->clean();
    CCLuaValueDict event;
    switch (eventType)
    {
        case CCTOUCHBEGAN:
            event["name"] = CCLuaValue::stringValue("began");
            break;

        case CCTOUCHMOVED:
            event["name"] = CCLuaValue::stringValue("moved");
            break;

        case CCTOUCHENDED:
            event["name"] = CCLuaValue::stringValue("ended");
            break;

        case CCTOUCHCANCELLED:
            event["name"] = CCLuaValue::stringValue("cancelled");
            break;

        case CCTOUCHADDED:
            event["name"] = CCLuaValue::stringValue("added");
            break;

        case CCTOUCHREMOVED:
            event["name"] = CCLuaValue::stringValue("removed");
            break;

        default:
            return 0;
    }

    event["mode"] = CCLuaValue::intValue(kCCTouchesAllAtOnce);
    switch (phase)
    {
        case NODE_TOUCH_CAPTURING_PHASE:
            event["phase"] = CCLuaValue::stringValue("capturing");
            break;

        case NODE_TOUCH_TARGETING_PHASE:
            event["phase"] = CCLuaValue::stringValue("targeting");
            break;

        default:
            event["phase"] = CCLuaValue::stringValue("unknown");
    }

    CCLuaValueDict points;
    CCDirector* pDirector = CCDirector::sharedDirector();
    char touchId[16];
    for (CCSetIterator touchIt = pTouches->begin(); touchIt != pTouches->end(); ++touchIt)
    {
        CCLuaValueDict point;
        CCTouch* pTouch = (CCTouch*)*touchIt;
        sprintf(touchId, "%d", pTouch->getID());
        point["id"] = CCLuaValue::stringValue(touchId);

        const CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView());
        point["x"] = CCLuaValue::floatValue(pt.x);
        point["y"] = CCLuaValue::floatValue(pt.y);
        const CCPoint prev = pDirector->convertToGL(pTouch->getPreviousLocationInView());
        point["prevX"] = CCLuaValue::floatValue(prev.x);
        point["prevY"] = CCLuaValue::floatValue(prev.y);

        points[touchId] = CCLuaValue::dictValue(point);
    }
    event["points"] = CCLuaValue::dictValue(points);
    m_stack->pushCCLuaValueDict(event);

    int eventInt = (phase == NODE_TOUCH_CAPTURING_PHASE) ? NODE_TOUCH_CAPTURE_EVENT : NODE_TOUCH_EVENT;
    CCArray *listeners = pNode->getAllScriptEventListeners();
    CCScriptHandlePair *p;
    for (int i = listeners->count() - 1; i >= 0; --i)
    {
        p = dynamic_cast<CCScriptHandlePair*>(listeners->objectAtIndex(i));
        if (p->event != eventInt || p->removed) continue;
        m_stack->copyValue(1);
        m_stack->executeFunctionByHandler(p->listener, 1);
        m_stack->settop(1);
    }

    m_stack->clean();
    
    return 1;
}
Example #15
0
void MainGameScene::ccTouchEnded(cocos2d::CCTouch *pTouch, cocos2d::CCEvent *pEvent)
{

    
    //タップポイント取得
    CCDirector* pDirector = CCDirector::sharedDirector();
    CCPoint touchPoint = pDirector->convertToGL(pTouch->getLocationInView());
    
    if(endFlag){
        
    }else{
        
       
        
        for(int i = 1; i <= COL_NUM * ROW_NUM; i++){
            Arrow* arrowObj = (Arrow *)this->getChildByTag(i);
            
            if(!arrowObj){
                return;
            }
            
            //タッチ判定
            CCRect arrowRect = arrowObj->getArrowSprite()->boundingBox();
            if(arrowRect.containsPoint(touchPoint)){
                
                //タッチ数を更新して表示
                if(totalGameCount < 99){
                    totalGameCount++;
                }
                
                showGameCountLabel();
                
                //z-indexあげる
                arrowObj->setZOrder(Z_ORDER_ARROW);
                
                setTouchEnabled(false);
                
                //タッチした時の内部処理
                arrowObj->onTouchArrow();
                
                
                bool reverseFlag;
                //左端かつ左向き
                if(i % COL_NUM == 1 && arrowObj->getDirection() == 3){
                    reverseFlag = false;
                    
                    //右端かつ右向き
                }else if (i % COL_NUM == 0 && arrowObj->getDirection() == 1){
                    reverseFlag = false;
                    
                    //下端かつ下向き
                }else if (i <= COL_NUM && arrowObj->getDirection() == 2){
                    reverseFlag = false;
                    
                    //上端かつ上向き
                }else if (i <= COL_NUM * ROW_NUM && i > COL_NUM * (ROW_NUM - 1) && arrowObj->getDirection() == 0 ){
                    reverseFlag = false;
                    
                    //反転する場合
                }else{
                    reverseFlag = true;
                    
                }
                
                int addIndex;
                switch (arrowObj->getDirection()) {
                    case 0: //上
                        addIndex = ROW_NUM;break;
                    case 1: //右
                        addIndex = 1;break;
                    case 2: //下
                        addIndex = -ROW_NUM;break;
                    case 3: //左
                        addIndex = -1;break;
                }
                
                int nextIndex = i + addIndex;
                Arrow* nextArrowObj;
                if(nextIndex > 0) {
                   nextArrowObj = (Arrow *)this->getChildByTag(nextIndex);
                }
                
                if(!nextArrowObj){
                    reverseFlag = false;
                }
                
                
                CCCallFunc* funcReverseArrow = CCCallFunc::create(arrowObj, callfunc_selector(Arrow::reverseArrow));
                
                CCRotateBy* rotate = CCRotateBy::create(0.3, 90);
                
                CCScaleTo* scaleUp = CCScaleTo::create( 0.15,1.8);
                CCScaleTo* scaleDown = CCScaleTo::create(0.15,1.0);
                CCSequence* seqScale = CCSequence::createWithTwoActions(CCEaseInOut::create(scaleUp, 2), CCEaseInOut::create(scaleDown, 2));
                
                CCSpawn* spaRotateAndScale = CCSpawn::createWithTwoActions(rotate, seqScale);
                
                CCDelayTime* delay = CCDelayTime::create(0.1);
                CCCallFunc* funcEndJudge = CCCallFunc::create(this, callfunc_selector(MainGameScene::endJudgemnet));
                
                if(reverseFlag){
                    
                    CCCallFunc* funcReverseNextArrow = CCCallFunc::create(nextArrowObj, callfunc_selector(Arrow::reverseArrow));
                    arrowObj->getArrowSprite()->runAction(CCSequence::create(spaRotateAndScale, delay, funcReverseArrow, funcReverseNextArrow, funcEndJudge, NULL));
                }else{
                    arrowObj->getArrowSprite()->runAction(CCSequence::create(spaRotateAndScale, delay, funcReverseArrow, funcEndJudge, NULL));
                }
                
                if(arrowObj->getArrowSprite() == arrowObj->getArrowOnSprite()){
                    //Onの場合
                    arrowObj->getArrowOffSprite()->setRotation(arrowObj->getDirection() * 90);
                }else if(arrowObj->getArrowSprite() == arrowObj->getArrowOffSprite()){
                    //Offの場合
                    arrowObj->getArrowOnSprite()->setRotation(arrowObj->getDirection() * 90);
                }
                
            }
            
        }
        
    }
}
Example #16
0
int CCLuaEngine::executeNodeTouchesEvent(CCNode* pNode, int eventType, CCSet *pTouches, int phase)
{
    CCScriptEventListenersForEvent &listeners = pNode->getScriptEventListenersByEvent(phase == NODE_TOUCH_CAPTURING_PHASE ? NODE_TOUCH_CAPTURE_EVENT : NODE_TOUCH_EVENT);
    if (listeners.size() == 0) return 1;

    m_stack->clean();
    CCLuaValueDict event;
    switch (eventType)
    {
        case CCTOUCHBEGAN:
            event["name"] = CCLuaValue::stringValue("began");
            break;

        case CCTOUCHMOVED:
            event["name"] = CCLuaValue::stringValue("moved");
            break;

        case CCTOUCHENDED:
            event["name"] = CCLuaValue::stringValue("ended");
            break;

        case CCTOUCHCANCELLED:
            event["name"] = CCLuaValue::stringValue("cancelled");
            break;

        default:
            return 0;
    }

    event["mode"] = CCLuaValue::intValue(kCCTouchesAllAtOnce);
    switch (phase)
    {
        case NODE_TOUCH_CAPTURING_PHASE:
            event["phase"] = CCLuaValue::stringValue("capturing");
            break;

        case NODE_TOUCH_TARGETING_PHASE:
            event["phase"] = CCLuaValue::stringValue("targeting");
            break;

        default:
            event["phase"] = CCLuaValue::stringValue("unknown");
    }

    CCLuaValueDict points;
    CCDirector* pDirector = CCDirector::sharedDirector();
    char touchId[16];
    for (CCSetIterator touchIt = pTouches->begin(); touchIt != pTouches->end(); ++touchIt)
    {
        CCLuaValueDict point;
        CCTouch* pTouch = (CCTouch*)*touchIt;
        sprintf(touchId, "%d", pTouch->getID());
        point["id"] = CCLuaValue::stringValue(touchId);

        const CCPoint pt = pDirector->convertToGL(pTouch->getLocationInView());
        point["x"] = CCLuaValue::floatValue(pt.x);
        point["y"] = CCLuaValue::floatValue(pt.y);
        const CCPoint prev = pDirector->convertToGL(pTouch->getPreviousLocationInView());
        point["prevX"] = CCLuaValue::floatValue(prev.x);
        point["prevY"] = CCLuaValue::floatValue(prev.y);

        points[touchId] = CCLuaValue::dictValue(point);
    }
    event["points"] = CCLuaValue::dictValue(points);
    m_stack->pushCCLuaValueDict(event);

    CCScriptEventListenersForEventIterator it = listeners.begin();
    for (; it != listeners.end(); ++it)
    {
        m_stack->copyValue(1);
        m_stack->executeFunctionByHandler((*it).listener, 1);
        m_stack->settop(1);
    }

    m_stack->clean();
    
    return 1;
}