How can you make a program run slower?

edited December 2016 in p5.js Library Questions

Hello! I want to run an animation one time within the draw () loop, while keeping everything else running _forever. I've set var flag to 0 in setup (). Ideally, if animationOne overlaps circle, then it should run a sequence, then stop. The problem is that (I think) the code is running too fast, it gets to flag = 1 before it has a chance to run animationOne.changeAnimation('round');

What's the best way to accomplish this? Thanks in advance.

    function draw() {
      if(animationOne.overlap(circle) && flag == 0){
        animationOne.changeAnimation('round');
        flag = 1;
        }
      else
        animationOne.changeAnimation('normal');

Answers

  • Hello @Sophi

    Do you have the code to get more help about your question?

  • edited December 2016

    hi @laimperiestro!

    I will add some more the code here :) What I intend is within the draw function, for the "if" expression to be evaluated as true and continue to run the line animationOne.changeAnimation('round'); then set the flag value to 1. The trouble is that the code seems to skip running the function animationOne.changeAnimation('round'); and change flag to 1 beforehand. Any help with a solution to this would be appreciated!

    Here is the full code:

    ` var animationOne, circle, ganda, flag;
    
       function preload() {
       animationOne = loadAnimation('assets/cloud_breathing0001.png','assets/cloud_breathing0009.png');
          circle = loadAnimation('assets/ghost_walk0001.png', 'assets/ghost_walk0004.png');
    
      function setup() {
      createCanvas(windowWidth,windowHeight);
      flag = 0;
      //create sprites
      circle = createSprite(400, 200);
      animationOne = createSprite(200, 200);
      animationOne.addAnimation('normal', 'assets/normal0001.png', 'assets/normal0003.png');
      animationOne.addAnimation('round', 'assets/GandA0001.png','assets/GandA0035.png');
      circle.addAnimation('circle', 'assets/ghost_walk0001.png', 'assets/ghost_walk0004.png');
    
      socket = io.connect('localhost:8080');
      socket.on('HappySadFight', newGame);
      }
    
      function newGame(data){
      noStroke();
      fill(255,0,100);
      ellipse(data.x, data.y, 36, 36);
      }
    
    function draw() {
      background(255, 255, 255);
    
      animationOne.position.x = mouseX;
      animationOne.position.y = mouseY;
    
      if(animationOne.overlap(circle) && flag == 0){
        animationOne.changeAnimation('round');
      flag = 1;
      }
      else
        animationOne.changeAnimation('normal');
        circle.changeAnimation('circle');
    
      animationOne.debug = mouseIsPressed;
      circle.debug = mouseIsPressed;
    
      drawSprites();
      }`
    
Sign In or Register to comment.