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))));
            }
        }
    }
 void CCParallaxScrollNode::updateWithYPosition(float y, float dt)
 {
     for (int i=scrollOffsets->num - 1; i >= 0; i--) {
         CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*)scrollOffsets->arr[i];
         CCNode *child = scrollOffset->getChild();
         float offset = y * scrollOffset->getRatio().y;//ccpCompMult(pos, scrollOffset.ratio);
         child->setPosition(ccp(child->getPosition().x, scrollOffset->getOrigPosition().y + offset));
     }
 }
void CCParallaxScrollNode::updateWithYPosition(float y, float dt)
{
  for (int i = 0; i < _scrollOffsets.size(); i ++)
  {
    CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*) _scrollOffsets.at(i);

		Node *child = scrollOffset->getTheChild();
		float offset = y * scrollOffset->getRatio().y;
		child->setPosition(Point(child->getPosition().x, scrollOffset->getOrigPosition().y + offset));
	}
}
 void CCParallaxScrollNode::removeChild(CCSprite *node, bool cleanup)
 {
     for (int i=0; i < scrollOffsets->num; i++) {
         CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*)scrollOffsets->arr[i];
         if(scrollOffset->getChild()->isEqual(node)) {
             ccArrayRemoveObjectAtIndex(scrollOffsets, i);
             break;
         }
     }
     if (batch) {
         batch->removeChild(node, cleanup);
     }
 }
void CCParallaxScrollNode::removeChild(Sprite *node, bool cleanup)
{
  cocos2d::Vector<CCParallaxScrollOffset *> tobeDeleted;

  for (int i = 0; i < _scrollOffsets.size(); i ++)
  {
    CCParallaxScrollOffset *scrollOffset = (CCParallaxScrollOffset*) _scrollOffsets.at(i);

		if( scrollOffset->getTheChild() == node){
			tobeDeleted.pushBack(scrollOffset);
			break;
		}
	}

  for (int i = 0; i < tobeDeleted.size(); i ++)
  {
    _scrollOffsets.eraseObject(tobeDeleted.at(i));
  }
}
// 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))));
		}
	}
CCParallaxScrollOffset* CCParallaxScrollOffset::scrollWithNode(CCNode *node, CCPoint r, CCPoint p, CCPoint s, CCPoint v){
    CCParallaxScrollOffset *offset = CCParallaxScrollOffset::create();
	return (CCParallaxScrollOffset*)offset->initWithNode(node,r,p,s,v);
}