从plist加载图像始终高度和宽度等于零

时间:2015-06-24 15:04:22

标签: cocos2d-js

我试图从已缓存的plist中加载图像并获取其高度和宽度,但它总是为零。 我发现here通过预加载图像解决了这个问题,但即使我正在预加载plist:

cc.game.onStart = function(){
if(!cc.sys.isNative && document.getElementById("cocosLoading")) //If referenced loading.js, please remove it
    document.body.removeChild(document.getElementById("cocosLoading"));

// Pass true to enable retina display, disabled by default to improve performance
cc.view.enableRetina(false);
// Adjust viewport meta
cc.view.adjustViewPort(true);
// Setup the resolution policy and design resolution size
cc.view.setDesignResolutionSize(800, 450, cc.ResolutionPolicy.SHOW_ALL);
// The game will be resized when browser size change
cc.view.resizeWithBrowserSize(true);
//load resources
console.log(g_resources);
cc.LoaderScene.preload([{type:'plist', src: "res/tiles.plist"}], function () {        
    cc.director.runScene(new HelloWorldScene());
}, this); };

它仍然没有显示尺寸:

var HelloWorldLayerTile = cc.Layer.extend({
    buildGround: function(){
        var tile = new cc.Sprite(cc.spriteFrameCache.getSpriteFrame("yellowTile.png"));
        console.log(tile.width); //Always get 0 
        console.log(tile.getContentSize().height); //Always get 0 
        this.addChild(tile);
    },
    ctor:function () {
        //////////////////////////////
        // 1. super init first
        this._super();
        this.buildGround();
        return true;
    }
});

var layer;
var HelloWorldScene = cc.Scene.extend({
    onEnter:function () {
        this._super();
        cc.spriteFrameCache.addSpriteFrames(res.tiles);
        layer = new HelloWorldLayerTile();
        this.addChild(layer);
    }
});

1 个答案:

答案 0 :(得分:1)

当你试图通过

获取框架时
  

cc.spriteFrameCache.getSpriteFrame(" yellowTile.png&#34)

它返回undefined,因为你还没有将帧添加到frameCache中。

你应该加载png文件,并在加载

之后将帧添加到frameCache中

试试这些:

cc.LoaderScene.preload([{type:'plist', src: "res/tiles.plist"}, {type:'png', src:"tiles.png"}], function () {  
    cc.spriteFrameCache.addSpriteFrames("res/tiles.plist");      
    cc.director.runScene(new HelloWorldScene());
}, this);