HitTestObject选择哪一个

时间:2015-12-11 12:37:59

标签: actionscript-3

我一直在为我班上的动作3中的这个游戏工作。当我从树中掉落并使用HitTestObject函数添加一个点时,我的游戏会将水果捕获到一个篮子中。当被称为圆形的黑色水果击中篮子时,它应该消失一点。在使用索引数组和if / else语句的大多数其他编程语言中,这似乎微不足道。请注意如何使用if / else语句设置带有currentFruit影片剪辑的比较语句。我以为我能做的就像

if(currentFruit.hitTestObject(basket_mc)!=5))
increase fruit
  else
decrease fruit

以下是我的代码。

function catchFruit(e:Event):void {

for (var i:int = fruitsOnstage.length-1; i > -1; i--) {
var currentFruit:MovieClip = fruitsOnstage[i];
currentFruit.y += currentFruit.speed;


if (currentFruit.y > stage.stageHeight - currentFruit.
 height) {
currentFruit.y = 0 - currentFruit.height;
fruitsLost++;
field2_txt.text = "Total Fruit Lost: " + fruitsLost;
}


if(currentFruit.hitTestObject(basket_mc)){      
fruitsCollected++;  

field1_txt.text = "Total Fruit Collected: " +
fruitsCollected;


removeChild(currentFruit);
fruitsOnstage.splice(i,1);

}


if (fruitsCollected >= 20) {
basket_mc.gotoAndStop(20);
} else if (fruitsCollected > 15) {
basket_mc.gotoAndStop(15);
} else if (fruitsCollected > 10) {
basket_mc.gotoAndStop(10);
} else if (fruitsCollected > 5) {
basket_mc.gotoAndStop(5);
}

}

if (fruitsOnstage.length <= 0) {
field1_txt.text = "You Win! You have collected enough fruit for dinner.";
field2_txt.text = "";

stage.removeEventListener(Event.ENTER_FRAME, catchFruit);
}


if (fruitsLost >= 20) {
field1_txt.text = "Sorry, you lose. You have lost too much fruit!";
field2_txt.text = "";

stage.removeEventListener(Event.ENTER_FRAME, catchFruit);


for (var j:int = fruitsOnstage.length-1; j > -1; j--) {
currentFruit = fruitsOnstage[j];
removeChild(currentFruit);
fruitsOnstage.splice(j,1);
}
}

2 个答案:

答案 0 :(得分:0)

hitTestObject()返回一个布尔值,因此将它与索引进行比较是没有意义的。您应首先检查是否发生了碰撞(使用hitTestObject),然后检查碰撞对象的身份。

您可以检查currentFruit对象到达篮子后的类型:

if(currentFruit.hitTestObject(basket_mc)){
    if(currentFruit is Circle){
        decreaseFruit()
    } else {
        increaseFruit()
    }
}

答案 1 :(得分:0)

aButton.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame);

这是完整的代码,你可以看到黑色水果是阵列的一部分,剩下的水果将会落下。这个游戏确实有效,你可以获得水果和得分,但是如果黑色水果击中篮子,我想要扣除这一点。我不确定如何确定哪种水果打到篮子里。现在知道它是一个布尔结果确实有助于不确定如何设置它。

function fl_ClickToGoToAndStopAtFrame(event:MouseEvent):void     {

var fruitArray:Array = new Array(Apple,Strawberry,Pear,Banana,
 Orange,Circle);
var fruitsOnstage:Array = new Array();
var fruitsCollected:int = 0;
var fruitsLost:int = 0;
for (var i:int = 0; i<20; i++) {
var pickFruit = fruitArray[int(Math.random() * fruitArray.
 length)];
var fruit:MovieClip = new pickFruit();
addChild(fruit);
fruit.x = Math.random() * stage.stageWidth;
fruit.y = Math.random() * -500;
fruit.speed = Math.random() * 15 + 5;
fruitsOnstage.push(fruit);
}
basket_mc.addEventListener(MouseEvent.MOUSE_DOWN, dragBasket);
stage.addEventListener(MouseEvent.MOUSE_UP, dragStop);
function dragBasket(e:Event):void {
basket_mc.startDrag();
}
function dragStop(e:Event):void {


basket_mc.stopDrag();
}
stage.addEventListener(Event.ENTER_FRAME, catchFruit);


function catchFruit(e:Event):void {

for (var i:int = fruitsOnstage.length-1; i > -1; i--) {
var currentFruit:MovieClip = fruitsOnstage[i];
currentFruit.y += currentFruit.speed;


if (currentFruit.y > stage.stageHeight - currentFruit.
 height) {
currentFruit.y = 0 - currentFruit.height;
fruitsLost++;
field2_txt.text = "Total Fruit Lost: " + fruitsLost;
}


if(currentFruit.hitTestObject(basket_mc)){      
fruitsCollected++;  

field1_txt.text = "Total Fruit Collected: " +
fruitsCollected;


removeChild(currentFruit);
fruitsOnstage.splice(i,1);

}


if (fruitsCollected >= 20) {
basket_mc.gotoAndStop(20);
} else if (fruitsCollected > 15) {
basket_mc.gotoAndStop(15);
} else if (fruitsCollected > 10) {
basket_mc.gotoAndStop(10);
} else if (fruitsCollected > 5) {
basket_mc.gotoAndStop(5);
}

}

if (fruitsOnstage.length <= 0) {
field1_txt.text = "You Win! You have collected enough fruit for dinner.";
field2_txt.text = "";

stage.removeEventListener(Event.ENTER_FRAME, catchFruit);
}


if (fruitsLost >= 20) {
field1_txt.text = "Sorry, you lose. You have lost too much fruit!";
field2_txt.text = "";

stage.removeEventListener(Event.ENTER_FRAME, catchFruit);


for (var j:int = fruitsOnstage.length-1; j > -1; j--) {
currentFruit = fruitsOnstage[j];
removeChild(currentFruit);
fruitsOnstage.splice(j,1);
}
}






}

}