cocos2d-js引擎學習筆記

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   strong   width   2014   

Scale9Sprite

在用Scale9Sprite.create的時候出現Uncaught TypeError: Cannot call method ‘create‘ of undefined這個錯誤,

後來發現在預設情況下,project.json裡的modules只內建cocos2d模組,通過檢查frameworks/cocos2d-html5/moduleConfig.json,可以看到cocos2d模組裡並沒有CCScale9Sprite.js這個類。

它在GUI裡,所以可以在modules裡添加"GUI"這個模組,或者可以像我這樣,在moduleConfig.json裡寫一個myNeedfulClass這樣的模組,裡面添加CCScale9Sprite.js這個類,這樣只要在modules裡添加"myNeedfulClass"就行。有個好處就是可以不用載入GUI裡其他的類。測試代碼如下

        var tmp = cc.Sprite.create(res.Block9);          var theSize = tmp.getContentSize();        var fullRect = cc.rect(0,0,theSize.width,theSize.height);        var insetRect = cc.rect(32,32,theSize.width-64,theSize.height-64);        var scale9sprite = cc.Scale9Sprite.create(res.Block9,fullRect,insetRect);        scale9sprite.setContentSize(theSize.width*3,theSize.height*2);        scale9sprite.x  = size.width/2;        scale9sprite.y = size.height/2;        this.addChild(scale9sprite);

原先圖片:

經過上述代碼處理後的圖片:

效果是A,B,C,D四個角大小不變,top,bottom倆條邊橫向展開,left,right倆條邊縱向展開,中間的center既橫向又縱向展開。

用在適配的地方比較多,譬如一個BUTTON精靈。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.