javascript - Cocos2d html5无法将属性src设置为null

标签 javascript cocos2d-x cocos2d-html5

浏览时出现主题错误:http://www.cocos2d-x.org/reference/html5-js/symbols/cc.html

请注意,我使用的是 2.1.2 beta 版本,因此我知道教程中会有差异。这是我到目前为止的代码:

(function() {
  var AppLayer = cc.LayerColor.extend({
    init: function() {
      this._super(new cc.Color4B(0, 0, 0, 255));
      var size = cc.Director.getInstance().getWinSize();
      this._jetSprite = new JetSprite();
      this.setTouchEnabled(true);
      this.setKeyboardEnabled(true);
      this.setPosition(new cc.Point(0, 0));

      this.addChild(this._jetSprite);
      this._jetSprite.setPosition(new cc.Point(size.width / 2, size.height / 2));
      this._jetSprite.scheduleUpdate();
      this.schedule(this.update);
      return true;
    },
    _jetSprite: null,
    onEnter: function() {
      this._super();
    },
    onKeyDown: function(e) {
      this._jetSprite.handleKey(e);
    },
    onKeyUp: function() {

    },
    onTouchesEnded: function(pTouch, pEvent) {
      this._jetSprite.handleTouch(pTouch[0].getLocation());
    },
    onTouchesMoved: function(pTouch, pEvent) {
      this._jetSprite.handleTouchMove(pTouch[0].getLocation());
    },
    update: function(dt) {

    }
  });


  window.AppScene = cc.Scene.extend({
    onEnter: function() {
      this._super();
      var layer = new AppLayer();
      layer.init();
      this.addChild(layer);
    }
  });
})();

var JetSprite = cc.Sprite.extend({
  _currentRotation:0,
  ctor: function() {
    this.initWithFile("images/jet.png");
  },
  handleKey: function(e) {
    if(e == cc.KEY.left) {
      this._currentRotation--;
    }
    else if(e == cc.KEY.right) {
      this._currentRotation++;
    }

    if(this._currentRotation < 0) {
      this._currentRotation = 360;
    }
    if(this._currentRotation > 360) {
      this._currentRotation = 0;
    }
  },
  handleTouch: function(touchLocation) {
    if(touchLocation.x < 300) {
      this._currentRotation = 0;
    }
    else {
      this._currentRotation = 180;
    }
  },
  handleTouchMove: function(touchLocation) {
    var angle = Math.atan2(touchLocation.x - 300, touchLocation.y - 300);

    angle = angle * (180/Math.PI);
    this._currentRotation = angle;
  },
  update: function(dt) {
    this.setRotation(this._currentRotation);
  }
});

这是错误的更详细信息:

Uncaught TypeError: Cannot set property 'src' of null CCSprite.js:2209
cc.SpriteWebGL.cc.Node.extend.initWithTexture CCSprite.js:2209
(anonymous function) CCSprite.js:2161

最佳答案

看起来我正在遵循的教程缺少一行。 ctor 函数需要 this._super() 以便一切都能正确初始化。

var JetSprite = cc.Sprite.extend({
  _currentRotation:0,
  ctor: function() {
    this._super();
    this.initWithFile("images/jet.png");
  },

关于javascript - Cocos2d html5无法将属性src设置为null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15756084/

相关文章:

javascript - 如何将对象放入三元条件中?

javascript - Foundation5 停止下拉菜单可点击

ios - Cocos2d-x 4.0新项目不包含xcodeproj文件

c++ - 为什么我的 OpenGL 自定义着色器给我这个错误?

javascript - 从项目外部导入文件

javascript - 嵌套 json 的 knockout 映射

c++ - 在 cocos2d-x 3 中异步预加载 spritsheet 并使用它

javascript - 如何在 Javascript 中访问外部对象上的函数

javascript - 如何使用 Cocos2D 制作 HTML5 Canvas 整页大小