4

I am currently using the CCScrollLayer (in the cocos2d extension classes) class to implement a menu system. It's working great but I would like to have other buttons on the screen and the scrollable area is the entire screen by default.

I tried messing with content size but no dice. After doing some reading, I found that the content size is set to the screen size per CCLayers behavior. A user suggested wrapping it in a CCNode and scaling, but this did not help.

Any suggestions or sample code? I'd have to think this should be possible.

Guru
  • 21,652
  • 10
  • 63
  • 102
Arbel
  • 425
  • 8
  • 26

3 Answers3

0

Thanks everyone for the help, unfortunately it seems you cannot change the size of a CCLayer (or the contentSize has no effect on touch events) without basically rewriting a lot of code.

However, I found a workaround that seems to be working. In the ccTouch events began and moved, I check my desired bounds after converting my UITouch to cocos2D space. If they are not in the my bounds I return, and in the case of move I manually call TouchEnded. TouchEnded also checks the initial touched point, and if it is was out of bounds it ignores it.

Things seems to be working as desired. I will post more information if I find it. Thanks again everyone.

Arbel
  • 425
  • 8
  • 26
0

CAScrollLayer is surprisingly simple, which may be confusing.

Just add the content layer to it:

[_scrollLayer addSublayer:_contentLayer];

To set the rect you want to be visible on the screen set bounds or frame of the scroll layer:

[_scrollLayer setBounds:visibleBounds];

Set the content's size respectively. The content's size can be bigger or smaller, it does not matter.

[_contentLayer setBounds:currentContextBounds];

If the content is bigger and you want to scroll to a certain point use scrollToPoint: or scrollToRect: methods of the scroll layer.

You need to implement your own scrolling indicators, bars, etc. if desired.

Davyd Geyl
  • 4,578
  • 1
  • 28
  • 35
  • 1
    This question is specifically for CCScrollLayer (the cocos2d extension class) I would use one of the UIKit classes if it didn't have severe performance hits in cocos2d. Sorry question was maybe a bit confusing. – Arbel Nov 04 '11 at 06:34
  • Sorry about that. I have not paid attention to the spelling. Unfortunately, I can't help you with cocos2d. – Davyd Geyl Nov 04 '11 at 06:48
0

Although I am using table view and not the scroll view, both share the same parent [scroll view doh]. Have you turned on clipToBounds after setting content size?

I would recommend using this constructor:

/**
 * Init this object with a given size to clip its content.
 *
 * @param size view size
 * @return initialized scroll view object
 */
- (id)initWithViewSize:(CGSize)size;
Krystian
  • 3,193
  • 2
  • 33
  • 71
  • Hmm I cant find clipToBounds property. CCScrollViews parent is CCLayer though which is a child of CCNode. – Arbel Nov 04 '11 at 12:18
  • Maybe Im not using the same CCScrollView cause it doesnt have that constructor. I am using one from the 3rd party extensions. https://github.com/cocos2d/cocos2d-iphone-extensions/tree/release-0.2 I think I used CCTableView in my old project but really had to hack a lot of stuff together, this one seems great (paging support, very smooth) but I cant set the content size which is maddening. – Arbel Nov 04 '11 at 13:19
  • Yeah, that's probably it. I am using this one: https://github.com/saim80/Cocos2D-Extensions and it works pretty fine. I know that on cocos2d forums some people changed the names to start with CC, that's why I thought you were using this one. It might be easy to implement clipping in the one you are using. I've created some scroll layers on my own, it's just a matter of adding onEnter, onExit and two [afair] more methods [copy & paste should be enough] – Krystian Nov 04 '11 at 13:26
  • Thanks. It looks like they have made a bunch of updates since I implemented in my game last year. Will give it a shot and see if it works well enough. Will post again later. – Arbel Nov 04 '11 at 13:40