Joseph Posted April 6, 2020 Share Posted April 6, 2020 I am trying to run a set interval to draw a tree every 3000 milliseconds in my game loop. however it runs according to the ticker and draw too many trees. function gameLoop() { drawRandomTree() } function drawRandomTree() { setInterval( drawTree(), 3000 ) } function drawTree() { tree = new PIXI.Sprite.from("images/Tree_3.png") tree.anchor.set(0.5) tree.x = Math.random() * 800 tree.y = app.view.height / 1.89 app.stage.addChild(tree) \ } Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted April 7, 2020 Share Posted April 7, 2020 (edited) Hello and Welcome to the forums! You have to put "app.render()" in setInterval too, and specify "{autoStart:false} " on start. Please read this article about gameloop: https://github.com/pixijs/pixi.js/wiki/v5-Custom-Application-GameLoop Dont forget to remove old tree from stage. Also after you add 1000 trees - it will draw 1000 trees every time you call render(). You can try to work with pixi like with drawer that doesnt clear() between frames, but.. its not guaranteed in webgl that your stuff wont be erased between frames If you want to store previous state of canvas - use RenderTexture. Edited April 7, 2020 by ivan.popelyshev Joseph 1 Quote Link to comment Share on other sites More sharing options...
Joseph Posted April 7, 2020 Author Share Posted April 7, 2020 thank you Ivan I actually had a classmate solve this question for me. but now Ive stumbled upon a new problem. I am looping in sprites trying to assign movement to sprites and its working but when the loop runs again the sprites movement is reassigned to the next sprite and the for sprite movement stops function gameLoop() if (!sprite) { return null } else { sprite.x -= 1 } Quote Link to comment Share on other sites More sharing options...
Joseph Posted April 7, 2020 Author Share Posted April 7, 2020 function drawRandomSprites() { let images = ["images/Tree_3.png", "images/crate.png"] let randomIndex randomIndex = Math.floor(Math.random() * images.length); sprite = new PIXI.Sprite.from(images[randomIndex]) sprite.anchor.set(0.5) sprite.x = 1200 sprite.y = 420 console.log(randomIndex) app.stage.addChild(sprite) } Quote Link to comment Share on other sites More sharing options...
Joseph Posted April 7, 2020 Author Share Posted April 7, 2020 let i = 0 function gameLoop() { setTimeout(() => drawRandomSprites() , i * 3000) i++ Quote Link to comment Share on other sites More sharing options...
Joseph Posted April 7, 2020 Author Share Posted April 7, 2020 former* Quote Link to comment Share on other sites More sharing options...
ivan.popelyshev Posted April 7, 2020 Share Posted April 7, 2020 Sorry, its more coding problem than pixi, you have to wait for someone else to answer Someone who can teach you both basic js lambdas stored in array and what is a "tween" 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.