Example #1
0
void CCMenuItemSprite::setAnchorPoint(cocos2d::CCPoint const & anchorPoint) {
    CCMenuItem::setAnchorPoint(anchorPoint);
    if (m_pNormalImage) {
        m_pNormalImage->setAnchorPoint(anchorPoint);
        m_pNormalImage->setPosition(ccpCompMult(ccpFromSize(this->getContentSize()), this->getAnchorPoint()));
    }
    if (m_pSelectedImage) {
        m_pSelectedImage->setAnchorPoint(anchorPoint);
        m_pSelectedImage->setPosition(ccpCompMult(ccpFromSize(this->getContentSize()), this->getAnchorPoint()));
    }
    if (m_pDisabledImage) {
        m_pDisabledImage->setAnchorPoint(anchorPoint);
        m_pDisabledImage->setPosition(ccpCompMult(ccpFromSize(this->getContentSize()), this->getAnchorPoint()));
    }
}
// Used with box2d style velocity (m/s where m = 32 pixels), but box2d is not required
void CCParallaxScrollNode::updateWithVelocity(Point vel, float dt)
{
	vel = vel * PTM_RATIO;
  
  for (int i = 0; i < _scrollOffsets.size(); i ++)
  {
    CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*) _scrollOffsets.at(i);
		Point relVel = scrollOffset->getRelVelocity() * PTM_RATIO;
		Point totalVel = vel + relVel;
		Point offset = ccpCompMult((totalVel * dt), scrollOffset->getRatio());
    
    Node *child = scrollOffset->getTheChild();
		child->setPosition(child->getPosition() + offset);
		
		if ( (offset.x < 0 && child->getPosition().x + child->getContentSize().width < 0) ||
        (offset.x > 0 && child->getPosition().x > _range.width) ) {
			child->setPosition((child->getPosition() + Point(-SIGN(offset.x) * fabs(scrollOffset->getScrollOffset().x), 0)));
		}
		
		// Positive y indicates upward movement in cocos2d
		if ( (offset.y < 0 && child->getPosition().y + child->getContentSize().height < 0) ||
        (offset.y > 0 && child->getPosition().y > _range.height) ) {
			child->setPosition((child->getPosition() + Point(0, -SIGN(offset.y) * fabs(scrollOffset->getScrollOffset().y))));
		}
	}
}
// Used with box2d style velocity (m/s where m = 32 pixels), but box2d is not required
void CCParallaxScrollNode::updateWithVelocity(CCPoint vel, float dt)
{
	vel = ccpMult(vel, PTM_RATIO);
//	CCLog("count: %i", _scrollOffsets->count());
    CCObject* object;
    CCARRAY_FOREACH(_scrollOffsets, object)
    {
        CCParallaxScrollOffset* scrollOffset = dynamic_cast<CCParallaxScrollOffset*>(object); 
		CCPoint relVel = ccpMult(scrollOffset->getRelVelocity(), PTM_RATIO);
		CCPoint totalVel = ccpAdd(vel, relVel);
		CCPoint offset = ccpCompMult(ccpMult(totalVel, dt), scrollOffset->getRatio());
        
        CCNode *child = scrollOffset->getTheChild();
		child->setPosition(ccpAdd(child->getPosition(), offset));
		
		if ( (offset.x < 0 && child->getPosition().x + child->getContentSize().width < 0) ||
            (offset.x > 0 && child->getPosition().x > _range.width) ) {
			child->setPosition(ccpAdd(child->getPosition(), ccp(-SIGN(offset.x) * fabs(scrollOffset->getScrollOffset().x), 0)));
		}
		
		// Positive y indicates upward movement in cocos2d
		if ( (offset.y < 0 && child->getPosition().y + child->getContentSize().height < 0) ||
			(offset.y > 0 && child->getPosition().y > _range.height) ) {
			child->setPosition(ccpAdd(child->getPosition(), ccp(0, -SIGN(offset.y) * fabs(scrollOffset->getScrollOffset().y))));
		}
	}
    void CCParallaxScrollNode::updateWithVelocity(const CCPoint &vel, float dt) {
        CCSize screen = CCDirector::sharedDirector()->getWinSize();
        
        CCPoint vel2 = ccpMult(vel, PTM_RATIO);
        
        for (int i=scrollOffsets->num - 1; i >= 0; i--) {
            CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*)scrollOffsets->arr[i];
            CCNode *child = scrollOffset->getChild();
            
            CCPoint relVel = ccpMult(scrollOffset->getRelVelocity(), PTM_RATIO);
            CCPoint totalVel = ccpAdd(vel2, relVel);
            CCPoint tmp = ccpMult(totalVel, dt);
            CCPoint offset = ccpCompMult(tmp, scrollOffset->getRatio());

            
            child->setPosition(ccpAdd(child->getPosition(), offset));
            
            if ( (vel2.x < 0 && child->getPosition().x + child->getContentSize().width < 0) ||
                (vel2.x > 0 && child->getPosition().x > screen.width) ) {
                
                child->setPosition(ccpAdd(child->getPosition(), ccp(-SIGN(vel2.x) * fabs(scrollOffset->getScrollOffset().x), 0)));
            }
            
            // Positive y indicates upward movement in cocos2d
            if ( (vel2.y < 0 && child->getPosition().y + child->getContentSize().height < 0) ||
                (vel2.y > 0 && child->getPosition().y > screen.height) ) {
                child->setPosition(ccpAdd(child->getPosition(), ccp(0, -SIGN(vel2.y) * fabs(scrollOffset->getScrollOffset().y))));
            }
        }
    }
Example #5
0
void CCMenuItemSprite::setDisabledImage(CCNode* pImage)
{
    if (pImage != m_pNormalImage)
    {
        if (pImage)
        {
            addChild(pImage, 0, kDisableTag);
            pImage->setAnchorPoint(this->getAnchorPoint());
            pImage->setPosition(ccpCompMult(ccpFromSize(this->getContentSize()), this->getAnchorPoint()));
        }

        if (m_pDisabledImage)
        {
            removeChild(m_pDisabledImage, true);
        }

        m_pDisabledImage = pImage;
        this->updateImagesVisibility();
    }
}