Phill Jones Loading a sprite from file error
Posts 47
Added by Phill Jones 11 months ago

When I attempt to load a sprite and set its position like this:

var dinosaur = cc.Sprite.spriteWithFile("Resources/res/menu/trex_seq/rex_001.png");
            dinosaur.setAnchorPoint(cc.PointZero);
            if ([cc.Director.sharedDirector().mainScreen].scale == 2) {
                dinosaur.setScale(2);                
            }

            //var test1 = dinosaur.getTextureRect().size.height;
            //var test2 = dinosaur.getTextureRect().size.width;
            var offset = 60;
            dinosaur.setPosition(cc.ccp(0, windowSize.height - dinosaur.getTextureRect().size.height - offset));

            this.addChild(dinosaur);

If I do a hard-reload (so with empty cache), the dinosaur.getTextureRect().size height and width are both set to 0 when they should be set to the texture width and height.
Whereas if I then follow on to do a normal refresh, the positions load perfectly.

Just wondering if anyone else has come across an issue similar!

@Frizzlep

Phill Jones RE: Loading a sprite from file error
Posts 47
Added by Phill Jones 11 months ago

As a note, with this small sample program I had a similar effect, where the menuButton only became clickable and in the normal position after the refresh and therefore cache loading:

var menu1;
var selection1;

var main = cc.LayerColor.extend({
    init:function()
    {
        this._super();
        this.initWithColor(cc.ccc4(0, 0, 255, 255));
        this.setIsTouchEnabled(false);

        var s = cc.Director.sharedDirector().getWinSize();

            selection1 = cc.MenuItemImage.itemFromNormalImage("Resources/play_btn.png",
            "Resources/play_btn_pressed.png",
            null,
            this,
            function()
            {
            titleLabel = cc.LabelTTF.labelWithString("Clicked", "Arial", 30);
            titleLabel.setPosition(cc.ccp(s.width/2, (s.height - s.height/20)));
            titleLabel.setColor(cc.ccc3(255,0,0));
            this.addChild(titleLabel, 0);
            });

            menu1 = cc.Menu.menuWithItems(selection1);        
            //menu1.setPosition(100, 200);        
            this.addChild(menu1);

        return true;
    },
    });

main.scene = function() {
    var scene = cc.Scene.node();
    var layer = this.node();

    scene.addChild(layer);
    return scene;
}

main.node = function() {
    var pRet = new main();

    if(pRet && pRet.init()){
        return pRet;
    }
    return null;
}

@Frizzlep

Shun Lin RE: Loading a sprite from file error
Posts 160
Added by Shun Lin 11 months ago

Preloading is necessary for all resources before they are displayed.
Please refer to cocos2d.js in the HelloWorld directory to preload your resources.

cc.loadjs = function (filename) {
    //add the file to the que
    var script = cc.$new('script');
    script.src = cc.Dir + filename;
    script.order = cc.loadQue.length;
    cc.loadQue.push(script);

    script.onload = function () {
        //file have finished loading,
        //if there is more file to load, we should put the next file on the head
        if (this.order + 1 < cc.loadQue.length) {
            cc.$('head').appendChild(cc.loadQue[this.order + 1]);
            //console.log(this.order);
        }
        else {
            cc.setup("gameCanvas");
            //we are ready to run the game
            cc.Loader.shareLoader().onloading = function () {
                cc.LoaderScene.shareLoaderScene().draw();
            };
            cc.Loader.shareLoader().onload = function () {
                cc.AppController.shareAppController().didFinishLaunchingWithOptions();
            };
            //preload ressources
            cc.Loader.shareLoader().preload([         //Hi, I am here. Please preload your resources firstly.
                {type:"image", src:"Resources/HelloWorld.png"},
                {type:"image", src:"Resources/CloseNormal.png"},
                {type:"image", src:"Resources/CloseSelected.png"}
            ]);
        }
    };
    if (script.order === 0)//if the first file to load, then we put it on the head
    {
        cc.$('head').appendChild(script);
    }
};
Phill Jones RE: Loading a sprite from file error
Posts 47
Added by Phill Jones 11 months ago

Thanks a lot.

@Frizzlep


(1-3/3)