Bits & Bytes

Posts Tagged ‘computer’

Responding to Mouse Click Events in Actionscript 3.0

To respond to mouse events of any kind, we create a callback function that takes a MouseEvent and returns void. Then we call addEventListener() with the event and the callback function. Below, we show how this is done for MOUSE_DOWN and MOUSE_UP events, but the same method can be used for all of the mouse event types.

Inside the callback functions, we change the message that is displayed by the TextField and position the message to be displayed where the event occurred. If you click in the box above, you will see the message change and move as you press and release the left mouse button. In this example, we position the message using the stage coordinates. However, we can also use the members localX and localY to get the position based on the object that is listening for the events.

var qMessage:TextField = new TextField();
qMessage.text = "Click In Here";
addChild(qMessage);

stage.addEventListener(MouseEvent.MOUSE_DOWN, OnPress);
function OnPress(e:MouseEvent): void {
	qMessage.text = "Down";
	qMessage.x = e.stageX;
	qMessage.y = e.stageY;
}

stage.addEventListener(MouseEvent.MOUSE_UP, OnRelease);
function OnRelease(e:MouseEvent): void {
	qMessage.text = "Up";
	qMessage.x = e.stageX;
	qMessage.y = e.stageY;
}

Keeping the C++ Console Window Open

One of the first problems that new C++ programmers have is keeping the console window open when writing C++ programs. The easiest solution to this problem is to use the Start Without Debugging option under the Debug the when executing programs. Unfortunately, Microsoft took this option and many others out of the default menus in Visual C++ 2010. To get this option back, select Tools->Settings->Expert Settings. Otherwise, you can use press (Ctrl + F5) to select Start Without Debugging without the menu.

That’s the simplest option for keeping the console window open. However, if you want to keep the window open when running an executable that you create, you will need to add some code to suspend execution and keep the window open. Below, we show one example of how to keep the window open by adding this line of code before the return statement:

system("pause");

The one objection I have to this method is that the system() function is not part of the C++ standard and may not be valid with some C++ compilers.

#include <iostream>

int main()
{
    using namespace std;

    cout << "Hello World!" << endl;

    system("pause");
    return 0;
}

Alternatively, we could use cin.get(); to keep the window open like this:

#include <iostream>

int main()
{
    using namespace std;

    cout << "Hello World!" << endl;

    cin.get();
    return 0;
}

However, using cin.get() can have problems if input is taken directly before it. The problem is that the input in the stream carries over to the cin.get() and causes the program to exit. To prevent this, we can add a call to clear() and ignore(), as we do below.

#include <iostream>

int main()
{
    using namespace std;

    int iInt;
    cin >> iInt;
    cout << "Input = " << iInt << endl;

    cin.clear();
    cin.ignore(0xFFFFFFFF, '\n');
    cin.get();
    return 0;
}

Creating a Timed Callback Function in Actionscript 3.0

Below, we have an example program, which creates a timer callback function. This program draws a circle that flashes in the middle of the screen at a rate that is specified by the timer that we create. The code sets a timer to call a function that, alternatively, adds and removes a circle from the objects that are to be displayed.

The first three lines of active code initialize the Timer. The first line creates an instance of a Timer that creates a TIMER event every 1000 milliseconds, which equals 1 second. The second line sets the function FlashCircle() to get called every time a TIMER event occurs. The third line starts the Timer so that it generates events every second.

// Create a 1 second timer
var qMyTimer:Timer = new Timer(1000);
qMyTimer.addEventListener(TimerEvent.TIMER, FlashCircle);
qMyTimer.start();

var bDrawCircle:Boolean = true;

// Create a sprite with a circle drawn on it.
var qCircle:Sprite = new Sprite();
var uiColor:uint = 0x00800000;
qCircle.graphics.beginFill(uiColor);
    qCircle.graphics.drawCircle(160, 120, 100);
qCircle.graphics.endFill();

function FlashCircle(e:TimerEvent):void {
    // Add or remove the circle from the stage
    if (bDrawCircle) {
        addChild(qCircle);
    } else {
        removeChild(qCircle);
    }
    // Alternate the drawing
    bDrawCircle = !bDrawCircle;
}

Once the timer is initialized, we have a Boolean, bDrawCircle, that is used to toggle the display of the circle. Below this, we create a Sprite and draw a dark red circle on it. The circle is located at (160, 120) and has a radius of 100.

Finally, we have the callback function FlashCircle(). This function is called every second via a TIMER event. Inside the function, we add or remove the Sprite with the circle drawn on it from the list of items to be displayed. The last line reverses the value of the Boolean, bDrawCircle, to alternate it value between true and false and toggle the drawing of qCircle.

var qMyTimer:Timer = new Timer(1000, 3);

The timer in the program runs continuously. We could set the second argument in the constructor to 3, as shown in the line above. With this constructor call, the timer only runs 3 seconds and generates 3 events. When we don’t specify a second argument, the default value of 0 is used, which sets the timer to run indefinitely.

When we have a finite number of events coming from the timer, we can set an event callback to run when the timer is completely finished. The event TimerEvent.TIMER_COMPLETE can be used instead of TimerEvent.TIMER for a callback that runs when the timer is completely finished.

 

© 2007–2024 XoaX.net LLC. All rights reserved.