예제 #1
0
void HoldOnGame::checkCollision(){
    //小球与障碍物
    CCRect ballRect = playerBall->boundingBox();
    CCRect rect = CCRect(ballRect.origin.x + KBallOffset, ballRect.origin.y + KBallOffset,
                         ballRect.size.width - KBallOffset * 2, ballRect.size.height - KBallOffset * 2);
    for (int i = KBodyTypeRectangleVer; i <= KBodyTypeSquare; i++) {
        CCSprite* sprite = (CCSprite*)gameLayer->getChildByTag(i);
        if (rect.intersectsRect(sprite->boundingBox())) {
            bool over = true;
            
            if (sprite->getTag() == KBodyTypeTriangle) {
                CCRect sRect = sprite->boundingBox();
                CCRect rect1 = CCRect(sRect.origin.x + (sRect.size.width * 0.25) / 2 + KBallOffset, sRect.origin.y,
                                      sRect.size.width * 0.75 - KBallOffset * 2, sRect.size.height * 0.25);
                if (!rect.intersectsRect(rect1)) {
                    rect1 = CCRect(sRect.origin.x + sRect.size.width * 0.25 + KBallOffset, sRect.origin.y + sRect.size.height * 0.25,
                                   sRect.size.width * 0.5 - KBallOffset * 2, sRect.size.height * 0.25);
                    if (!rect.intersectsRect(rect1)) {
                        rect1 = CCRect(sRect.origin.x + sRect.size.width * 3 / 8 + KBallOffset, sRect.origin.y + sRect.size.height * 0.5,
                                       sRect.size.width * 0.25 - KBallOffset* 2, sRect.size.height * 0.25);
                        if (!rect.intersectsRect(rect1)) {
                            rect1 = CCRect(sRect.origin.x + sRect.size.width * 0.45, sRect.origin.y + sRect.size.height * 0.75,
                                           sRect.size.width * 0.1, sRect.size.height * 0.15);
                            if (!rect.intersectsRect(rect1)) {
                                over = false;
                            } else {
                                CCLOG("44444 rect1 = (%.1f, %.1f, %.1f, %.1f)", rect1.origin.x, rect1.origin.y, rect1.size.width, rect1.size.height);
                            }
                        } else {
                            CCLOG("333333 rect1 = (%.1f, %.1f, %.1f, %.1f)", rect1.origin.x, rect1.origin.y, rect1.size.width, rect1.size.height);
                        }
                    } else {
                        CCLOG("22222222 rect1 = (%.1f, %.1f, %.1f, %.1f)", rect1.origin.x, rect1.origin.y, rect1.size.width, rect1.size.height);
                    }
                } else {
                    CCLOG("1111111 rect1 = (%.1f, %.1f, %.1f, %.1f)", rect1.origin.x, rect1.origin.y, rect1.size.width, rect1.size.height);
                }
                
//                if (over) {
//                    CCLOG("rect = (%.1f, %.1f, %.1f, %.1f)", rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
//                    CCLOG("sRect = (%.1f, %.1f, %.1f, %.1f)", sRect.origin.x, sRect.origin.y, sRect.size.width, sRect.size.height);
//                    CCLOG("game over");
////                    this->gameOver();
//                    break;
//                }
            }
            
            if (over) {
                this->gameOver();
                break;
            }
        }
    }
}
void  HelloWorld::handleUserTouch(CCTouch *touch)
{
	  CCSprite *pArrow= (CCSprite*)getChildByTag(kArrow);
	 
         CCPoint location = touch->getLocation();
        location = CCDirector::sharedDirector()->convertToGL(location);
       float x =(float) location.x;
	   float y =(float) location.y;
	   CCSprite *pImage2= (CCSprite*)getChildByTag(kTop);
	   CCSprite *pImage1= (CCSprite*)getChildByTag(kRight);
       CCSprite *pImage3= (CCSprite*)getChildByTag(kBot);
	   CCSprite *pImage4= (CCSprite*)getChildByTag(kLeft);

		CCRect topRect = CCRectMake(pImage2->getPosition().x,pImage2->getPosition().y,50,50);
		CCRect rightRect = CCRectMake(pImage1->getPositionX(),pImage1->getPositionY(),50,50);
		CCRect botRect = CCRectMake(pImage3->getPositionX(),pImage3->getPositionY(),50,50);
	    CCRect leftRect = CCRectMake(pImage4->getPositionX(),pImage4->getPositionY(),50,50);

	CCRect pointRect = CCRectMake(x,y,50,50);

	if(topRect.intersectsRect(pointRect))
 
        {
			 
                pArrow->setPositionX(pArrow->getPositionX());
                pArrow->setPositionX(pArrow->getPositionY()+10);
				
        }
 
         else if
                 (rightRect.intersectsRect(pointRect))
         {
                 pArrow->setPositionX(pArrow->getPositionX()+10);
                pArrow->setPositionX(pArrow->getPositionY());
				
         }
 
          else if
                   (botRect.intersectsRect(pointRect))
         {
                 pArrow->setPositionX(pArrow->getPositionX());
                pArrow->setPositionX(pArrow->getPositionY()-10);
				
         }
          else if
                  (leftRect.intersectsRect(pointRect))
          {
                   pArrow->setPositionX(pArrow->getPositionX()-10);
                pArrow->setPositionX(pArrow->getPositionY());
				
          }
        
}
void HelloWorld::updateGame(float dt)
{
    // given the current Player's Rect, check collisions with all targets falling on the screen
    CCSize playerSize = m_player->getContentSize();
    CCPoint playerPosition = m_player->getPosition();
    CCRect playerRect = CCRectMake(playerPosition.x - playerSize.width/2, playerPosition.y - playerSize.height / 2, playerSize.width, playerSize.height);
    CCArray* targetsToDelete =new CCArray;
    CCObject* jt = NULL;
    
    CCARRAY_FOREACH(m_targets, jt)
    {
        CCSprite *target = dynamic_cast<CCSprite*>(jt);
        CCRect targetRect = CCRectMake(
                                       target->getPosition().x - (target->getContentSize().width/2),
                                       target->getPosition().y - (target->getContentSize().height/2),
                                       target->getContentSize().width,
                                       target->getContentSize().height);
        
        // if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
        if (playerRect.intersectsRect(targetRect))
        {
            // collision here...
            targetsToDelete->addObject(target);
            CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("splash.mp3");
        }
    }
예제 #4
0
KDvoid JewelController::touchMoved ( APSGraphic* pGraphic, const APSTouch& tTouch ) 
{
    // calculate touched position in symbol
    CCPoint  tPosition  = m_pSymbol->convertPositionFromParentNode ( tTouch.getPosition ( ) );    
    CCNode*  pJewelNode = pGraphic->getNode ( );
    
    // change jewel graphic node position to touched position.
    pJewelNode->setPosition ( tPosition );
    
    // get the rectangular box of jewel node
    CCRect  tJewelBox = pJewelNode->boundingBox ( );
    
    // get the rectangular box of boy node.
    CCRect  tBoyBox = m_pBoyGraphic->getNode ( )->boundingBox ( );
    
    // check intersection of the two boxes.
	if (  tJewelBox.intersectsRect ( tBoyBox ) )
	{
        APSActionGroup*  pShakeBoyAction =  (APSActionGroup*) m_pSymbol->getResourceForTag ( "ShakeBoy" );
        // trigger 'ShakeBoy' action group only if it is currently not running.
        if ( !pShakeBoyAction->getRunning ( ) )
		{
            pShakeBoyAction->trigger ( );
        }
    }
}
예제 #5
0
void CCEditBox::keyboardWillShow(CCIMEKeyboardNotificationInfo& info)
{
    // CCLOG("CCEditBox::keyboardWillShow");
    CCRect rectTracked = getRect(this);
    
    //postion bug fix code -sunxy
    if (m_pParent != NULL)
    {
        rectTracked.origin = m_pParent->convertToWorldSpace(rectTracked.origin);
    }
    
    // if the keyboard area doesn't intersect with the tracking node area, nothing needs to be done.
    if (!rectTracked.intersectsRect(info.end))
    {
        CCLOG("needn't to adjust view layout.");
        return;
    }
    
    // assume keyboard at the bottom of screen, calculate the vertical adjustment.
    m_fAdjustHeight = info.end.getMaxY() - rectTracked.getMinY();
    // CCLOG("CCEditBox:needAdjustVerticalPosition(%f)", m_fAdjustHeight);
    
    if (m_pEditBoxImpl != NULL)
    {
        m_pEditBoxImpl->doAnimationWhenKeyboardMove(info.duration, m_fAdjustHeight);
    }
}
예제 #6
0
void CATableView::update(float fDelta)
{
    CAScrollView::update(fDelta);
    
    CCRect rect = this->getBounds();
    rect.origin.y -= rect.size.height * 0.1f;
    rect.size.height *= 1.2f;
    
    std::deque<CATableViewCell*>::iterator itr;
    for (itr=m_pTableCells.begin(); itr!=m_pTableCells.end(); itr++)
    {
        CATableViewCell* cell = *itr;
        CCRect cellRect = cell->getFrame();
        cellRect.origin = m_pContainer->convertToWorldSpace(cellRect.origin);
        cellRect.origin = this->convertToNodeSpace(cellRect.origin);
        
        if (rect.intersectsRect(cellRect))
        {
            cell->setVisible(true);
        }
        else
        {
            cell->setVisible(false);
        }
    }
}
예제 #7
0
bool CBoxBehaviorState::IsPutable(CCSprite* sprite, CCPoint touchPos, CCPoint& avaliablePos)
{
	auto arr = CObjectManager::getInstance()->getBox2dSprite();
	CCPoint setPos;
	bool bIsEnable = true;
	CCRect rect;

	for (int i = 0; i <= 20; i++)
	{
		for (int j = 0; j <= 20; j++)
		{
			CCRect r;
			r.setRect(i * 105 + CScrollManager::getInstance()->getDeltaPosition().x, j * 105 + CScrollManager::getInstance()->getDeltaPosition().y, 105, 105);

			if (r.containsPoint(touchPos))
			{
				setPos = ccp(r.getMidX(), r.getMidY());
				rect = r;
			}
		}
	}

	for (int i = 0; i < arr->getSize(); i++)
	{
		auto anothersprite = arr->getObjectAt(i)->getSpritePtr();

		if (rect.intersectsRect(anothersprite->getBoundingBox()))
			bIsEnable = false;
	}

	avaliablePos = setPos;
	return bIsEnable;
}
예제 #8
0
void LCBattleScene::update(float duration){
    tree->clear();
    
    for(int i = 0; i < monsterArr->count(); ++i){
        tree->addObject((ObjMonster*)monsterArr->objectAtIndex(i));
    }
    tree->addObject(hero);
    
    CCArray* arr = CCArray::create();
    
    for(int i = 0; i < monsterArr->count(); ++i){
        ObjMonster* monster = (ObjMonster*)monsterArr->objectAtIndex(i);
        //if(monster->getrootObj() == NULL) continue;
        tree->getCollisionObjects(monster, arr);
        
        for(int j = 0; j < arr->count(); ++j){
            ObjMonster* monster_1 = (ObjMonster*)arr->objectAtIndex(j);
            CCRect rec = CCRectMake(monster->getShadow()->getPositionX(), monster->getShadow()->getPositionY(), monster->getShadow()->getContentSize().width, monster->getShadow()->getContentSize().height);
            CCRect rec_1 = CCRectMake(monster_1->getShadow()->getPositionX(), monster_1->getShadow()->getPositionY(), monster_1->getShadow()->getContentSize().width, monster_1->getShadow()->getContentSize().height);
            if(rec.intersectsRect(rec_1)){
                //int flag = j % 2 == 0 ? 1 : -1;
                monster_1->moveAway();//(ccp(flag * 20, flag * 20));
            }
        }
    }
}
예제 #9
0
void CACollectionView::loadCollectionCell()
{
	CCRect rect = this->getBounds();
	rect.origin = getContentOffset();
	rect.origin.y -= rect.size.height * 0.1f;
	rect.size.height *= 1.2f;

	std::map<CAIndexPath3E, CACollectionViewCell*>::iterator itr;
	for (itr = m_pUsedCollectionCells.begin(); itr != m_pUsedCollectionCells.end(); itr++)
	{
		CC_CONTINUE_IF(itr->second != NULL);

		CAIndexPath3E r = itr->first;
		CCRect cellRect = m_rUsedCollectionCellRects[r];
		CC_CONTINUE_IF(!rect.intersectsRect(cellRect));

		CACollectionViewCell* cell = m_pCollectionViewDataSource->collectionCellAtIndex(this, cellRect.size, r.section, r.row, r.item);
		if (cell)
		{
			cell->m_nSection = r.section;
			cell->m_nRow = r.row;
			cell->m_nItem = r.item;
			cell->updateDisplayedAlpha(this->getAlpha());
			this->addSubview(cell);
			cell->setFrame(cellRect);
			itr->second = cell;

			if (m_pSelectedCollectionCells.count(r))
			{
				cell->setControlStateSelected();
			}
		}
	}
}
예제 #10
0
NS_S_HARPENGINE

bool _ai_checkFullHit(harp::Character* base, harp::Character *against, float yLayerAttackRangeCheck, bool isMidAirCheck)
{
    CCSize baseDynamicSize = base->getDynamicCollisionSize();
    CCSize againstDynamicSize = against->getDynamicCollisionSize();
    
    // check roughly first
    if(((!isMidAirCheck && !against->isInMidAir()) || isMidAirCheck) &&
       !against->isDead() &&
       fabsf(against->getPositionX() - base->getPositionX()) <= baseDynamicSize.width*2 &&
       fabsf(against->getPositionY() - base->getPositionY()) <= yLayerAttackRangeCheck)
    {
        // finer check
        CCRect baseRect = CCRectMake(base->getPositionX() - baseDynamicSize.width*0.5f, base->getPositionY() - baseDynamicSize.height*0.5f, baseDynamicSize.width, baseDynamicSize.height);
        CCRect againstRect = CCRectMake(against->getPositionX() - againstDynamicSize.width*0.5f, against->getPositionY() - againstDynamicSize.height*0.5f, againstDynamicSize.width, againstDynamicSize.height);
        
        if(baseRect.intersectsRect(againstRect))
        {
            // collided!
            return true;
        }
        else
        {
            // not collide
            return false;
        }
    }
    else
    {
        // not collide
        return false;
    }
}
예제 #11
0
CCPoint Grass::getMaterialPosition(DungeonSpriteBase* parent)
{
    int width = parent->getContentSize().width;
    int height = parent->getContentSize().height;
    
    CCPoint resultPoint;
    while (true) {
        
        int x = ShinzoUtil::getRandamRange((parent->getPosition().x) - (width * 3.0f / 2), (parent->getPosition().x) + (width * 3.0f / 2));
        int y = ShinzoUtil::getRandamRange((parent->getPosition().y) - (height * 3.0f / 2), (parent->getPosition().y) + (height * 3.0f / 2));
        CCPoint gracePoint = parent->convertToNodeSpace(ccp(x, y));
        CCRect graceRect = CCRectMake(x, y, 25, 30);
        
        CCArray* children =  parent->getChildren();
        CCObject* childObj;
        bool isCollision = true;

        CCARRAY_FOREACH(children, childObj) {
            SpriteBase* wall = (SpriteBase*)childObj;
            //自分とは判定しない
            if (wall->spriteType != S_Wall) {
                continue;
            }
            CCRect wallRect = wall->getSelfRectConverted();
            if (wallRect.intersectsRect(graceRect)) {
                isCollision = false;
                break;
            }
        }
        if (isCollision) {
            resultPoint = gracePoint;
            break;
        }
    }
예제 #12
0
void CATableView::recoveryTableCell()
{
    CCRect rect = this->getBounds();
    rect.origin.y -= rect.size.height * 0.1f;
    rect.size.height *= 1.2f;
    
    std::map<CAIndexPath2E, CATableViewCell*>::iterator itr;
    for (itr=m_pUsedTableCells.begin(); itr!=m_pUsedTableCells.end(); itr++)
    {
        
        CATableViewCell* cell = itr->second;
        CC_CONTINUE_IF(cell == NULL);
        CCRect cellRect = cell->getFrame();
        cellRect.origin = m_pContainer->convertToWorldSpace(cellRect.origin);
        cellRect.origin = this->convertToNodeSpace(cellRect.origin);
        
        CC_CONTINUE_IF(rect.intersectsRect(cellRect));
        m_pFreedTableCells[cell->getReuseIdentifier()].pushBack(cell);
        cell->removeFromSuperview();
        cell->resetTableViewCell();
        itr->second = NULL;
        
        CAView* line = m_pUsedLines[itr->first];
        CC_CONTINUE_IF(line == NULL);
        m_pFreedLines.pushBack(line);
        line->removeFromSuperview();
        m_pUsedLines[itr->first] = NULL;
    }
}
예제 #13
0
void Recipe15::update(float dt)
{
	CCSprite* player = (CCSprite*) this->getChildByTag(1);

	CCRect playerRect = CCRectMake(
		player->getPosition().x - (player->getContentSize().width/4),
		player->getPosition().y - (player->getContentSize().height/4),
		player->getContentSize().width/2,
		player->getContentSize().height/2);
	
	CCSprite *food = (CCSprite*)this->getChildByTag(2);
	if(food==NULL)return;

	CCRect foodRect = CCRectMake(
		food->getPosition().x - (food->getContentSize().width/2),
		food->getPosition().y - (food->getContentSize().height/2),
		food->getContentSize().width/2,
		food->getContentSize().height/2);

		if(playerRect.intersectsRect(foodRect) && player->isVisible())
		{
			this->removeChild(food,true);

			player->setTexture(CCTextureCache::sharedTextureCache()->addImage("monkey02.png"));
			this->scheduleOnce(schedule_selector(Recipe15::eat),0.1f);
		}
}
bool ObstacleDobleAir::collision(BaseVehicle &vehicle)
{
    if(vehicle.getState() != kStateJump)
        return false;
    
    float y = vehicle.getPositionY() - vehicle.getPlayerY() - vehicle.getContentSize().height * 0.5f;
    if(y < MAX_PLAYER_JUMP * 0.45f)
        return false;
    
    CCRect rectAir = vehicle.getAirCollision();
    
    int i;
    CCRect area;
    
    for(i = 0; i < vCollision.size(); i++)
    {
        area = currentCollisionArea(vCollision[i]);
        if(area.intersectsRect(rectAir))
        {
            return true;
        }
    }
    
    return false;
}
예제 #15
0
void HelloWorld::update(float dt)
{
    CCArray *projectilesToDelete = new CCArray;
    CCObject* it = NULL;
    CCObject* jt = NULL;
    
    CCARRAY_FOREACH(_projectiles, it)
    {
        CCSprite *projectile = static_cast<CCSprite*>(it);
        CCRect projectileRect = CCRectMake(
        projectile->getPosition().x - (projectile->getContentSize().width/2),
        projectile->getPosition().y -(projectile->getContentSize().height/2),
        projectile->getContentSize().width,
        projectile->getContentSize().height);
        CCArray* targetsToDelete = new CCArray;
        
        CCARRAY_FOREACH(_targets, jt)
        {
            CCSprite *target = static_cast<CCSprite*>(jt);
            CCRect targetRect = CCRectMake(
            target->getPosition().x - (target->getContentSize().width/2),
            target->getPosition().y - (target->getContentSize().height/2),
            target->getContentSize().width,
            target->getContentSize().height);
            
            if(projectileRect.intersectsRect(targetRect))
               {
                   targetsToDelete->addObject(target);
               }
        }
예제 #16
0
void Recipe9Scene::update(float dt)
{
	//	うっほい君を取得
	CCSprite* player = (CCSprite*)this->getChildByTag(1);
	CCRect playerRect =
		CCRectMake(
		player->getPositionX() - (player->getContentSize().width / 4),
		player->getPositionY() - (player->getContentSize().height / 4),
		player->getContentSize().width / 2,
		player->getContentSize().height / 2
		);
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	//	ハンバーガー取得
	CCSprite* food = (CCSprite*)this->getChildByTag(2);
	if (food == NULL) return;
	CCRect foodRect =
		CCRectMake(
		food->getPositionX() - (food->getContentSize().width / 2),
		food->getPositionY() - (food->getContentSize().height / 2),
		food->getContentSize().width,
		food->getContentSize().height
		);
	//	衝突判定
	if (playerRect.intersectsRect(foodRect))
	{
		this->removeChild(food, true);
		///	うっほい君のイメージ変更
		player->setTexture(CCTextureCache::sharedTextureCache()->addImage("monkey02.png"));
		///	01秒後にeatを1度だけ実行させる
		this->scheduleOnce(schedule_selector(Recipe9Scene::eat), 0.1f);
	}
}
예제 #17
0
void ItemLadderLR::collision()
{
	CCRect rcMario = _mario->boundingBox();
	CCRect rcItem = boundingBox();

	if (rcItem.intersectsRect(rcMario))
	{
		// 脑袋撞上了
		if (_mario->_speedUp > 0)
		{
			_mario->_speedDown = 10;
			_mario->_speedUp = 0;
		}
		else
		{
			// 刚落地
			if (_mario->_status ==Mario::FLY)
			{
				_mario->_onLadder = true;
				_marioOnThisLadder = true;
				_mario->setPositionY(rcItem.getMaxY());

			}
		}
	}
	else
	{
		if (_marioOnThisLadder)
		{
			_marioOnThisLadder = false;
			_mario->_onLadder = false;
		}
	}
	
}
예제 #18
0
bool MJHelper::isBeCovered(int layer, cocos2d::CCNode *node)
{
    CCPoint mjPoint = node->getPosition();
    CCSize mjsize = node->getContentSize();
    CCRect mjRect = CCRectMake(mjPoint.x, mjPoint.y, mjsize.width, mjsize.height);//得到要检测麻将的尺寸
    
    mjRect = getZoomRect(0.8, mjRect);
    
    std::string overLayerName;
    overLayerName = overLayerName + ISBeCovered + getstring(layer + 1);//检查将要移除的牌的上面一层
    
    GameState *gs = GameState::getInstance();
    CCArray *layerArr =(CCArray*)gs->getGlobalObject(overLayerName.c_str());
    
    bool covered = false;//没有被覆盖
    
    if (layerArr != NULL) {
        for (int arrNum = 0;arrNum < layerArr->count(); arrNum++)
        {
            CCNode *overNode = (CCNode*)layerArr->objectAtIndex(arrNum);
            CCPoint overPoint = overNode->getPosition();
            CCSize overSize = overNode->getContentSize();
            CCRect overRect = CCRectMake(overPoint.x, overPoint.y, overSize.width, overSize.height);
            
            overRect = getZoomRect(0.8, overRect);
            
            if (mjRect.intersectsRect(overRect)) {
                covered = true;//被覆盖了
            };
        }
    }
    
    return covered;
    
}
예제 #19
0
파일: CtrlEdit.cpp 프로젝트: twkevin/ddz
void KeyboardNotificationLayer::keyboardWillShow(CCIMEKeyboardNotificationInfo& info)
{
	if (! m_pTrackNode)
	{
		return;
	}

	CCRect rectTracked = ccGetRect(m_pTrackNode);
	// if the keyboard area doesn't intersect with the tracking node area, nothing need to do.
	if (! rectTracked.intersectsRect(info.end))
	{
		return;
	}

	// assume keyboard at the bottom of screen, calculate the vertical adjustment.
	float adjustVert = info.end.getMaxY() - rectTracked.getMinY();
	CCLOG("TextInputTest:needAdjustVerticalPosition(%f)", adjustVert);

	// move all the children node of KeyboardNotificationLayer
	/*CCArray * children = getChildren();
	CCNode * node = 0;
	int count = children->count();
	CCPoint pos;
	for (int i = 0; i < count; ++i)
	{
		node = (CCNode*)children->objectAtIndex(i);
		pos = node->getPosition();
		pos.y += adjustVert;
		node->setPosition(pos);
	}*/
}
void ProjectileController::update(float delta)
{
    CCComponent *com = m_pOwner->getParent()->getComponent("SceneController");
    cocos2d::CCArray *_targets = ((SceneController*)com)->getTargets();

    CCSprite *projectile = dynamic_cast<CCSprite*>(m_pOwner);
    CCRect projectileRect = CCRectMake(
                                projectile->getPosition().x - (projectile->getContentSize().width/2),
                                projectile->getPosition().y - (projectile->getContentSize().height/2),
                                projectile->getContentSize().width,
                                projectile->getContentSize().height);

    CCArray* targetsToDelete =new CCArray;
    CCObject* jt = NULL;
    CCARRAY_FOREACH(_targets, jt)
    {
        CCSprite *target = dynamic_cast<CCSprite*>(jt);
        CCRect targetRect = CCRectMake(
                                target->getPosition().x - (target->getContentSize().width/2),
                                target->getPosition().y - (target->getContentSize().height/2),
                                target->getContentSize().width,
                                target->getContentSize().height);

        // if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
        if (projectileRect.intersectsRect(targetRect))
        {
            targetsToDelete->addObject(target);
        }
    }
예제 #21
0
void CAListView::recoveryCollectionCell()
{
	CCRect rect = this->getBounds();
	rect.origin = getContentOffset();
    rect.origin.y -= rect.size.height * 0.1f;
    rect.size.height *= 1.2f;
    
	std::map<unsigned int, CAListViewCell*>::iterator itr;
	for (itr = m_pUsedListCells.begin(); itr != m_pUsedListCells.end(); itr++)
	{
		CAListViewCell* cell = itr->second;
		CC_CONTINUE_IF(cell == NULL);

		CCRect cellRect = cell->getFrame();
		CC_CONTINUE_IF(rect.intersectsRect(cellRect));

		m_pFreedListCells[cell->getReuseIdentifier()].pushBack(cell);
		cell->removeFromSuperview();
		cell->resetListViewCell();
		itr->second = NULL;
        
        
        CAView* line = m_pUsedLines[itr->first];
        CC_CONTINUE_IF(line == NULL);
        m_pFreedLines.pushBack(line);
        line->removeFromSuperview();
        m_pUsedLines[itr->first] = NULL;
	}
}
예제 #22
0
void HelloWorld::doSimpleBulletHitDetect(){
		// Asteroids
		CCArray *simpleBulletsToDelete = new CCArray;
		CCObject *isAsteroid = NULL, *isSimpleBullet = NULL;
		CCARRAY_FOREACH( _asteroids, isAsteroid ){
			CCSprite * asteroid = dynamic_cast<CCSprite*>(isAsteroid);
			if( asteroid->isVisible() ){
						CCRect asteroidRect = CCRectMake(
							asteroid->getPosition().x - (asteroid->getContentSize().width/2),
							asteroid->getPosition().y - (asteroid->getContentSize().height/2),
							asteroid->getContentSize().width,
							asteroid->getContentSize().height);

						//CCArray* lasersToDelete = new CCArray;
						CCARRAY_FOREACH( m_simpleBullets, isSimpleBullet) {
							CCSprite * simpleBullet = dynamic_cast<CCSprite*>(isSimpleBullet);
							if( simpleBullet->isVisible() ){
								CCRect laserRect = CCRectMake(
									simpleBullet->getPosition().x - (simpleBullet->getContentSize().width/2),
									simpleBullet->getPosition().y - (simpleBullet->getContentSize().height/2),
									simpleBullet->getContentSize().width,
									simpleBullet->getContentSize().height);

								if( asteroidRect.intersectsRect(laserRect)){
									simpleBulletsToDelete->addObject(simpleBullet);
									freeAsteroid(asteroid);
									freeSimpleBullet(simpleBullet);
								}
							}
						}//done going through lasers, delete lasersToDelete
void HelloSimple::updateGame(float dt)
{
	CCArray *projectilesToDelete = new CCArray;
    CCObject* it = NULL;
    CCObject* jt = NULL;

	// for (it = _projectiles->begin(); it != _projectiles->end(); it++)
    CCARRAY_FOREACH(_projectiles, it)
	{
		CCSprite *projectile = dynamic_cast<CCSprite*>(it);
		CCRect projectileRect = CCRectMake(
			projectile->getPosition().x - (projectile->getContentSize().width/2),
			projectile->getPosition().y - (projectile->getContentSize().height/2),
			projectile->getContentSize().width,
			projectile->getContentSize().height);

		CCArray* targetsToDelete =new CCArray;

		// for (jt = _targets->begin(); jt != _targets->end(); jt++)
        CCARRAY_FOREACH(_targets, jt)
		{
			CCSprite *target = dynamic_cast<CCSprite*>(jt);
			CCRect targetRect = CCRectMake(
				target->getPosition().x - (target->getContentSize().width/2),
				target->getPosition().y - (target->getContentSize().height/2),
				target->getContentSize().width,
				target->getContentSize().height);

			// if (CCRect::CCRectIntersectsRect(projectileRect, targetRect))
            if (projectileRect.intersectsRect(targetRect))
			{
				targetsToDelete->addObject(target);
			}
		}
예제 #24
0
void Obstacle::Update(float dt)
{
    
    if (obstacleSprite)
    {
        
        if (destroyAll)
        {
            obstacleSprite->removeFromParentAndCleanup(true);
            obstacleSprite = NULL;
            return;
        }
        
        CCRect obstacleRect;
        if (obstacleType != crow)
        {
            obstacleSprite->setPosition(ccp(obstacleSprite->getPosition().x + pathSpeed , obstacleSprite->getPosition().y));
        
            obstacleRect = CCRectMake(obstacleSprite->getPosition().x - obstacleSprite->getContentSize().width * 0.35f,
                                         obstacleSprite->getPosition().y ,
                                         obstacleSprite->getContentSize().width * 0.6f,
                                         obstacleSprite->getContentSize().height * 0.7f);
            
            if (obstacleSprite->getPosition().x < -obstacleSprite->getContentSize().width)
            {
                removeMe = true;
                checkCollisions = false;
            }
        }
        else
        {
            obstacleSprite->setPosition(ccp(obstacleSprite->getPosition().x + pathSpeed , obstacleSprite->getPosition().y));
            if (obstacleSprite->getPosition().x < winSize.width - obstacleSprite->getContentSize().width && moveOnce)
            {
                MoveCrow();
                moveOnce = false;
            }
            obstacleRect = CCRectMake(obstacleSprite->getPosition().x - obstacleSprite->getContentSize().width * 0.35f,
                                      obstacleSprite->getPosition().y - obstacleSprite->getContentSize().height * 0.2f,
                                      obstacleSprite->getContentSize().width * 0.6f,
                                      obstacleSprite->getContentSize().height * 0.5f);
        }
        //obstacleBoundingBox->DestroyAll();
        //obstacleBoundingBox->appendRect(obstacleRect);
        if (obstacleRect.intersectsRect(playerRect) && checkCollisions)
        {
            if (playerCurrentState == rollState && obstacleType == crow)
            {
                return;
            }
           
            checkCollisions = false;
            playerCurrentState = gotHitState;
            //RemoveObstacle();
            removeMe = true;
            CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("Audio/Explosion.wav");
        }
    }
}
//-------------------------------------------------------------------------
// 碰撞检测
// 参数:rt 检测矩形
// 返回值:true 表示碰撞,false 表示未碰撞
bool FKFrameSpriteObj::HitTest( const CCRect& rt )
{
	if( m_pSprite == NULL )
		return false;

	CCRect rect = m_pSprite->boundingBox();
	return rect.intersectsRect( rect );
}
예제 #26
0
CollisionType Obstacle::checkCollisionWithPlayer(CCSprite *sprite, const CCPoint& point)
{
    CollisionType type = CollisionTypeNone;
    
    CCRect bottomPipeRect = CCRectMake(
                                       point.x,
                                       point.y,
                                       _bottomPipe->getContentSize().width/2,
                                       _bottomPipe->getPositionY());
    
    CCRect topPipeRect = CCRectMake(
                                       point.x,
                                       _topPipe->getPositionY(),
                                       _topPipe->getContentSize().width/2,
                                       _screenSize.height);
    
    CCRect goalRect = CCRectMake(
                                       point.x,
                                       point.y,
                                       _goal->getContentSize().width,
                                        VisibleRect::getVisibleRect().size.height);
    
    
    CCRect spriteBoundingBox = CCRectMake(sprite->getPositionX() - sprite->getContentSize().width/4,
                                          sprite->getPositionY() - sprite->getContentSize().height/4,
                                          sprite->getContentSize().width/2,
                                          sprite->getContentSize().height/2);
    
    if (bottomPipeRect.intersectsRect(spriteBoundingBox))
    {
        type = CollisionTypePipe;
    }
    else if (topPipeRect.intersectsRect(spriteBoundingBox))
    {
        type = CollisionTypePipe;
    }
    else if(goalRect.intersectsRect(spriteBoundingBox))
    {
        type = CollisionTypeGoal;
    }
    
    return type;
}
예제 #27
0
bool CCScrollView::isNodeVisible(CCNode* node)
{
    const CCPoint offset = this->getContentOffset();
    const CCSize  size   = this->getViewSize();
    const float   scale  = this->getZoomScale();

    CCRect viewRect;

    viewRect = CCRectMake(-offset.x/scale, -offset.y/scale, size.width/scale, size.height/scale);

    return viewRect.intersectsRect(node->boundingBox());
}
예제 #28
0
void ItemTortoiseFly::collision()
{
	if (_bGodMode || _mario->_status == Mario::DEAD || _mario->_bGodMode)
	{
		return;
	}

	CCRect rcItem = boundingBox();
	CCRect rcMario = _mario->boundingBox();
	if (rcItem.intersectsRect(rcMario))
	{
		if (_status == NORMAL)
		{
			if (_mario->_status == Mario::FLY && _mario->_speedUp <= 0 && rcMario.getMinY() > rcItem.getMaxY() - rcItem.size.height / 2)
			{
				_status = DROPPING;
				updateStatus();
			}
			else
			{
				_mario->die(false);
			}
		}
		else if (_status == DROPPING)
		{
			// ... nothing to do
			// 设计中,乌龟下降的速度应该大于mario下降的速度,所以不应该有碰撞
		}
		else if (_status == ONLAND)
		{
			if (_mario->_status == Mario::FLY && _mario->_speedUp <= 0 && rcMario.getMinY() > rcItem.getMaxY() - rcItem.size.height / 2)
			{
				_status = SLEEP;
				updateStatus();
			}
			else
			{
				_mario->die(false);
			}
		}
		else if (_status == SLEEP)
		{
			if (_bGodMode) return;

			_status = CRAZY;
			updateStatus();
		}
	}
}
예제 #29
0
bool CCScrollView::isNodeVisible(CCNode* node)
{
    const CCPoint offset = this->getContentOffset();
    const CCSize  size   = this->getViewSize();
    const float   scale  = this->getZoomScale();
    
    CCRect viewRect;
    
    viewRect = CCRectMake(-offset.x/scale, -offset.y/scale, size.width/scale, size.height/scale); 
    
    CCRect nodeBound = CCRectMake(0, 0, node->getContentSize().width, node->getContentSize().height);
    CCAffineTransform t = node->nodeToAncestorTransform(getContainer());
    nodeBound = CCRectApplyAffineTransform(nodeBound, t);
    return viewRect.intersectsRect(nodeBound);
}
예제 #30
0
void CAListView::loadCollectionCell()
{
	CCRect rect = this->getBounds();
	rect.origin = getContentOffset();
    rect.origin.y -= rect.size.height * 0.1f;
    rect.size.height *= 1.2f;
    
	std::map<unsigned int, CAListViewCell*>::iterator itr;
	for (itr = m_mpUsedListCells.begin(); itr != m_mpUsedListCells.end(); itr++)
	{
		CC_CONTINUE_IF(itr->second != NULL);

		unsigned int index = itr->first;
		CCRect cellRect = m_rIndexRects[index];
		CC_CONTINUE_IF(!rect.intersectsRect(cellRect));

		CAListViewCell* cell = m_pListViewDataSource->listViewCellAtIndex(this, cellRect.size, index);
		if (cell)
		{
            cell->m_nIndex = index;
            cell->updateDisplayedAlpha(this->getAlpha());
			addSubview(cell);
			cell->setFrame(cellRect);
            m_mpUsedListCells[index] = cell;
            m_vpUsedListCells.pushBack(cell);
		}

		if (m_pSelectedListCells.count(index))
		{
			cell->setControlStateSelected();
		}
        
        if (m_pListViewDataSource)
        {
            m_pListViewDataSource->listViewWillDisplayCellAtIndex(this, cell, index);
        }
        
        CAView* view = this->dequeueReusableLine();
        CCRect lineRect = m_rLineRects[index];
        if (view == NULL)
        {
            view = CAView::createWithFrame(lineRect, m_obSeparatorColor);
        }
        m_pUsedLines[index] = view;
        this->insertSubview(view, 1);
		view->setFrame(lineRect);
	}
}