2013-02-13 3 views
-3

6x6 색 사각형의 격자가 포함 된 EnchantJS로 HTML5/JavaScript 게임을 만들려고합니다. 나는 1 개의 이미지를 보여 주었지만 모든 이미지를 표시하기 위해 36x4 라인의 코드를 가지고 있기보다는 모든 것을 함수로 옮기고 싶었습니다. 코드는 라인 62 this.addChild(block);에서 중단됩니다. 왜 이것이 작동하지 않는지 알 수가 없습니다. 나는 과거의 일을 기능들과 기능 들간의 정보 호출 방법에 대해 읽는 데 보냈다. 내가 뭘 잘못하고 있는지 알아낼 수 없으며 검색에서 답변을 찾을 수 없습니다. 나는 순전히 자바와 C++ 배경에서 왔기 때문에 문법을 엉망으로 만들고 뭔가를 이해하지 못한다고 생각합니다.Javascript OOP, functions

enchant(); 

window.onload = function() { 

    /* 
    * Game width and height. 
    * Note: Game scale is half so actual width=width/2 and 
    * actual heigh it height/2. 
    */ 
    var game = new Game(1280,768); 

    /* 
    * Game Preload Vars 
    */ 
    game.preload('res/bg2x.png', 
    'res/block.png'); 

    game.fps = 30; 
    game.scale = 0.5; 

    game.onload = function() { 

     console.log("Game Loaded"); 
     var scene = new SceneGame; 
     game.pushScene(scene); 

    } 

    game.start(); 
    window.scrollTo(0, 0); 

    var SceneGame = Class.create(Scene, { 

     initialize: function() { 

     var game, bg; 
     Scene.apply(this); 
     game = Game.instance; 

     // Background 
     bg = new Sprite(1280,768); 
     bg.image = game.assets['res/bg2x.png']; 

     // Populate Screen 
     this.addChild(bg); 

     gridBuilder(500,75); 

     } 

    }); 

    var gridBuilder = function(x,y) { 

     this.x=x; 
     this.y=y; 

     block = new Block; 
     block.x = x; 
     block.y = y; 
     this.block = block; 

     this.addChild(block); // THIS IS WHERE THE ERROR IS OCCURING. 

    }; 

    var Block = Class.create(Sprite, { 

     // The player character.  
     initialize: function() { 
     // 1 - Call superclass constructor 
     Sprite.apply(this,[100, 100]); 
     this.image = Game.instance.assets['res/block.png']; 
     // 2 - Animate 
     this.animationDuration = 0; 
     this.addEventListener(Event.ENTER_FRAME, this.updateAnimation); 
     }, 

     updateAnimation: function (evt) {   
      this.animationDuration += evt.elapsed * 0.001;  
      if (this.animationDuration >= 0.25) { 
       this.frame = (this.frame + 1) % 4; 
       this.animationDuration -= 0.25; 
      } 
     }, 

    }); 

} 
+5

"휴식"방법? 오류가 있습니까? – deceze

답변

1

gridBuilder은 기능입니다. 그것은 addChild 메소드가 없습니다. 이유는 addChildSceneGame에서 작동하기 때문에 SceneGameClass.create으로 작성 되었기 때문에 addChild 방법이 본질적으로 포함되어 있기 때문입니다. 을 만들려면 SceneGame 클래스를 만들거나 실제로 gridBuilder에 수행 할 작업을 다시 생각해보십시오. 한 가지 방법이나 다른 방법, 당신이하는 일은 더해지지 않고 실제로 JS가 성취하기를 기대하고있는 것에 대해 생각할 필요가 있습니다.

+0

많은 의미가 있습니다. 나는 Java/C++ 배경에서 왔기 때문에 .js 파일의 구조를 오해하고있었습니다. 나는 단지'this.addChild()'메소드를'gridBuilder'에서 꺼내서'gridBuilder'를'block'을 리턴 한 다음'this.addChild (gridBuilder (500,75))'를 SceneGame에 추가함으로써 문제를 해결했습니다. . @EliGassert에 대한 설명 주셔서 감사합니다! – pattmorter