Forum > Extensions > UIScrollView solution for cocos2d-x

UIScrollView solution for cocos2d-x

By leo2 Posted 2011-04-03 10:52 Comments 48
Melchior16
  • Posts: 10

#26 RE: 2012-04-20 09:32

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 !

Melchior16
  • Posts: 10

#27 RE: 2012-04-27 12:36

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!

feiting_schatt
  • Posts: 11

#28 RE: 2012-05-02 18:06

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:

CCScrollLayer.h - Corrected headers (5.8 kB)

feiting_schatt
  • Posts: 11

#29 RE: 2012-05-02 18:06

and cpp

Melchior16
  • Posts: 10

#30 RE: 2012-05-03 10:13

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

feiting_schatt
  • Posts: 11

#31 RE: 2012-05-03 17:16

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 :D

leo2
  • Posts: 21

#32 RE: 2012-05-03 17:39

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.

feiting_schatt
  • Posts: 11

#33 RE: 2012-05-03 23:02

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);
}

@

Melchior16
  • Posts: 10

#34 RE: 2012-05-04 09:14

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

feiting_schatt
  • Posts: 11

#35 RE: 2012-05-04 17:31

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.

Melchior16
  • Posts: 10

#36 RE: 2012-06-13 15:22

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 ^^!

blurg
  • Posts: 28

#37 RE: 2012-06-21 13:20

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.

ttvu90
  • Posts: 5

#38 RE: 2012-08-16 10:47

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)

elr0nd
  • Posts: 43

#39 RE: 2012-09-21 07:07

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[]()

umni4ek
  • Posts: 54

#40 RE: 2012-09-21 07:15

Hi, Emilio, here examples of our vertical scroll.

elr0nd
  • Posts: 43

#41 RE: 2012-09-21 07:27

WOW[]()!

Thanks for your quickly answer[]()!

Sure it will help me.

aparajita
  • Posts: 25

#42 RE: 2012-09-24 13:04

Hi,

I am using cocos2d-x with html5. I have lot of text added to a layer. I want that text to be within layer boundaries and layer should have scrolling option to view other text.I want to use CCScrollLayer class. How can I add this CCScrollLayer.h file in my application? or I can say how can i load this .h file through cocos2d.js file? Please let me know the solution.

Regards,
Aparajita

aparajita
  • Posts: 25

#43 RE: 2012-09-29 00:27

Hi,

I am using cocos2dx-html5 for my project. I want scrolling option for my layer. Can any of you please suggest me the solution. The above solution will not work for me. becoz of header file i cannot include in my project, As I am developing on only js files. please let me know the solution as fast as posible.

Thanks in advance. Waiting for solution.

rebelM
  • Posts: 1

#44 RE: 2012-11-23 15:05

hi there.
nice work you did there - but what’s up with the copyright in your headers?
Kind regards

festival
  • Posts: 119
  • Location: Germany

#45 RE: 2012-12-01 11:18

Updated this scroll layer for Cocos2d-x V2.x - cleaned up some stuff too. Enjoy!

It works, ship it!

AdiFly
  • Posts: 71

#46 RE: 2013-02-27 08:49

Hello

I’m trying to use the CCScrollView, but I don’t understand the way I need to set the content size and the position.
Can you please point me to an example of CCScrollView ?

Thanks!
Adrian

bomzj
  • Posts: 14
  • Location: Belarus, Minsk

#47 RE: 2013-03-05 16:51

Adi Fly wrote:

Hello
>
I’m trying to use the CCScrollView, but I don’t understand the way I need to set the content size and the position.
Can you please point me to an example of CCScrollView ?
>
Thanks!
Adrian

@ var container = new cc.Layer.create();
var logo = new cc.Sprite.create(“image file to test”);
logo.setPosition(new cc.Point(winSize.width/2, winSize.height/2));
container.addChild(logo);@
@
var scrollView = new cc.ScrollView.create(new cc.Size(800, 480), container);
scrollView.setContentSize(new cc.Size(800, 480));
scrollView.setPosition(0, 0);
scrollView.setContentOffset(new cc.Point(0,0));
scrollView.setDirection(cc.SCROLLVIEW_DIRECTION_HORIZONTAL);
this.addChild(scrollView);@

Try to increase/decrease parameters (x coordinate) in cc.ScrollView.create(new cc.Size(800, 480), container) and/or scrollView.setContentSize(new cc.Size(800, 480)) and you will see how it easily works.

hayashi_leo
  • Posts: 4

#48 RE: 2013-12-07 20:23

I added pageIndicators to your work. It works very well with iPhone and Android.

void CCScrollLayer::visit()
{
CCLayer::visit();
CCLog(“visit() d of d”, currentScreen, totalScreens);

// Prepare Points Array
float n = (float) totalScreens;
float pY = m_tPagesIndicatorPosition.y; // < Point y-coord in parent coord sys.
float d = 32.0f; // < Distance between points.
CCPoint* points = new CCPoint[totalScreens];
for (int i = 0; i < totalScreens; +*i)
{
float pX = m_tPagesIndicatorPosition.x* d * ((float)i - 0.5f*(n-1.0f));
points[i] = ccp(pX, pY);
}

// Set GL Values
ccGLEnable(CC_GL_ALL);
ccPointSize(12.0f * CC_CONTENT_SCALE_FACTOR());

#define DRAW_4B_FUNC ccDrawColor4B

glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

// Draw Gray Points
DRAW_4B_FUNC(0x96,0x96,0x96,0xFF);

ccDrawPoints( points, totalScreens );

// Draw White Point for Selected Page
DRAW_4B_FUNC(0xFF,0xFF,0xFF,0xFF);

ccDrawPoint(points[currentScreen-1]);

// Restore GL Values
ccPointSize(1.0f);

delete [] points;
}

Copyright © 2010 - 2013 Cocos2d-x.orgClustrmaps