Sign in to follow this  
Joseph

set interval

Recommended Posts

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)
\
}

Share this post


Link to post
Share on other sites

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 by ivan.popelyshev

Share this post


Link to post
Share on other sites

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
}

Share this post


Link to post
Share on other sites
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)
}

Share this post


Link to post
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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.