Actionscript 3 在for循环中的多个变量上创建冲突检测?
基本上,我正在尝试创建一个游戏,玩家必须躲避某些物品,到目前为止,我有一段代码,可以在舞台上随机添加3条鲨鱼 这个想法是,一旦玩家击中鲨鱼,他/她返回开始位置,我有一个动作脚本文件,其中包含鲨鱼的速度、速度等,每次程序运行时,鲨鱼都会出现在不同的位置 然而,当我尝试对鲨鱼进行碰撞测试时,只有一条鲨鱼响应,我无法确定如何使所有3条鲨鱼都影响玩家(square_mc)。任何帮助都将不胜感激Actionscript 3 在for循环中的多个变量上创建冲突检测?,actionscript-3,collision-detection,Actionscript 3,Collision Detection,基本上,我正在尝试创建一个游戏,玩家必须躲避某些物品,到目前为止,我有一段代码,可以在舞台上随机添加3条鲨鱼 这个想法是,一旦玩家击中鲨鱼,他/她返回开始位置,我有一个动作脚本文件,其中包含鲨鱼的速度、速度等,每次程序运行时,鲨鱼都会出现在不同的位置 然而,当我尝试对鲨鱼进行碰撞测试时,只有一条鲨鱼响应,我无法确定如何使所有3条鲨鱼都影响玩家(square_mc)。任何帮助都将不胜感激 //Pirate game, where you have to avoid particular objec
//Pirate game, where you have to avoid particular object and get to the finish line to move onto the final level.
stage.addEventListener(KeyboardEvent.KEY_DOWN, moveMode );
function moveMode(e:KeyboardEvent):void {
//movements for the pirate ship, this will allow the ship to move up,down,left and right.
if (e.keyCode == Keyboard.RIGHT) {
trace("right");
square_mc.x = square_mc.x + 25;
}
else if (e.keyCode == Keyboard.LEFT) {
trace("left");
square_mc.x = square_mc.x - 25;
}
else if (e.keyCode == Keyboard.UP) {
trace("up");
square_mc.y = square_mc.y - 25;
}
else if (e.keyCode == Keyboard.DOWN) {
trace("down");
square_mc.y = square_mc.y + 25;
}
}
//for.fla
//this program uses a for loop to create my Sharks
//a second for loop displays the property values of the sharks
function DisplayShark():void{
for (var i:Number=0;i<3;i++)
{
var shark:Shark = new Shark(500);
addChild(shark);
shark.name=("shark"+i);
shark.x=450*Math.random();
shark.y=350*Math.random();
}
}
DisplayShark();
for(var i=0; i<3;i++){
var currentShark:DisplayObject=getChildByName("shark"+i);
trace(currentShark.name+"has an x position of"+currentShark.x+"and a y position of"+currentShark.y);
}
//here we will look for colliosion detection between the two move clips.
addEventListener(Event.ENTER_FRAME, checkForCollision);
function checkForCollision(e:Event):void {
if (square_mc.hitTestObject(currentShark))
{
trace("The Square has hit the circle");
square_mc.x=50
square_mc.y=50 //these lines of code return the square back to it's original location
}
//海盗游戏,你必须避开特定的物体,到达终点线才能进入最后关卡。
stage.addEventListener(KeyboardEvent.KEY_DOWN,moveMode);
功能移动模式(e:键盘事件):无效{
//海盗船的移动,这将允许船上下左右移动。
if(e.keyCode==Keyboard.RIGHT){
跟踪(“权利”);
平方米x=平方米x+25;
}
else if(e.keyCode==Keyboard.LEFT){
痕迹(“左”);
平方米x=平方米x-25;
}
else if(e.keyCode==Keyboard.UP){
追踪;
平方毫米y=平方毫米y-25;
}
else if(e.keyCode==Keyboard.DOWN){
追踪;
平方米y=平方米y+25;
}
}
//佛罗里达州
//这个程序使用for循环来创建我的鲨鱼
//第二个for循环将显示鲨鱼的特性值
函数DisplayShark():void{
for(var i:Number=0;i只需将for循环移动到ENTER\u帧中:
addEventListener(Event.ENTER_FRAME, checkForCollision);
function checkForCollision(e:Event):void {
for(var i=0; i<3;i++){
var currentShark:DisplayObject=getChildByName("shark"+i);
if (square_mc.hitTestObject(currentShark))
{
trace("The Square has hit the circle");
square_mc.x=50;
square_mc.y=50;
}
}
}
addEventListener(Event.ENTER\u FRAME,checkForCollision);
函数checkForCollision(e:Event):void{
对于(var i=0;我太感谢你了!不知道为什么我没有想到这一点,现在看起来很明显!感谢你的快速响应;)