UIScrollView solution for cocos2d-x

Simply add the created CCScrollableButton to your scrollable layer and it will work.
If the user tap the button it will run the callback function, if the user will drag the button, the whole layer will be scrolled.

Okay thanks a lot! I use the scroll layer class and itā€™s work very well ;)!
I will try to include the button !

You welcome! You may try my game as a thanks!
http://www.roymam.com/rollypoly

well, i would like to make a scrollable area, and when i scroll that area, all that is out of this area is not visible, how can I do that ?

best regards

The only way to do this with the current implementation is to add a top layer above the scrollable layer that hides everything beside that area, maybe using a png with a transparent box within it.

I used this solution in my project ! I changed some strings =) very usefull, thanks !

I found another way to do what i wanted, i used the GL_SCISSOR_TEST in the function called visit which is called when i want to draw my ScrollLayer. Like that :

void CCScrollLayer::visit()
{
screenSize = CCDirector::sharedDirector()->getWinSize();
glEnable(GL_SCISSOR_TEST);
cocos2d::CCDirector::sharedDirector()->getOpenGLView()->setScissorInPoints(100,100,831,831);
CCLayer::visit();
glDisable(GL_SCISSOR_TEST);
}

if it can help someone else !

Well now i put some menu item in a menu in the Scrollable layers, but when i begin the scrolling on this items, it doesnā€™t workā€¦ how can i make it to work ?

Best regards!

The above code has many problems, especially for C**. I created a more C** version, and fixed all the missing variables. I took away paging, but it should be easy to add back in as well.

Hereā€™s the header:

and cpp

Sorry but i tried your solution and that doesnā€™t workā€¦ may have i did a mistake, but when i created layers, they all overlap ā€¦

Did someone found a solution for my previous problem ? ->
ā€œWell now i put some menu item in a menu in the Scrollable layers, but when i begin the scrolling on this items, it doesnā€™t workā€¦ how can i make it to work ?ā€

Thanks

One more problem!

Create the onExit function and comment out this line from the destructor, and move it to that (otherwise the destructor is not called):

void CCScrollLayer::onExit()
{
CCTouchDispatcher::sharedDispatcher()>removeDelegate;
}
The destructor is not called until itā€™s removed as a delegate ā€¦ except that destructors arenā€™t a part of iOs native.
Valentin, stop using CCMenuItems and use the CCScrollLayerButton. Menu Items do not work, I learned that the hard way as well.
If you do not do the fix above, the scroll view will continue taking touches.
And remember to set the maximum scroll height after creating the scroll layer:
Hereā€™s a snippet:
Also, itemsArray is used like this:
CCArray itemsArray = CCArray::array;
CCScrollLayerButton * b = CCScrollLayerButton::buttonWithFile, targetScene, menu_selector );
//Remember to set the position and other things
itemsArray
>addObject(b);

CCScrollLayer * scroller = CCScrollLayer::nodeWithLayers(itemsArray, 0);
scroller~~>setMaximumScrollHeight; //Look at my code, I automatically subtract the height of the screen to make this behave more like iPhone.
scroller~~>setPosition(ccp(x,y));

Thatā€™s an almost fully-working example.

Also, you can add things other than ccScrollLayerButtons, but theyā€™re the only things that directly respond to input.

Good luck :smiley:

Thanks for all the feedback and fixes.
Iā€™ll try to integrate all of those fixes into my code and republish it on this post.

Hereā€™s a bouncing one, it could use some tweaking to make it easier to get natural, itā€™s based on distances and time helpers. (note, this is just all the touches methods for the ccScrollView)

If youā€™re not using pages, feel free to paste this ver:

@
bool CCScrollLayer::ccTouchBegan(CCTouch touch, CCEventwithEvent)
{
return true;
}

void CCScrollLayer::ccTouchMoved(CCTouch touch, CCEventwithEvent)
{
CCPoint touchPoint = touch~~>locationInView;
CCPoint prevPoint = touch~~>previousLocationInView();

touchPoint = CCDirector::sharedDirector()>convertToGL;
prevPoint = CCDirector::sharedDirector
>convertToGL;
CCPoint difference = ccp;
CCPoint currentPos = this~~>getPosition;
currentPos = ccp;
if
{
currentPos.y~~= difference.y/2.0f;// + currentPos.y;
}
else if (currentPos.y < 0)
{
currentPos.y = difference.y/2.0f;
}
this
>setPosition(currentPos);
}

