cocos2d-xcocos2d-js

Cocos2dx - js - ScrollView - Unable to position it


var size = cc.winSize;
    var scrollView = new ccui.ScrollView();
    scrollView.setDirection(ccui.ScrollView.DIR_VERTICAL);
    scrollView.setTouchEnabled(true);
    scrollView.setBounceEnabled(true);
    scrollView.setBackGroundColorType(ccui.Layout.BG_COLOR_SOLID);
    scrollView.setBackGroundColor(cc.color(255,255,255));
    //scrollView.setBackGroundImageScale9Enabled(true);
    scrollView.setContentSize(cc.size(720, 1280));
    scrollView.setInnerContainerSize(cc.size(720, (1280*2)));
    scrollView.setAnchorPoint(cc.p(0.5, 0.5));
    scrollView.setPosition(cc.p(360, 640));
    this.addChild(scrollView);

The position of the button starts in the middle.

enter image description here

        scrollView.setPosition(cc.p(360, 0));

if i change the y position, it gives different look.

enter image description here


Solution

  • I think you are making the same mistake that I made when I first started working with cocos2d-x. I was under the assumption that when you change the anchor point of an object it affects the positioning of its children as well, however, this is not the case with cocos2d-x. Changing the anchor position only affects the object itself. Its children are always positioned from the bottom-left corner of the object.

    So if you were to leave the scroll views anchor point at (0,0) and its position at (0,0) you would get the exact same result.

    Also, I should point out a few things about scroll views.

    Hope this helps!