收藏
回答

Uncaught TypeError: Cannot set property 'ctx' of u

框架类型 问题类型 终端类型 微信版本 基础库版本
小游戏 Bug 微信iOS客户端 1.02.1911180 2.0.0

请问这个问题怎么办啊?(呜呜呜,我好卑微,根本看不懂~)

回答关注问题邀请回答
收藏

2 个回答

  • Bruce Lee
    Bruce Lee
    2020-04-20
    import {ResourceLoader} from "./base/ResourcrLoader.js";
    import {DataStore} from "./base/DataStorm.js";
    import {Background} from "./runtime/Background.js";
    import {Director} from "./Director.js";
    import {Land} from "./runtime/Land.js";
    
    // function createRecycleContext() {
    //     return undefined;
    // }
    //let ctx = canvas.getContext('2d');
    //const ctx = createRecycleContext();
    export class Main{
        constructor() {
            this.canvas = document.getElementById('game_canvas');       //准备好画布
            this.ctx = this.canvas.getContext('2d');                    //准备好上下文对象
            this.dataStore = DataStore.getInstance();                               //找到变量存储器
            this.director = Director.getInstance();                     //找到导演类
            const loader = ResourceLoader.create();                  //创建资源加载器
            loader.onload(map=>this.onResourceFirstLoad(map));
            // let image = new Image();
            // image.src = './images/background1.png';
            // image.onload = ()=>{
            //     this.ctx.drawImage(image,
            //         0,0,
            //         image.width,image.height,
            //         0,0,
            //         image.width,image.height);
            // }
        }
    
        onResourceFirstLoad(map){       //第一次完成加载时,将canvas,context,map对象存入变量存贮器中
            //console.log(map)
            //let ctx = createRecycleContext();
            this.dataStore = DataStore.getInstance();
            this.dataStore.ctx = this.ctx ;
            this.dataStore.canvas = this.canvas;
            this.dataStore.images = map;        //DataStore中的images变量储存的是图片对象
            this.init();
        }
    
        init(){     //将每个精灵放入DataStore中
            this.dataStore.put('background1',Background)
                .put('land',Land);
            this.director.run();
    
        }
    }
    


    2020-04-20
    有用
    回复 1
    • Forever
      Forever
      2020-04-21
      说实话,我依然看不懂你的逻辑是怎么写的,直接看报错信息的话你的 this.dataStore 这个对象是undefined
      2020-04-21
      回复
  • Forever
    Forever
    2020-04-20

    https://developers.weixin.qq.com/miniprogram/dev/devtools/minicode.html 能否按照网址里面的操作制作简单的代码片段给我们进行排查问题?

    2020-04-20
    有用
    回复 2
    • Bruce Lee
      Bruce Lee
      2020-04-20
      这次不是用微信开发者工具,可以直接输入代码吗?不好意思,麻烦你了
      2020-04-20
      回复
    • Bruce Lee
      Bruce Lee
      2020-04-20
      import {ResourceLoader} from "./base/ResourcrLoader.js";
      import {DataStore} from "./base/DataStorm.js";
      import {Background} from "./runtime/Background.js";
      import {Director} from "./Director.js";
      import {Land} from "./runtime/Land.js";

      // function createRecycleContext() {
      // return undefined;
      // }
      //let ctx = canvas.getContext('2d');
      //const ctx = createRecycleContext();
      export class Main{
          constructor() {
              this.canvas = document.getElementById('game_canvas'); //准备好画布
              this.ctx = this.canvas.getContext('2d'); //准备好上下文对象
              this.dataStore = DataStore.getInstance(); //找到变量存储器
              this.director = Director.getInstance(); //找到导演类
              const loader = ResourceLoader.create(); //创建资源加载器
              loader.onload(map=>this.onResourceFirstLoad(map));
              // let image = new Image();
              // image.src = './images/background1.png';
              // image.onload = ()=>{
              // this.ctx.drawImage(image,
              // 0,0,
              // image.width,image.height,
              // 0,0,
              // image.width,image.height);
              // }
          }

          onResourceFirstLoad(map){ //第一次完成加载时,将canvas,context,map对象存入变量存贮器中
              //console.log(map)
              //let ctx = createRecycleContext();
              this.dataStore = DataStore.getInstance();
              this.dataStore.ctx = this.ctx ;
              this.dataStore.canvas = this.canvas;
              this.dataStore.images = map; //DataStore中的images变量储存的是图片对象
              this.init();
          }

          init(){ //将每个精灵放入DataStore中
              this.dataStore.put('background1',Background)
                  .put('land',Land);
              this.director.run();

          }
      }
      2020-04-20
      回复
登录 后发表内容
问题标签