我正在为一个班级构建一个简单的Flash游戏。我想要达到二级但似乎无法使敌人独立爆炸。我使用实例名称生成了enemy2,enemy3,enemy4和enemy5符号。为了以防万一,我还做了一个单独的玩家。下面的代码是播放器的框架。任何建议都将不胜感激。
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++;
}
}
答案 0 :(得分:0)
您肯定需要学习如何正确缩进代码。如你所见,它不仅导致混乱,还导致许多错误。
问题在于这部分代码
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();
不在missle的onEnterFrame事件监听器中。移动迷路}大括号下面这段代码。也可以将missleNum++
部分放在1括号
// 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++;
进入这个:
// 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++;
}
此外,您提供的代码似乎在代码末尾包含2个不必要/未使用的}括号。
让我给你完整,正确缩进“导弹”部分:
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
另一个建议我会把你的敌人放在一个阵列中,让我们在开始时说“敌人”
var enemies:Array = new Array(); //initiate them now or later
然后你可以把敌人推进去,就像你对导弹一样
enemies.push(_root.some_newly_created_enemy);
然后再做
foreach(enemy in enemies){
if(this.hitTest(enemy)) {
enemy.play();}
}
而不是逐个静态地检查它们