Quantcast
Channel: Cocos中文社区 - 最新主题
Viewing all articles
Browse latest Browse all 88737

Cocos js 3.11版本的scrollview如何设置偏移量 setContentOffset报错

$
0
0

@251804974 写道:

下面是实际代码。。。moveToIndex里面调用setContentOffset()显示 TypeError: self.getViewSize is not a function,不知道什么原因,用self.setSize可以,用self.setViewSize不行。
//==========================使用实例================
//ui测试
function testui(){
preloadResPlist(plist_currencyButton);
var scene = new cc.Scene();

var reslist = ["currencyButton/2.png","currencyButton/2.png","currencyButton/2.png","currencyButton/2.png","currencyButton/2.png","currencyButton/2.png","currencyButton/2.png"];
var selectlist = ["currencyButton/4.png","currencyButton/4.png","currencyButton/4.png","currencyButton/4.png","currencyButton/4.png","currencyButton/4.png","currencyButton/4.png"];
var test = new testscrollview(reslist);
test.setBounceEnabled(true);
test.setSize(cc.size(720,300)).setDirection(ccui.ScrollView.DIR_VERTICAL).setSelectList(selectlist).createListView();
test.setPosition(cc.p(0,300));
test.moveToIndex(3);
scene.addChild(test);
cc.director.runScene(scene);

};

var testscrollview = ccui.ScrollView.extend({
//外部参数
reslist:null, //按钮资源列表
selectreslist:null, //选中资源列表
direction:null, //方向 水平从左到右 垂直从上到下 ccui.ScrollView.DIR_VERTICAL ccui.ScrollView.DIR_HORIZONTAL
click_callback:null, //点击回调
itemspace:0, //item间隔

//内部参数
itemlist:null,                  //item列表
selected:null,                  // 选中tag
currentselect:null,             //当前选中
itemsize:null,                  //item尺寸,包含宽高
innersize:null,                 //内部宽高
ctor : function(reslist,click_callback){
    this._super();
    var self = this;
    this.itemlist = [];

    this.setResList(reslist);
    this.setItemClickCallBack(click_callback);
},
onExist:function(){
    this._super();
},
createListView:function(){
    var self = this;
    var pos = cc.p(0,0);
    for(var i = 0; i < this.reslist.length; i++) {
        var item = new cc.Sprite(this.reslist[i]);

        var itemClick = function(sender,type){
            if (type != ccui.Widget.TOUCH_ENDED) { return; }
            if (self.currentselect.index == sender.index) {return;}
            self.currentselect = sender;
            self.selectIndex(sender.index);
        };
        var item = new ccui.Button();
        item.loadTextures(this.reslist[i],"", this.selectreslist[i],ccui.Widget.PLIST_TEXTURE);
        item.addTouchEventListener(itemClick);
        item.index = i;
        if (!self.currentselect) {self.currentselect = item;}

        if (!this.itemsize) {
            this.itemsize = cc.size(item.width,item.height);
        }
        if (!this.innersize){
            if (this.direction == ccui.ScrollView.DIR_VERTICAL) {
                this.innersize = cc.size(Math.max(this.itemsize.width,this.width),Math.max((this.itemsize.height+this.itemspace)*this.reslist.length-this.itemspace,this.height));
            } else {
                this.innersize = cc.size(Math.max((this.itemsize.width+this.itemspace)*this.reslist.length-this.itemspace,this.width),Math.max(this.itemsize.height,this.height));
            }
            cc.log("=======================size="+this.innersize.width);
            this.setInnerContainerSize(this.innersize);
        }
        if (this.direction == ccui.ScrollView.DIR_VERTICAL) {
            pos = cc.p(this.innersize.width/2,(this.innersize.height-(this.itemsize.height+this.itemspace)*i-this.itemsize.height/2));
        } else {
            pos = cc.p((this.itemsize.width+this.itemspace)*i+this.itemsize.width/2,this.innersize.height/2);
        }
        this.addChild(item);
        item.setPosition(pos); 
    }
    this.selectIndex(0);
},

selectIndex:function(index){
    var self = this;
    if (self.selected) {self.selected.setEnabled(true);}
    self.currentselect.setEnabled(false);
    self.selected = self.currentselect;

    if (self.click_callback) {click_callback(item);}
},
setSize:function(size){
    this._super(size);
    return this;
},
setItemSpace:function(space){
    this.itemspace = space;
    return this;
},
setItemSize:function(size){
    this.itemsize = size;
    return this;
},
setItemClickCallBack:function(callback){
    this.click_callback = callback;
    return this;
},
setDirection:function(direction){
    this.direction = direction;
    this._super(direction);
    return this;
},
setSelectList:function(selectlist){
    this.selectreslist = selectlist;
    return this;
},
setResList:function(reslist){
    this.reslist = reslist;
    return this;
},
setContentOffset:function(dir,animated){
    this._super(dir,animated);
    return this;
},
moveToIndex:function(index,isanimation){
    var self = this;
    var offset = 0;
    var dir = cc.p(0,0);
    if (this.direction == ccui.ScrollView.DIR_VERTICAL) {
        offset = (this.itemsize.height + this.itemspace)*index;
        if (offset < 0) {offset = 0;}
        else if (offset > this.innersize.height - this.height) {offset = this.innersize.height - this.height;}
        dir = cc.p(0,offset);
    } else {
        offset = -(this.itemsize.width + this.itemspace)*index;
        if (offset > 0) {offset = 0;}
        else if (offset < this.width - this.innersize.width) {offset = this.width - this.innersize.width;}
        dir = cc.p(offset,0);
    }
    var aa = self.getViewSize();
    // self.setContentOffset(dir,isanimation);
},
MoveToLastOne:function(){
    var dir = this.getContentOffset();
    var index = 0;
    if (this.direction == ccui.ScrollView.DIR_VERTICAL) {
        index = Math.floor(dir.y/(this.itemsize.height + this.itemspace));
        this.moveToIndex(index,true);
    } else {
        index = Math.floor(Math.abs(dir.y/(this.itemsize.width + this.itemspace)));
        this.moveToIndex(index);
    }
},
MoveToNextOne:function(){
    var dir = this.getContentOffset();
    var index = 0;
    if (this.direction == ccui.ScrollView.DIR_VERTICAL) {
        index = Math.floor(dir.y/(this.itemsize.height + this.itemspace));
         this.moveToIndex(index++,true);
    } else {
        index = Math.floor(Math.abs(dir.y/(this.itemsize.width + this.itemspace)));
        this.moveToIndex(index++);
    }
},

});

帖子: 1

参与者: 1

阅读整个主题


Viewing all articles
Browse latest Browse all 88737

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>