Cocos2d iPhone. Scrollayer contentSize. Layers

845 views Asked by At

I want to set the contentsize of scrollayer.

I have a scrollayer, it's CCLayer type and moving is set by ccTouchMove. I have one schedule for smoothing. BUT.

Problem is that scrolling layer is big like the whole display. I want to set the contentsize of scrollayer. Content in this layer will be scroll and showing ONLY in this layer. Not taking up the whole display. Something like this

enter image description here

Scrolling just in gray CCLayer (scrollayer) ....NOT WHOLE SCREEN.

Can you help me?

P.S.: Setting CCLayerColor and initWithColor:Width:Height: is not working. It just makes some stupid color box and it's moving too.

2

There are 2 answers

0
YvesLeBorg On

ok, honestly i would put the window frame at a higher z than the scrolling object ... if you dont you may have to crop and change sprite on the fly for the window content, nasty (at least that is the one way i could do this, without further research).

so :

// initialize this logic somewhere useful

CCNode scrollableContent;
CCSprite windowFrame; 
BOOL isScrollPossible;
[self addChild:scrollableContent z:0];
[self addChild:windowFrame z:1];

// and in the touch delegate methods

-(void) ccTouchBegan:{
  check if the touch happened in the window, if yes, scrolling is possible
}
-(void) ccTouchMoved:{
  if (isScrollPossible) {

    compute displacement
    compute nextPosition for scrollableContent node;
    if ( nextPosition in window ) {
      // make scrollableContent follow touch
      scrollableContent.position=nextPosition;
    } else {
      // stop any further scrolling until the next 'touch began'
      isScrollPossible=NO; 
    }
  } else {
    // scroll not possible, do nothing
  }
}

This is the basic idea. You may need clamping logic to prevent the creeping of scrollableContent beyond the edges of the window.

Edited for typos.

0
James Webster On

After trying desperately with masking and GL_SCISSOR, I settled on cutting a hole in the foreground and only moving the background object when onTouchMoved updated.

To see it in action, have a look at the Stats page of Angry Gran.