Jump to content

[SOLVED]PIXI.extras.AnimatedSprite erroring: Cannot read property '0' of undefined


Jambutters
 Share

Recommended Posts

So I'm trying to splice all frames from an image, convert them to textures then put them into an array but I'm not sure as to why it is erroring, also I am also not sure why the if statements need to be above the "let rect" declarations

    let createArrayTexture = function(source, frameWidth, frameHeight){
        let textureArray = [];
        let frameX = 0;
        let frameY = 0;
        
        let totalFrames = PIXI.loader.resources[source].texture.width/32 * PIXI.loader.resources[source].texture.height/32;
        
        for(let i = 0; i <= totalFrames; i++){
            console.log(`${i}, frameX: ${frameX} frameY: ${frameY}`);
            let tempTexture = new PIXI.Texture(PIXI.utils.TextureCache[source]);
            if(frameX === PIXI.loader.resources[source].texture.width){
                frameX = 0;
                frameY+=32;
            }
            if(frameY === PIXI.loader.resources[source].texture.height){
                
                return;
            }
            let rect = new PIXI.Rectangle(frameX, frameY, frameWidth, frameHeight);
            tempTexture.frame = rect;
            
            textureArray.push(tempTexture);
            frameX+=32;
            //console.log(tempTexture);
            
            
        }
        
            return textureArray;
    };


    let createPlayer = function(){
        let animationArray = createArrayTexture("./img/player.png", 32, 32);
        let frameFlipChar = new PIXI.extras.AnimatedSprite(animationArray);
};

Solution: Thought return undefined would only break out of for loop, not entire function

Link to comment
Share on other sites

1) division doesnt work that way. If you want integer division , its Math.floor(x/32). 

2) For cycle doesnt work that way. remove "=" in for 

3) you return undefined in the middle of the cycle. You wanted "break" there, right?

It seems that you need more practive with javascript, or any other language. 

Take that one: ftp://91.193.236.10/pub/docs/linux-support/programming/JavaScript/[O`Reilly] - JavaScript. The Definitive Guide, 6th ed. - [Flanagan].pdf

And that one: http://ce.bonabu.ac.ir/uploads/30/CMS/user/file/115/EBook/Introduction.to.Algorithms.3rd.Edition.Sep.2010.pdf

I just googled it, I'm sure O'relly will remove links later :)

Link to comment
Share on other sites

6 hours ago, ivan.popelyshev said:

1) division doesnt work that way. If you want integer division , its Math.floor(x/32). 

2) For cycle doesnt work that way. remove "=" in for 

3) you return undefined in the middle of the cycle. You wanted "break" there, right?

It seems that you need more practive with javascript, or any other language. 

Take that one: ftp://91.193.236.10/pub/docs/linux-support/programming/JavaScript/[O`Reilly] - JavaScript. The Definitive Guide, 6th ed. - [Flanagan].pdf

And that one: http://ce.bonabu.ac.ir/uploads/30/CMS/user/file/115/EBook/Introduction.to.Algorithms.3rd.Edition.Sep.2010.pdf

I just googled it, I'm sure O'relly will remove links later :)

Thanks mate! Indeed I do need a lot of practice, hopefully I at least become proficent in 3-4 months. I've been reading a lot of resources on js over and over but it does not stay in my head if I do not use it lol.

Anyways, what do you mean by removing the "=" in the for loop?

Solved the problem by issuing a break statement in the if statement opposed to returnning undefined P: . (wtf, though, thought return undefined; takes me out of the for loop only, not the entire function).

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...