简体   繁体   中英

hitTestObject working on PC but not on android device

I am building an AIR ActionScript 3.0 application. Whenever i am trying it on the pc, by doing Command + Enter and testing the movie, the app will work fine, and a collision between two movie clips is working fine. However, when i try to publish it and get the appropriate apk, the app will run normally, but no collision will take place! as if the two movie clips just fly above each other and nothing happens ! While on the PC, its working perfectly! I am new to AS3, so anyone can point out where am i going wrong? This is the code i am using for the collision :

function ifHitAct(e:Event):void
{
    for each (var obstacle in runNow.manyObs)
    {
        //trace("the obstacle Index Created is" , parent.getChildIndex(runNow.obstacle));
        //parent.setChildIndex(runNow.obstacle ,1)
        if (MC1.hitTestObject(obstacle))
        {
            hit.play(0);
            runNow.manyObs.splice(runNow.manyObs.indexOf(obstacle), 1);

            //this.removeChild(obstacle);
            score++;
            scoreField.text = String(score);
            obstacle.alpha = 0;
        }
    }
}

the problem was actually a little weird, but it worked! If any future users notices the same problem, you should check you Music!! well, i am calling a sound effect when i wrote " hit.play(0) " . I haven't embed any mp3 file, so whenever i try to run my app on a real device, the compiler just breaks on that line, and simply bypasses all the rest of the code. So, when i embed the mp3 file, it all worked fine! To embed the mp3 file, just use the following structure :

 [Embed(source='/hit.mp3')] 

      private var MySound : Class;         
      private var sound : Sound;

and then initiate your var whenever you need the music to play :

function ifHitAct(e:Event):void
        {
            //

            for each (var obstacle in runNow.manyObs)
            {

                if (MC1.hitTestObject(obstacle))
                {


                    sound = (new MySound()) as Sound;
                    sound.play(0);

                    runNow.manyObs.splice(runNow.manyObs.indexOf(obstacle), 1);

                    score++;
                    scoreField.text = String(score);
                    obstacle.alpha = 0;



                }
            }



        }

And it will all work perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM