How to pause 'if collision' in Flash CS5?

This is the collision

   if(blue2.hitTestObject(_helicopter))

            {
                trace("YOU HIT THE BLOCK!");
                ihit = true;
                _helicopter.x = 76;
                _helicopter.y = 217;
            }

Is it possible to add the pause function in that collision code^? Is so, what do I write there please? Thank you!

Answers


Pause is a bit difficult to interpret in your context.

For instance, if your game had an enter frame handler you could stop all game animation for a period of time. Or, maybe you want to freeze a "You hit the block!" message for a period of time while background animations still ran.

One approach is to use a timer. It's asynchronous, but the end result here is that a function is called after a period of time (milliseconds).

In this example, 5-seconds after your code is executed, an anonymous function responds that the time is up:

// needed imports:
import flash.events.TimerEvent;
import flash.utils.Timer;

if (blue2.hitTestObject(_helicopter))
{
    trace("YOU HIT THE BLOCK!");
    ihit = true;
    _helicopter.x = 76;
    _helicopter.y = 217;

    var timer:Timer = new Timer(5000); // 5-seconds
    timer.addEventListener(TimerEvent.TIMER, function(event:TimerEvent):void
    {
        timer.reset();
        timer.removeEventListener(TimerEvent.TIMER, arguments.callee);
        trace("5-seconds after hitting the block.");
    });
    timer.start();
}

If you don't like the anonymous function, you could implement it as:

// needed imports:
import flash.events.TimerEvent;
import flash.utils.Timer;

if (blue2.hitTestObject(_helicopter))
{
    trace("YOU HIT THE BLOCK!");
    ihit = true;
    _helicopter.x = 76;
    _helicopter.y = 217;

    var timer:Timer = new Timer(5000); // 5-seconds
    timer.addEventListener(TimerEvent.TIMER, collisionWaitHandler);
    timer.start();
}

// ... later in its own function:

protected function collisionWaitHandler(event:TimerEvent):void
{
    var timer:Timer = Timer(event.currentTarget);
    timer.reset();
    timer.removeEventListener(TimerEvent.TIMER, collisionWaitHandler);

    trace("5-seconds after hitting the block.");
}

Need Your Help

Copying folders simply C#

c# copy folder copy-paste

Im trying to create a simple program that to help learn c# and to be semi useful for my self and expandable.

How can I make a method with parameters creating an instance in Java?

java class methods

so I'm a newbie learning 'bout classes and object orienting in java. Each instance of a mobile saves the model, owner name and number. So in the main file I try to make a method with those paramete...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.