Loading image from plist always height and width equals to zero

737 views Asked by At

I'm trying to load an image from a plist already cached and get the height and width of it, but it's always zero. I found here that this problems is solved by preloading the images but even though I'm preloading the plist like:

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); };

It still doesn't show the size:

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

There are 1 answers

1
yangguang1029 On BEST ANSWER

when you tried to get frame by

cc.spriteFrameCache.getSpriteFrame("yellowTile.png")

it returned undefined because you haven't added the frames into frameCache.

you should both load the png file, and add frames into frameCache after loaded

try these:

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);