void CCScrollLayer::ccTouchEnded(CCTouch * touch, CCEvent * withEvent)
{
if (this~~>getPositionY < 0)
{
this~~>bounceToPosition(ccp(this~~>getPositionX, 0));
}
else if > maximumScrollHeight)
{
this~~>bounceToPosition(ccp(this~~>getPositionX, maximumScrollHeight));
}
else {
CCPoint touchPoint = touch~~>locationInView();
CCPoint prevPoint = touch~~>previousLocationInView;
touchPoint = CCDirector::sharedDirector~~>convertToGL;
prevPoint = CCDirector::sharedDirector~~>convertToGL;
CCPoint difference = ccp;
CCPoint currentPos = this~~>getPosition;
float multiplier = 7;
float ease = 2;
float moveToY = multiplier*difference.y+currentPos.y;
if {
moveToY = 0;
}
else if {
moveToY = maximumScrollHeight;
}
//float timeMultiplier=1;
float halfScreenHeight = CCDirector::sharedDirector~~>getWinSize.height/2.0f;
float timeMultiplier = fabsf/halfScreenHeight*7;
CCAction * move = CCMoveTo::actionWithDuration );
CCEaseOut * easeOut = CCEaseOut::actionWithActionmove, ease);
this~~>runAction;
}
}
void CCScrollLayer::ccTouchCancelled
{
this~~>ccTouchEnded;
}
void CCScrollLayer::onExit
{
CCTouchDispatcher::sharedDispatcher~~>removeDelegate;
}
void CCScrollLayer::bounceToPosition
{
float dY =maximumScrollHeight~~ this~~>getPositionY();

if (toPos.y <= 0) {
dY = this>getPositionY();
}
dY /= 3.0f;

float timeMultiplier = 3.4f;
float halfScreenHeight = CCDirector::sharedDirector()>getWinSize.height/2.0f;
timeMultiplier = fabsf/halfScreenHeight*timeMultiplier;
float time1 = 0.09 * timeMultiplier;
float firstEase = 3;
float time2 = 0.13 * timeMultiplier;
float time3 = 0.11 * timeMultiplier;
float lastEase = 1;
CCAction * move = CCMoveTo::actionWithDuration, toPos.y));
CCEaseOut * easeOut = CCEaseOut::actionWithActionmove, firstEase);
// CCJumpTo * jump = CCJumpTo::actionWithDuration, dY, 1);
move = CCMoveTo::actionWithDuration );
CCAction * move2 = CCMoveTo::actionWithDuration;
CCEaseOut * lastEaseAction = CCEaseOut::actionWithActionmove2, lastEase);
CCAction * lastMove;
lastMove = CCSequence::actionseaseOut, move, lastEaseAction, NULL);
this
>runAction(lastMove);
}

@

Iā€™m sorry but I tried replacing the ā€œmenu itemā€ by the CCScrollLayerButton by adding many scrollLayerButton in a single layer, and I have a lot of problem and I do not see how ā€¦

I want to do a function that loads all ScrollButton I need and loads it into a CCScrollLayer but I can not do it ā€¦ Can you help me please ā€¦ itā€™s urgent ā€¦

Thanks

Valentin, Iā€™m guessing youā€™re still adding them to a CCMenu? The weird part about the scroll layer is, you do not add a ccMneu, only use the buttons.

So normally you would do this (to something else)
CCMenuItem * i = ā€¦.
CCMenu * menu = ā€¦MenuWithItem(i);
this->addChild(menu);

BUT in the scrollLayer you just do this:

CCScrollLayerButton * sButton = ā€¦
CCArray * a = CCArray::arrayWithItem(sButton)
CCScrollLayer * l = ā€¦withLayers(a, 0 );

Aside from adding the ā€œreal codeā€, thatā€™s all you need. If your input isnā€™t working, something else is wrong. If you know Cocos2d, then it should be easy. Try learning Cocos2D and itā€™ll give you examples of how to properly add/remove these things.

Also, iOS memory management rules apply too, seriously learn those and basic Cocos2D and this should be easy to do. I donā€™t see why youā€™re using the scissorTest right now.

Make sure your memory is being handled right. And ensure everything is in the correct spot ccp(0,0) is the lower-left.

Hi, i come back a little lateā€¦ but I donā€™t have the function ā€œCCArray *a = CCArray::arrayWithItem(sButton) ā€¦ so how can i do that ?
I got arrayWithArray, arrayWithCapacity, initWithArray or initWithCapacity but not arrayWithItem :confused: ā€¦ please help me
Thanks
PS: Iā€™m trainee and iā€™m on a Cocos2D-X project which use theā€scissorTest" so i have to learn many Cocos class very quickly ā€¦ thatā€™s why i need help ^^!

Great classes, thanks !
Is there a way to use sprite frameā€™s images as CCScrollLayerButtons ? I tried to write a initWithSpriteFrameName function in CCScrollLayerButton but some weird things happens with the selector.

Well now i put some menu item in a menu in the Scrollable layers, but when i begin the scrolling on this items, it doesnā€™t workā€¦ how can i make it to work ?

i have a solution. when touchmove, you send to CCDirector view a event ā€œtouchesEndedā€. In my case, itā€™s true.

sorry for my english. :frowning:

contact with me if you need (ttvu90@gmail.com)

Hello!

I have a problem using these classes, I hope someone can help me, I spent two days with the issue and I have been not able to get it working.

Let me explain the case, I am using these classes to show a list of users with a vertical scroll, it appears in the screen and it can be moved up and down, but the problem is that the visible space is a very small region in the screen and the list of users is showed from the last element so I need to scroll down the list to see the first elementā€¦

Does anyone have a example with a vertical scroll? I found some examples using this class but every of them is with a horizontal scrollā€¦

Thanks in advance