开发者

Problem with more than one enemy and playing if hit by missile

开发者 https://www.devze.com 2023-02-16 23:45 出处:网络
I am building a simple flash game for a class.I wanted to make a level two but cannot seem to get the enemy to explode independently. I made enemy2, enemy3, enemy4, and enemy5 symbols with instance na

I am building a simple flash game for a class.I wanted to make a level two but cannot seem to get the enemy to explode independently. I made enemy2, enemy3, enemy4, and enemy5 symbols with instance names the same. I also made a separate player just in case. the code below is ON the frame of the player. Any suggestions would be greatly appreciated.

onClipEvent(load) {
    var shipSpeed:Number = 15;
    var rotationSpeed:Number = 15;
    var missileNum:Number = 0;
    var missile:Array = new Array();
    var missleSpeed:Number = 20;
}



onClipEvent (enterFrame) {
if(Key.isDown(Key.LEFT)) {
    _rotation -= shipSpeed;
}

if(Key.isDown(Key.RIGHT)) {
    _rotation += shipSpeed;
}

var radian:Number = (-1 * _rotation + 90) * Math.PI/180;

if(Key.isDown(Key.UP)) {
    _x += shipSpeed * Math.cos(radian);
    _y -= shipSpeed * Math.sin(radian);
}

if(Key.isDown(Key.DOWN)) {
    _x -= shipSpeed * Math.cos(radian);
    _y += shipSpeed * Math.sin(radian);
}


// keeps player in the frame    
if(this._x >= 800)  this._x = 11;
if(this._x <= 10)  this._x = 799;
if(this._y >= 600)  this._y = 11;
if(this._y <= 10)  this._y = 599;




 if(this.hitTest(_root.enemy2))
 {
    _root.player2.play ();
 }

if(this.hitTest(_root.enemy3))
 {
    _root.player2.play ();
 }

 if(this.hitTest(_root.enemy4))
 {
    _root.player2.play ();
 }

 if(this.hitTest(_root.enemy5))
 {
    _root.player2.play ();
 }

//Shoot missile in direction the ship is facing
if(Key.isDown(Key.SPACE)) {
    missile[missileNum] = _root.attachMovie("missile","missile"+missileNum,_root.getNextHighestDepth(),{_x:_x,_y:_y,_rotation:_rotation});
    missile[missileNum].ySpeed = Math.sin(radian)*missleSpeed;
    missile[missileNum].xSpeed = Math.cos(radian)*missleSpeed;
    missile[missileNum].onEnterFrame = function() {
        if(this.hitTest(_root.enemy2)) {
            _root.enemy2.play ();}


        if(this.hitTest(_root.enemy3)) {
            _root.enemy3.play ();}

    if(this.hitTest(_root.enemy4)) {
        _root.enemy4.play();}

    if(this.hitTest(_root.enemy5)) {
        _root.enemy5.play();}

           // this.attachMovie("enemy", "enemy", 3 );
        }
        this._y -= this.ySpeed;
        this._x += this.xSpeed;
        trace("missile: " + missileNum);
        if(0 > this._x || this._x > 800 || 0 > this._y || this._y > 600)
            this.removeMovieClip();
    }
    missileNum+开发者_JS百科+;
}
}


you definitely need to learn how to indent your code properly. as you see, it leads not only confusion but also many bugs.

the problem is that this part of code

    this._y -= this.ySpeed;
    this._x += this.xSpeed;
    trace("missile: " + missileNum);
    if(0 > this._x || this._x > 800 || 0 > this._y || this._y > 600)
        this.removeMovieClip();

is not within the missle's onEnterFrame event listener. move the stray } curly bracket BELOW this code. also dive the missleNum++ part above 1 bracket

in other words, transform this:

       // this.attachMovie("enemy", "enemy", 3 );
    }
    this._y -= this.ySpeed;
    this._x += this.xSpeed;
    trace("missile: " + missileNum);
    if(0 > this._x || this._x > 800 || 0 > this._y || this._y > 600)
        this.removeMovieClip();
}
missileNum++;

into this:

       // this.attachMovie("enemy", "enemy", 3 );
    this._y -= this.ySpeed;
    this._x += this.xSpeed;
    trace("missile: " + missileNum);
    if(0 > this._x || this._x > 800 || 0 > this._y || this._y > 600)
        this.removeMovieClip();

    }

missileNum++;
}

also, it seems that the code you provided contains 2 unnecessary/unused } brackets at the end of the code.

let me just give you the full, properly indented "missle" part:

if(Key.isDown(Key.SPACE)) {
    missile[missileNum] = _root.attachMovie("missile","missile"+missileNum,_root.getNextHighestDepth(),{_x:_x,_y:_y,_rotation:_rotation});
    missile[missileNum].ySpeed = Math.sin(radian)*missleSpeed;
    missile[missileNum].xSpeed = Math.cos(radian)*missleSpeed;

    missile[missileNum].onEnterFrame = function() {
      if(this.hitTest(_root.enemy2)) {
        _root.enemy2.play ();}

      if(this.hitTest(_root.enemy3)) {
        _root.enemy3.play ();}

      if(this.hitTest(_root.enemy4)) {
        _root.enemy4.play();}

      if(this.hitTest(_root.enemy5)) {
          _root.enemy5.play();}

      // this.attachMovie("enemy", "enemy", 3 );

      this._y -= this.ySpeed;
      this._x += this.xSpeed;
      trace("missile: " + missileNum);
      if(0 > this._x || this._x > 800 || 0 > this._y || this._y > 600)
          this.removeMovieClip();
    }//end of the missle's onEnterFrame

    missileNum++;
}//end of the Key.isDown

another suggestion i would have is to move your enemies in an array, let's say "enemies" on the start

 var enemies:Array = new Array(); //initiate them now or later

then you may push the enemies in, just like you do with the missles enemies.push(_root.some_newly_created_enemy);

and then do

foreach(enemy in enemies){
  if(this.hitTest(enemy)) {
    enemy.play();}
}

rather than checking them statically one by one

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号