Jambutters Posted February 12, 2017 Share Posted February 12, 2017 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 Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted February 12, 2017 Share Posted February 12, 2017 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 Quote Link to comment Share on other sites More sharing options...
Jambutters Posted February 12, 2017 Author Share Posted February 12, 2017 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). Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted February 12, 2017 Share Posted February 12, 2017 about =: for (var i=0;i<=2;i++) { ... } will iterate 0,1,2 - three times and not two. You have to read some fundamental stuff at the same time, otherwise you'll be a bit undereducated. Don't be afraid of it Save the books while you can Quote Link to comment Share on other sites More sharing options...
Recommended Posts
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.