转到下一帧,然后在几秒钟后删除其中的movieClip(Actionscript 2)

Posted

技术标签:

【中文标题】转到下一帧,然后在几秒钟后删除其中的movieClip(Actionscript 2)【英文标题】:Going to next frame and then removing movieClip in it after a few seconds (Actionscript 2) 【发布时间】:2016-10-20 14:52:55 【问题描述】:

我正在开发一款射击游戏。这是我的情况,我怎样才能让我的敌人进入下一帧,当它被播放器拍摄时包含影响影片剪辑,然后自己删除影片剪辑

就像你射了一辆车,然后爆炸,几秒钟后爆炸就消失了

这是我在框架中的代码:

stop(); //add a stop function too!

var gameOver: Boolean = false;

var mainSpeed: Number = 10; //how fast the main guy can move

//BULLET TIMING VARIABLES
var cTime: Number = 0; //the amount of frames that has elapsed since last bullet shot
var cLimit: Number = 12; //amount of frames needed to shoot another bullet
var shootAllow: Boolean = false; //whether or not main can shoot
//ENEMY TIMING VARIABLES
//how much time before another enemy is made
var enemyTime: Number = 0;
//how much time needed to make an enemy
//it should be more than the shooting rate
//or else killing all of the enemies would
//be impossible :O
var enemyLimit: Number = 50;
//showing the amount of enemies that have been added to the stage
var enemyTotal: Number = 0;

//the player's score
var score: Number = 0;

//this movieclip will hold all of the bullets
_root.createEmptyMovieClip('bulletHolder', _root.getNextHighestDepth());

onEnterFrame = function()  //this function will run every frame (needed for moving the character
  if (Key.isDown(37) || Key.isDown(65))  //if the "A" key or Left Arrow Key is Down
    bat._x -= mainSpeed; //then the move the guy left
  
  if (Key.isDown(38) || Key.isDown(87))  //if the "W" key or Up Arrow Key is Down
    bat._y -= mainSpeed; //then move the guy up
  
  if (Key.isDown(39) || Key.isDown(68))  //if the "D" key or Right Arrow Key is Down
    bat._x += mainSpeed; //then move the guy to the right
  
  if (Key.isDown(40) || Key.isDown(83))  //if the "S" key or Down Arrow Key is Down
    bat._y += mainSpeed; //then move the guy down
  

  //keeping the main character within bounds
  if (bat._x <= 0) 
    bat._x += mainSpeed;
  
  if (bat._y <= 0) 
    bat._y += mainSpeed;
  
  if (bat._x >= 500) 
    bat._x -= mainSpeed;
  
  if (bat._y >= 500) 
    bat._y -= mainSpeed;
  

  if (Key.isDown(32) && shootAllow)  //if the space bar is pressed
    var bulletID: Number = Math.random(); //create a variable that we'll use at the bullet's id
    //then attach a bullet to the stage
    bulletHolder.attachMovie('fire', 'Bullet' + bulletID, _root.getNextHighestDepth());
    //setting the coordinates of the bullet to be the same as the main character
    bulletHolder['Bullet' + bulletID]._x = bat._x;
    bulletHolder['Bullet' + bulletID]._y = bat._y - 70;
    bulletHolder['Bullet' + bulletID].onEnterFrame = function() 
      //giving the bullet some actions
      this._y -= 50; //moving the bullet
      if (this._y < -1 * this._height)  //if the bullet goes off stage
        //then destroy it
        this.removeMovieClip();
      

      //checking if the game is over
      if (gameOver) 
        //destroy this guy if the game is over
        this.removeMovieClip();
      
    
    shootAllow = false;
  

  cTime++; //increment the time
  if (cTime == cLimit)  //if enough time has elapsed
    shootAllow = true; //allow shooting again
    cTime = 0; //reset the time
  

  enemyTime++; //incrementing time for enemy
  if (enemyTime == enemyLimit)  //if enough time has elapsed
    _root.attachMovie('thug', 'en' + enemyTotal, _root.getNextHighestDepth()); //then add the enemy
    //setting it's coordinates
    _root['en' + enemyTotal]._x = int(Math.random() * Stage.width); //randomly within the boundaries
    _root['en' + enemyTotal]._y = -50; //sets this offstage at first
    _root['en' + enemyTotal].onEnterFrame = function()  //then give it some functions
      this._y += 5;

      //run a loop checking if it's touching any bullets
      for (var cBullet: String in _root.bulletHolder) 
        //if it's touching the bullet
        //we have to use coordinates because hit testing doesn't seem to work
        if (this._y >= _root.bulletHolder[cBullet]._y - 50 && this._y <= _root.bulletHolder[cBullet]._y) 
          if (this._x <= _root.bulletHolder[cBullet]._x + 25 && this._x >= _root.bulletHolder[cBullet]._x - 55) 
            //then destroy this guy
            //this is where I tried to make it work but
            //it just change the movieclip but not removed it
            this.gotoAndStop(2)
            if (_root.thug.frame = 2) 
              _root.thug.removeMovieClip();
            
            //and destroy the bullet
            _root.bulletHolder[cBullet].removeMovieClip();

            //up the score
            _root.score += 5;
          
        
      

      //hit testing with the user
      if (this.hitTest(_root.bat)) 
        //set the game to be over and go to lose screen
        gameOver = true;
        gotoAndStop(2);
      

      //checking if the game is over
      if (gameOver) 
        //destroy this guy if the game is over
        this.removeMovieClip();
      
    
    enemyTime = 0; //reset the time
    enemyTotal++; //add 1 more to the amount of enemies total
  

  //updating the score text field
  txtScore.text = 'Score:  ' + score;

谢谢你..

【问题讨论】:

【参考方案1】:

只需创建另一个函数来删除您的电影剪辑并延迟调用它:

setTimeout(onTimeout, 2000); // call the onTimeout function with a delay of 2 seconds

function onTimeout()

    this.removeMovieClip();

我只是不确定你是否能够在你的主函数之外调用这个新函数,因为它是在 onEnterFrame 范围内声明的。因此,如果这不起作用,您可能需要稍微修改一下 main 函数:

onEnterFrame = onEnterFrameFunction;

function onEnterFrameFunction()

    // move your onEnterFrame code here

【讨论】:

以上是关于转到下一帧,然后在几秒钟后删除其中的movieClip(Actionscript 2)的主要内容,如果未能解决你的问题,请参考以下文章

JavaScript 转到下一帧(Flash Javascript)

转到下一帧(Flash Javascript)

如何缩放一个对象,以便当它等于 0 时它会转到下一帧? AS3

AddChild 仅在重新访问框架时添加到舞台一次

进入下一帧/错误时如何停止循环声音

状态栏在关闭模式视图后保持隐藏并在几秒钟后出现