2012-10-14 80 views
11

我正在嘗試cocosmotion,Cocos2d的Rubymotion實現。在我正在學習的教程中,我有一個將結構作爲參數的方法。我諮詢了Rubymotion和MacRuby文檔,但無法弄清楚如何基於我需要的結構實例化一個新對象。我認爲主要問題是結構以小寫字母開頭,而Ruby在我嘗試使用它時認爲它是一個局部變量。Rubymotion和構造指針

我相信我有同樣的問題在這裏說: https://github.com/MacRuby/MacRuby/issues/121

是否有解決方案或解決方法嗎?

的結構也是在Objective-C的定義,像這樣:

typedef struct _ccTexParams { 
     GLuint minFilter; 
     GLuint magFilter; 
     GLuint wrapS; 
     GLuint wrapT; 
} ccTexParams; 

我想調用的方法需要一個ccTexParams結構。

這裏是我試過:

  @mysprite = Pointer.new("{ccTexParams= 
      {GLUint=GL_LINEAR_MIPMAP_LINEAR} 
      {GLUint=GL_LINEAR} 
      {GLUint=GL_CLAMP_TO_EDGE} 
      {GLUint=GL_CLAMP_TO_EDGE}}", 4) 

當我嘗試這樣說:

 @mysprite = Pointer.new(:object, 4) 

     @mysprite[0] = GL_LINEAR_MIPMAP_LINEAR 
     @mysprite[1] = GL_LINEAR 
     @mysprite[2] = GL_CLAMP_TO_EDGE 
     @mysprite[3] = GL_CLAMP_TO_EDGE 

運行時錯誤是:

expected instance of Pointer of type `{_ccTexParams=IIII}', got `@' (TypeError) 

我也試過:

@mysprite = Pointer.new("_ccTexParams", 4) 

     @mysprite[0] = GL_LINEAR_MIPMAP_LINEAR 
     @mysprite[1] = GL_LINEAR 
     @mysprite[2] = GL_CLAMP_TO_EDGE 
     @mysprite[3] = GL_CLAMP_TO_EDGE 

錯誤:

Can't find pointer description for type `_ccTexParams' 

我還試圖取代它成爲CcTexParams,_ccTexParams,只是ccTexParams在一堆不同的方式,沒有它的版本。

我嘗試這樣做:

@mysprite = CcTexParams.new 
@mysprite.minFilter = GL_LINEAR_MIPMAP_LINEAR 
@mysprite.magFilter = GL_LINEAR 
@mysprite.wrapS = GL_CLAMP_TO_EDGE 
@mysprite.wrapT = GL_CLAMP_TO_EDGE 

RubyMotion會抱怨expected an instance of Pointer, got '#<CcTexParams minFilter=9987 magFilter=9729 wrapS=33071 wrapT=33071>' (CcTexParams) (TypeError)

我試着只是路過[GL_LINEAR_MIPMAP_LINEAR, GL_LINEAR, GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE]直接的方法。它抱怨它expected a Pointer but got an Array

也許我應該將lib中的ccTexParams重命名爲CCTexParams之類的東西?我猜這不是一個理想的解決方法。

+0

我將我的建議的內容移到您的問題中,並刪除了我的答案。希望這會讓你的問題更加明顯,因爲它沒有答案。 – vacawama

+0

感謝您的幫助,@vacawama。 – plasticbugs

回答

2

如果有人正在爲此尋找解決方案,請嘗試Joybox。這是Cocos2D的包裝。

Joybox combines the most popular and widely used 2D game engine for iOS, Cocos2D, folds in the amazing Box2D physics engine, and then wraps them up into a clean Ruby API that will make game development faster and more enjoyable for both beginners and experts alike.