Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 为什么在我的代码中,当它试图显示一个对象(AS3)时会出现延迟?_Actionscript 3_Flash_Delay - Fatal编程技术网

Actionscript 3 为什么在我的代码中,当它试图显示一个对象(AS3)时会出现延迟?

Actionscript 3 为什么在我的代码中,当它试图显示一个对象(AS3)时会出现延迟?,actionscript-3,flash,delay,Actionscript 3,Flash,Delay,我最近用as3写了一个flash程序。在项目的主类中,我有一个addChild方法,它给了我一些问题。问题是,当我运行这部电影时,孩子应该在舞台上展示,在渲染图像之前,时间会有很大的延迟 下面是主类中的代码:(下面是精确的代码行) 我尝试了不同的方法,但没有找到解决办法。我已经在另一台计算机上运行了该程序,但仍有延迟。 我只是想知道我的代码是否有问题,我必须修改,或者它是否是一个flash错误 谢谢你的阅读 ~m5是因为您将帧速率设置为0吗?我认为卡丁是正确的。“帧速率的有效范围为每秒0.01到

我最近用as3写了一个flash程序。在项目的主类中,我有一个addChild方法,它给了我一些问题。问题是,当我运行这部电影时,孩子应该在舞台上展示,在渲染图像之前,时间会有很大的延迟

下面是主类中的代码:(下面是精确的代码行)

我尝试了不同的方法,但没有找到解决办法。我已经在另一台计算机上运行了该程序,但仍有延迟。 我只是想知道我的代码是否有问题,我必须修改,或者它是否是一个flash错误

谢谢你的阅读


~m5

是因为您将帧速率设置为0吗?我认为卡丁是正确的。“帧速率的有效范围为每秒0.01到1000帧。”-AS3参考。问题是,Flash的生命周期是:执行脚本-渲染帧-空闲-执行脚本-渲染帧-空闲-等等。如果设置低帧速率,脚本将变得无响应,并且每100秒会对您的任何交互做出一次响应。尝试将空闲帧速率设置为10。为什么
stage.frameRate=0?除了停止计时器外,如果您使用了Enter_Frame,那么您还应该删除那些
Enter_Frame
侦听器(稍后重新添加以恢复动画)是的。当帧速率过低时,刷新和响应能力会急剧下降。添加和删除enterframe事件监听器是非常好的选择。谢谢大家。我试着把听者移走,效果很好。再次非常感谢。:)
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);
        }
    }
}   
}
    // 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();
    }