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 !
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;>addObject(b);
}
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
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
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;>convertToGL;
prevPoint = CCDirector::sharedDirector
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;>setPosition(currentPos);
}
this
}
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;>runAction(lastMove);
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
}
@
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 ā¦ 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.
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