从 plist 加载图像始终高度和宽度等于零
Loading image from plist always height and width equals to zero
我正在尝试从已缓存的 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);
}
});
当您尝试通过
获取帧时
cc.spriteFrameCache.getSpriteFrame("yellowTile.png")
它返回未定义,因为您还没有将帧添加到 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);
我正在尝试从已缓存的 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);
}
});
当您尝试通过
获取帧时cc.spriteFrameCache.getSpriteFrame("yellowTile.png")
它返回未定义,因为您还没有将帧添加到 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);