在自定义图层中设置计划并且不会触发回调

时间:2015-03-17 16:34:13

标签: cocos2d-js cocos2d-html5

我正在使用Cocos2d-html5中的一个简单的可重复使用的自定义图层来管理经典的连续背景滚动条。我最初在我的主游戏层创建了这个,在那里我得到它的工作,但是当我将它移动到它自己的BackgroundScroller层时,除了Schedule之外,一切都有效,无论是从主层还是在BackgroundScroller里面的方法中设置“滚动” “永远不会被召唤。

什么可以导致时间表登记而不是火灾?这是范围问题吗?我应该在BackgroundScroller上还是在实例化它的代码中安排事件?

onEnter: function(){
    var self = this;
    this._super();

    winSize = cc.director.getWinSize();
    centerPos = cc.p(winSize.width/2, winSize.height/2);

    bs = new BackgroundScroller("https://fbcdn-sphotos-f-a.akamaihd.net/hphotos-ak-xap1/v/t1.0-9/1014377_10153225997278888_209146662968373758_n.jpg?oh=287c045d5b8752581f8bdce968312c5c&oe=55BBA0B3&__gda__=1437658479_65b312f58967c3e1bd0ddb07260395cb", 
winSize, centerPos, BackgroundScroller.Speeds.MEDIUM_SLOW);
        this.addChild(bs);
        bs.startScroll();
    }

这是BackgrounScroller自定义图层的代码:

var BackgroundScroller = cc.Layer.extend( /** @lends cc.Layer# */ {
  _bgSprites: [],
/* This is the interesting bit */
  startScroll: function(scrollSpeed) {
    scrollSpeed = scrollSpeed || this.scrollSpeed;
    cc.log('Start Scroll:' + scrollSpeed);
    this.schedule(this.scroll, scrollSpeed);
    this.scheduleUpdate();
  },
  scroll: function(dt) {
    debugger;
    for (var i = 0; i < this._bgSprites.length; i++) {
      var bgSprite = this._bgSprites[i];
      bgSprite.setPositionX(bgSprite.getPosition().x - 1);
      this.checkIsOffEdge(bgSprite);
    }
  },
  ctor: function(bgSpriteOrCallbackFunction, winSize, centerPos, scrollSpeed) {
    this._super();

    this.scrollSpeed = scrollSpeed;
    this.winSize = winSize || cc.director.getWinSize();
    this.centerPos = centerPos || cc.p(this.winSize.width / 2, this.winSize.height / 2);

    if (typeof bgSpriteOrCallbackFunction == "function") {
      this.setupBackgroundSprite = bgSpriteOrCallbackFunction;
    } else {
      this.bgSpriteName = bgSpriteOrCallbackFunction;
    }

    this._bgSprites = [];
  },
  onEnter: function() {
    this.setupBackgroundSprites();
  },
  stopScroll: function() {
    this.pause();
  },
  /**
   * Sets up sprites with either the Background sprite name or the callback function specified in the ctor.
   * @returns void
   */
  setupBackgroundSprites: function() {
    cc.log("Setup background sprites");
    this.totalBgWidth = 0;
    var bgSpriteWidth = 0;
    while (this.totalBgWidth < this.winSize.width * 2) {
      var bgSprite = this.setupBackgroundSprite();
      this._bgSprites.push(bgSprite);
      bgSprite.setAnchorPoint(0, 0);
      bgSpriteWidth = bgSprite.getBoundingBox().width;
      this.addChild(bgSprite);
      this.totalBgWidth += bgSpriteWidth;
      this.maximumBgExtent = this.totalBgWidth - bgSpriteWidth;
      var posN = (this._bgSprites.length - 1) * bgSprite.getBoundingBox().width; // Stack them left to right
      bgSprite.setPositionX(posN);
    }

    cc.log("BgSprites in background sprites:" + this._bgSprites);
  },
  setupBackgroundSprite: function() {
    var bgSprite = new cc.Sprite(this.bgSpriteName)
    return bgSprite;
  },
  checkIsOffEdge: function(sprite) {
    var spriteBB = sprite.getBoundingBox();
    if (spriteBB.x + spriteBB.width < 0) {
      sprite.setPositionX(this.maximumBgExtent - 1);
    }
  }
});

BackgroundScroller.Speeds = {
  "SLOW": 0.5,
  "MEDIUM_SLOW": 0.005,
  "MEDIUM": 0.001,
  "MEDIUM_FAST": 0.0005,
  "FAST": 0.0001
};

1 个答案:

答案 0 :(得分:0)

我明白了。我在BackgroundScroller层中封装了除日程表之外的所有内容。在MainLayer中,我设置了时间表,并调用&#34;滚动&#34;在BackgroundScroller上。

我仍然不确定它为什么不起作用,但这是有道理的 - 无论如何,父层都应该管理所有这些逻辑。