user1884296
user1884296

Reputation: 35

Using two eventlisteners for one function in Actionscript

I'm making a dots and boxes game in flash using actionscript 3.

Currently I'm completely stuck as I want the player to click two buttons before a line is displayed. I planned to do something like this using the if statement

if button1 and button2 clicked 
line1 visible = true

I've also tried adding eventlisteners with one function

function showLine(e:Event):void {
blue0001.visible = true
}
dot00.addEventListener(MouseEvent.CLICK, showLine);

But as far as I know this can only be used when you want to click one button. Is there anyway to have two eventlisteners satisfyed before the function is carried out? Also how would I (if i can) use the if statements to carry out this?

Upvotes: 3

Views: 375

Answers (1)

sberry
sberry

Reputation: 132008

You would probably do something like this, pseudo-code:

Assume all of the dots are in a dots array.

for (var i: Number = 0; i < dots.length; i++) {
    dots.addEventListener(MouseEvent.CLICK, dotClicked, false, 0, true);
}

dotSelected = null;
function dotClicked(evt:MouseEvent):void {
    if (dotSelected && isNeighbor(evt.target, dotSelected)) {
        showLineConnecting(evt.target, dotSelected)
        dotSelected = null;
    } else if (!dotSelected) {
        highlightDot(evt.target);
        dotSelected = evt.target;
    } else {
        showError("You must click an adjacent dot");
    }
}

At the request of the OP, this is what's going on.

for (var i: Number = 0; i < dots.length; i++) {
    dots.addEventListener(MouseEvent.CLICK, dotClicked, false, 0, true);
}

Add an event listener to every dot. Here I am assuming you already have an array of dots defined. Each instance in the Array would be a MovieClip (likely), Sprite, or another DisplayObject.

dotSelected = null;

We will use a variable to keep track of any currently selected dot. Since no dot will be selected when the game starts, we set it to null.

function dotClicked(evt:MouseEvent):void {
    if (dotSelected && isNeighbor(evt.target, dotSelected)) {
        showLineConnecting(evt.target, dotSelected)
        dotSelected = null;
    } else if (!dotSelected) {
        highlightDot(evt.target);
        dotSelected = evt.target;
    } else {
        showError("You must click an adjacent dot");
    }
}

This is the function that will get called when any dot is clicked. For explanation's sake, let's take the first click of the game. dotSelected is null, so the first if is false. The second if though is true, because (!dotSelected) is true. So, we run some function I called highlightDot with the dot as the argument. That function could look something like this:

function hightlightDot(dot:Dot):void {
    dot.gotoAndStop("selected");
}

Now a second click is made. Now the first part of the first if, dotSelected, is true. The second part is now evaluated. Again I put in a made up function isNeighbor. The isNeighbor function takes two arguments, the dot that was just clicked and the dot that has already been clicked. This function needs to make sure the two dots are adjacent. This could be something like...

function isNeighbor(dot1:Dot, dot2:Dot):void {
    return ((dot1.xGrid == dot2.xGrid && Math.abs(dot1.yGrid - dot2.yGrid) == 1) || (Math.abs(dot1.xGrid - dot2.xGrid) == 1) && dot1.yGrid == dot2.yGrid));
}

The above function assumes that the instances of Dot have some properties xGrid and yGrid which defines where in the playing board they sit. If they are in the same row and 1 column apart they are neighbors. If they are in the same column and 1 row apart they are neighbors.

The last thing that happens in a function showLineConnecting is called. That function will again take the two adjacent dots as arguments. It will then draw a line between them in whatever way you choose to do that. Finally, dotSelected is set back to null, allowing another set of dots to be selected.

One thing I just realized, it would probably be helpful to have an additional property that gets triggered on a Dot when it is connected to all of its neighbors so it could no longer be selected.

You will also need logic to handle knowing that a box has been created. For that you would likely just iterate the possibilities given the line that was just drawn. For each line drawn there are only two possible boxes that have been created. So check them both. Watch out for edges.

Upvotes: 3

Related Questions