mardi 10 janvier 2017

Why is in my code a delay when it tries to display an object (AS3)?

I recently wrote down a flash programm using as3. In the Main class for the project I have an addChild method that is giving me some problems. The thing is that, when I run the movie and the child is supposedly displayed on the stage, there is a big delay in time before the image is rendered.

Here is the code in the Main class: (exact code lines below)

package code {

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.display.Graphics;
import code.particles.*;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.text.TextField;

// import this to access to the particles


public class Main extends MovieClip {

    // set player vars
    public var player1:Player;
    public var player2:Player;
    public var ball:Ball;

    // set pause vars
    public var pauseParticle:Particle2; // var used for the pause visual effects
    public var pauseEffectMC:MovieClip = new MovieClip(); // var used for the pause visual effects
    public var gameIsPaused:Boolean;

    // set buff vars
    public var buffTimer:Timer = new Timer(10000, 0);

    public function Main() {
        // constructor code
        if (stage) init(); // init function
        else addEventListener(Event.ADDED_TO_STAGE, init);

    }

    // init function:
    public function init():void
    {
        // set movieClip class vars
        player1 = new Player(stage, 75, 200); // important values: (stage, and two numbers for position)
        player2 = new Player(stage, 650, 200); //same for player two
        ball = new Ball(stage); // same for the ball
        pauseParticle = new Particle2(stage, stage.stageWidth/2, stage.stageHeight/2);

        // define movie clips (calling a function)
        definePauseEffectMC();

        // start timer
        buffTimer.start();

        // important: add childs (create the visuals)
        stage.addChild(player1);
        stage.addChild(player2);
        stage.addChild(ball);

        // add event listeners
        stage.addEventListener(KeyboardEvent.KEY_DOWN, keyIsPressed);
        stage.addEventListener(KeyboardEvent.KEY_UP, keyIsReleased);
        stage.addEventListener(Event.ENTER_FRAME, entFrame);
        buffTimer.addEventListener(TimerEvent.TIMER, buffClock);
    }

    // constant function
    public function entFrame(e:Event):void
    {
        ball.ballRotation(player1, player2); // call the function for movement in the ball class
        ball.ballReset();
    }

    // key handlers function
    public function keyIsPressed(e:KeyboardEvent):void
    {
        switch(e.keyCode) //key cases:
        {
            /* player 1:
            */

            case 65: // a key
                player1.left = true; // change values for vars inside player1
                break;

            case 68: // d key
                player1.right = true;
                break;

            case 87: // w key
                player1.up = true;
                break;

            case 83: // s key
                player1.down = true;
                break;

            /* player 2:
            */
            case 37: // left arrow
                player2.left = true; // change values for vars inside player2
                break;

            case 39: // right arrow
                player2.right = true;
                break;

            case 38: // up arrow
                player2.up = true;
                break;

            case 40: // down arrow
                player2.down = true;
                break;

            /* pause button
            */
            case 32: // spacebar button
                pauseGame();
                break;
        }
    }

    public function keyIsReleased(e:KeyboardEvent):void
    {
        switch(e.keyCode) //key cases:
        {
            /* player 1:
            */

            case 65: // a key
                player1.left = false; // change values for vars inside player1
                break;

            case 68: // d key
                player1.right = false;
                break;

            case 87: // w key
                player1.up = false;
                break;

            case 83: // s key
                player1.down = false;
                break;

            /* player 2:
            */
            case 37: // left arrow
                player2.left = false; // change values for vars inside player2
                break;

            case 39: // right arrow
                player2.right = false;
                break;

            case 38: // up arrow
                player2.up = false;
                break;

            case 40: // down arrow
                player2.down = false;
                break;
        }
    }

    // function to pause the game
    public function pauseGame():void
    {
        if(gameIsPaused == false)
        {
            gameIsPaused = true; // revert boolean

            buffTimer.stop();
            ball.timer.stop();

            stage.addChild(pauseEffectMC); // add the effect
            stage.addChild(pauseParticle); // add the pause symbol
            trace("Paused");

            stage.frameRate = 0;
        }
        else
        {
            gameIsPaused = false; // revert boolean
            buffTimer.start();
            ball.timer.start();

            stage.removeChild(pauseEffectMC); // remove the effect
            stage.removeChild(pauseParticle); // remove the pause symbol
            trace("Continue");

            stage.frameRate = 60;
        }
    }

    // function to define the pause effect mc
    public function definePauseEffectMC():void
    {
        pauseEffectMC.graphics.beginFill(0x000000); // fill with color (black)
        pauseEffectMC.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
        pauseEffectMC.alpha = 0.6 // change the alpha
        pauseEffectMC.graphics.endFill();
    }

    /* Buffs
    */

    //
    public function buffClock(e:TimerEvent):void
    {
        var buffOn:Boolean = randBoolean(0.3);

        if(buffOn == true)
        {
            var buff:Buff = new Buff(stage, ball, player1, player2);
            stage.addChild(buff);
        }
    }

    // random boolean with probability (to get "true" in return) ("prob" must be between 0 and 1)
    public function randBoolean(prob:Number):Boolean
    {
        var randNum:Number = Math.random(); // get a random number between 0 and 0.99

        if(randNum <= prob)
        {
            return(true);
        }
        else
        {
            return(false);
        }
    }
}   
}

And here is the exact code that is giving me problems:

    // function to pause the game
    public function pauseGame():void
    {
        if(gameIsPaused == false)
        {
            gameIsPaused = true; // revert boolean

            buffTimer.stop();
            ball.timer.stop();

            stage.addChild(pauseEffectMC); // add the effect
            stage.addChild(pauseParticle); // add the pause symbol
            trace("Paused");

            stage.frameRate = 0;
        }
        else
        {
            gameIsPaused = false; // revert boolean
            buffTimer.start();
            ball.timer.start();

            stage.removeChild(pauseEffectMC); // remove the effect
            stage.removeChild(pauseParticle); // remove the pause symbol
            trace("Continue");

            stage.frameRate = 60;
        }
    }

    // function to define the pause effect mc
    public function definePauseEffectMC():void
    {
        pauseEffectMC.graphics.beginFill(0x000000); // fill with color (black)
        pauseEffectMC.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
        pauseEffectMC.alpha = 0.6 // change the alpha
        pauseEffectMC.graphics.endFill();
    }

I tried different things, but I did not find a solution. I have run the programm in another computer, but it is still having a delay. I just wanted to know if there is something wrong with my code that I have to modify, or if it is a flash error.

Thank you for reading.

~m5

Aucun commentaire:

Enregistrer un commentaire