如何检查多个符号的冲突
How to check collision for multiple symbols
我想知道是否可以通过实例名称而不是单独的 mc 名称来检查冲突。我有大约 150-200 个对象(吃豆子游戏的点)需要检查碰撞并希望有效地进行。谢谢!
如果您有名为 dots 的实例和一个玩家,您可以这样做:
//a var to hold each loop iteration's dot for convenience
var tmpDot:DisplayObject;
//loop 200 times from 1 - 200
for(var i:int=1;i<= 200;i++){
//getChildByName gets an instance from a string, in this case dot plus i (i is the current iteration number)
tmpDot = getChildByName("dot" + i);
//check if the dot exists and is hitting the player
if(tmpDot && tmpDot.hitTestObject(player)){
//hit a dot, do something here like remove the dot
removeChild(tmpDot);
//increment points etc.
//if there's no possibility of the player hitting more than one dot at a time, then for efficiency you should break out of this loop
break;
}
}
现在,正如你的问题的评论中提到的,给 200 个点一个实例名称是乏味的。一种更简单的方法是将您的点 MovieClip 放入您的库中,转到它的属性,然后将其导出以用于 actionscript(假设您为它指定了一个 class Dot
的名称)。然后你可以做的是,在一个级别的开始找到你在时间轴上的所有点对象(不需要实例名称)并将它们添加到数组中:
//DO THIS ONLY WHEN THE LEVEL STARTS
//create a vector/array to store all your dots for better speed
var allDots:Vector.<Dot> = new Vector.<Dot>();
//iterate over all the children of this timeline frame
for(var i:int=0;i<numChildren;i++){
//if the item is a Dot, add it to the array
if(getChildAt(i) is Dot){
allDots.push(getChildAt(i) as Dot);
}
}
现在,您可以像这样进行命中测试:
//YOU PROBABLY WANT TO DO THIS EITHER EVERY FRAME, OR WHENEVER THE PLAYER MOVES
//flag to see if all dots are eaten
var allEaten:Boolean = true;
var tmpDot:Dot;
for(var i:int=0;i<allDots.length;i++){
tmpDot = allDots[i];
//.... same as the top code example at this point
if(tmpDot && tmpDot.hitTestObject(player)){
removeChild(tmpDot);
//do anything else you need to do when a dot is eaten
//if we've already determined that we haven't eaten all the dots, then break the loop
if(!allEaten) break;
}
//if a dot has a parent, then they haven't been all eaten
if(tmpDot.parent){
allEaten = false;
}
}
我想知道是否可以通过实例名称而不是单独的 mc 名称来检查冲突。我有大约 150-200 个对象(吃豆子游戏的点)需要检查碰撞并希望有效地进行。谢谢!
如果您有名为 dots 的实例和一个玩家,您可以这样做:
//a var to hold each loop iteration's dot for convenience
var tmpDot:DisplayObject;
//loop 200 times from 1 - 200
for(var i:int=1;i<= 200;i++){
//getChildByName gets an instance from a string, in this case dot plus i (i is the current iteration number)
tmpDot = getChildByName("dot" + i);
//check if the dot exists and is hitting the player
if(tmpDot && tmpDot.hitTestObject(player)){
//hit a dot, do something here like remove the dot
removeChild(tmpDot);
//increment points etc.
//if there's no possibility of the player hitting more than one dot at a time, then for efficiency you should break out of this loop
break;
}
}
现在,正如你的问题的评论中提到的,给 200 个点一个实例名称是乏味的。一种更简单的方法是将您的点 MovieClip 放入您的库中,转到它的属性,然后将其导出以用于 actionscript(假设您为它指定了一个 class Dot
的名称)。然后你可以做的是,在一个级别的开始找到你在时间轴上的所有点对象(不需要实例名称)并将它们添加到数组中:
//DO THIS ONLY WHEN THE LEVEL STARTS
//create a vector/array to store all your dots for better speed
var allDots:Vector.<Dot> = new Vector.<Dot>();
//iterate over all the children of this timeline frame
for(var i:int=0;i<numChildren;i++){
//if the item is a Dot, add it to the array
if(getChildAt(i) is Dot){
allDots.push(getChildAt(i) as Dot);
}
}
现在,您可以像这样进行命中测试:
//YOU PROBABLY WANT TO DO THIS EITHER EVERY FRAME, OR WHENEVER THE PLAYER MOVES
//flag to see if all dots are eaten
var allEaten:Boolean = true;
var tmpDot:Dot;
for(var i:int=0;i<allDots.length;i++){
tmpDot = allDots[i];
//.... same as the top code example at this point
if(tmpDot && tmpDot.hitTestObject(player)){
removeChild(tmpDot);
//do anything else you need to do when a dot is eaten
//if we've already determined that we haven't eaten all the dots, then break the loop
if(!allEaten) break;
}
//if a dot has a parent, then they haven't been all eaten
if(tmpDot.parent){
allEaten = false;
}
}