2013-04-24 68 views
0

我有一个动作脚本3.重复播放功能,当满足条件

我想提出一个问题的游戏,游戏的基本规则是:

  1. 对象从顶部落下
  2. 英雄(用户)必须避开物体
  3. 如果物体撞击地面或英雄:英雄死亡或物体再次从顶部跌落。

我正在使用添加子对象的方法,并为秋季计时器功能。

问题是: 当物体碰到地面时,函数不会循环。它就这样结束。所以不会再有任何坠落的物体了。

请帮帮我。谢谢:)

stage.addEventListener(Event.ENTER_FRAME, addfire1); 

function addfire1(e:Event):void 
{ 
    if (api1==false)//if object is not on the stage 
    { 
     randomx = randomRange();//generate random X 
     addChild(api); 
     api.x = randomx;//set x 
     api1 = true;//object is now on stage 
    } 
    if (api.hitTestObject(hero) || api.hitTestObject(ground)) 
    { 
     falltimer.stop(); 
     //stop timer; 
     falltimer.reset(); 
     //reset fall Count to zero ; 
     removeChild(api);//object removed 
     api1=false; 
    } 
} 

function firefall(Event:TimerEvent):void 
{ 
    if (api1) 
    { 
     api.y += 20;//set speed y 
    } 
} 
+0

何必删除/每撒在地板上的时间增加了对象?只需将相同的对象移回顶端:'api.y = 0;' – 2013-04-24 09:35:27

+0

感谢您的帮助,它的工作:) – Briandito 2013-04-24 13:56:09

回答

2

只是分开两种情况:英雄和地板。

if (api.hitTestObject(hero)) 
{ 
    falltimer.stop(); 
    //stop timer; 
    falltimer.reset(); 
    //reset fall Count to zero ; 
    removeChild(api);//object removed 
    api1=false; 
} 
else if (api.hitTestObject(ground)) 
{ 
    //reset object's position 
    api.y = -20; 
} 
+0

非常感谢!它的作用就像一种魅力 – Briandito 2013-04-24 13:56:51

0

假设只有一个落下的时候我不能去除异物而只是将其移动到原始y位置和新的x位置的对象。而不是有计时器,我会创建一个更新函数,每次输入框架时都会运行。

stage.addEventListener(Event.ENTER_FRAME, update); 

private function update(e:Event):void 
{ 
    if(!api1) //if object is not on the stage (same as api1==false) 
    { 
     addfire1(); 
    } 

    if(api1) //if object if on stage (same as api1==true) 
    { 
     firefall(); 
    } 
} 

private function addfire1(e:Event):void 
{ 
    randomx = randomRange();//generate random X 
    addChild(api); 
    api.x = randomx;//set x 
    api1 = true;//object is now on stage 

    if (api.hitTestObject(hero)) 
    { 
     hitHero(); //execute what should happen to the hero when the he is hit 
     resetFire(); 
    } 
    else if(api.hitTestObject(ground)) 
    { 
     resetFire(); 
    } 
} 

private function firefall(Event:TimerEvent):void 
{ 
    api.y += 20;//set speed y 
} 

private function resetFire():void 
{ 
    api.x = randomRange(); 
    api.y = 0; 
} 

如果你这样写,你不必混合起来。尽量保持一切分离,一个功能做一件事。如果你正在做一个大型游戏,试着在课堂上把所有东西都分开。

希望这能解决问题,使您更容易完成游戏:)