ActionScript 3 If/ Else If Statement

ActionScript 3 If/ Else If Statement

本文关键字:If Statement Else ActionScript      更新时间:2023-09-26

我正在遵循本教程

优酷 :

https://www.youtube.com/watch?v=ohlrknEwobs&list=PLE10sFVGtI1ejzPFmX5CebHrTMhaCx2oI&index=31

这个概念是制作一个回避游戏,其中 3 个物体垂直下落,其中一个将增加分数,另外两个将结束游戏。

我在数组中创建了 4 个对象,如下所示:

var:objects Array = [new BlueBird(),new RedBird(),new Goomba(),new Ham()]

对象[0] = 是一只跳到场景 3("死亡场景"(的蓝鸟

对象[1] = 是一只跳到场景 3("死亡场景"(的红鸟

对象[2] = 是一个跳到场景 3("死亡场景"(的 Goomba

对象[3] = 是一块增加点数的火腿

这是我遇到麻烦的声明。我无法让对象[3],火腿的一块,来测试else if语句。我在 && objectsIndex == 3 中编码,但它不起作用:

if((Pig.hit).hitTestObject(objects[objectsIndex]))
   {
        // Reset objects to y positions
      objects[objectsIndex].x = randomRange(0,3) * 50
      objects[objectsIndex].y = -50;
      
      stage.removeEventListener(KeyboardEvent.KEY_DOWN,pressedButton);
      stage.removeEventListener(Event.ENTER_FRAME,CollisionSensor);
      stage.frameRate = 24;
      
      gotoAndStop(1,"GG WP");
      //trace ("you died!");
   }
else if((Pig.hit).hitTestObject(objects[objectsIndex]) && objectsIndex == 3)
    {
        
    score = score + 1;  //increase score by 1 point
        text1.text = String(score); // Score is now a string value
                                    // Since text1 is a text box, it must be denotated as a  string
        // Reset the objects to y positions
        objects[objectsIndex].x = randomRange(0,3) * 50;
        objects[objectsIndex].y = -50
    }

以下是供参考的整个项目代码:

    stop();
import flash.events.Event;
stage.focus = this;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressedButton);
stage.addEventListener(Event.ENTER_FRAME, CollisionSensor);
var score:Number = 0;   //Player Score
var enemy_velocity:Number = 5;
var objects:Array = [new RedBird(),new Goomba,new BlueBird(),new Ham()];
// objects[0] is a RedBird
// objects[1] is a Goomba
// objects[2] is a BlueBird
// objects[3] is an piece of Ham
var objectsIndex:Number = randomRange(0,3);     //Randomizer
                                                //Picks a random array value
for (var i:int=0; i<objects.length; i++)
{
    objects[i].x = randomRange(0,3) * 50;
    objects[i].y = -50;
    
    //Brings objects to stage
    stage.addChild(objects[i]);
}

//Generate a ranodom number from minNum to maxNum including the endpoints
function randomRange(minNum:Number, maxNum:Number):Number
{
    return (Math.floor(Math.random() * (maxNum - minNum + 1)) + minNum);
}

function CollisionSensor(e:Event)
{
    
    objects[objectsIndex].y = objects[objectsIndex].y + enemy_velocity;
    
    // check if enemy objects have gone past the bottom of the game screen
    if (objects[objectsIndex].y > stage.stageHeight)
    {
        objects[objectsIndex].y = -50; //Resets objects in array and parameters
                            // To enter frame in a repeated loop
                            
        //Randomizes a random x position for Objects
        var num:Number = randomRange(0,3);
        //This creates 4 random x positions - 0,50,100,150 To randomly place
        //The array objects in
        objects[objectsIndex].x = num * 50;
        
        // now pick a random object
        objectsIndex = randomRange(0,3);
        // objects[0] is a RedBird
        // objects[1] is a Goomba
        // objects[2] is a BlueBird
        // objects[3] is an piece of Ham
                                

                        
if(stage.frameRate <= 60)
{
    stage.frameRate = stage.frameRate + 5;
}
else
{
    stage.frameRate = 60;
    
    if (enemy_velocity <= 25)
    {
        enemy_velocity += 1; 
    }
    else
    {
        enemy_velocity = 25;
    }
  }
}
//check contanct with enemy objects

if((Pig.hit).hitTestObject(objects[objectsIndex]))
       {
            // Reset objects to y positions
          objects[objectsIndex].x = randomRange(0,3) * 50
          objects[objectsIndex].y = -50;
          
          stage.removeEventListener(KeyboardEvent.KEY_DOWN,pressedButton);
          stage.removeEventListener(Event.ENTER_FRAME,CollisionSensor);
          stage.frameRate = 24;
          
          gotoAndStop(1,"GG WP");
          //trace ("you died!");
       }

else if((Pig.hit).hitTestObject(objects[objectsIndex]) && objectsIndex == 3)
        {
            
        score = score + 1;
//increase score by 1 point
        `text1.text = String(score);` // Score is now a string value
                                    // Since text1 is a text box, it must be denotated as a string
        // Reset the objects to y positions
        
objects[objectsIndex].x = randomRange(0,3) * 50;
            objects[objectsIndex].y = -50
        }
}

function pressedButton(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.LEFT && Pig.x >= 0)
    {
        Pig.x = Pig.x - 50;
    }
    else if (event.keyCode == Keyboard.RIGHT && Pig.x <= 150)
    {
        Pig.x = Pig.x + 50;
    }
}

谢谢你,一如既往,祝你有美好的一天:)

您的代码可能有其他问题,但这看起来确实正确

if((Pig.hit).hitTestObject(objects[objectsIndex]))

应该是..

if((Pig).hitTestObject(objects[0]))

其中objects[0]是数组中要访问以进行测试的索引。但这甚至可能有问题,因此请获取您在舞台上添加的内容的特定名称,请在将对象添加到舞台的for loop中尝试此操作

//Brings objects to stage
stage.addChild(objects[i]);
trace ("added object name : " + objects[i] );

无论你在那里得到什么名字,你都会测试if( (Pig).hitTestObject(namehere) )