標籤: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精靈。