0
votes

I'm trying to make a primitive game in Flash. I'm using AS3 to build it. So far everything I have coded is working fine. Until now, I've only had 3 frames. However, I am ready to move on so I added a fourth frame. But, when I test the animation, it skips from frame 2 to frame 4. I put a trace in Frame 3 to see if Flash was even running it, and the trace executes the trace so I know Flash isn't completely ignoring Frame 3. But, I have a stop(); at the end of frame 3 and I have a stop(); on Frame 4. So, I'm not sure why Frame 3 is being skipped. The game doesn't have any tweens or actual animations so it shouldn't be anything of that sort. The only interaction is clicking on dots. I've put the code for all 4 of my frames below (I'm not sure if this is frowned upon. If it is, please tell me and I'll remove it but I'm putting it because it seems like it may be helpful). I'm also uploading a link to my .FLA file in case someone wants to see the whole thing.

Frame 1:

import flash.events.MouseEvent;

var dotList = new Array(); var level:int = 10; var invisoDotList = new Array();
var loop:int;
for(loop = 0; loop < level; loop++)
{
    var dot:Dot = new Dot();
    var invisoDot:InvisoDot = new InvisoDot();
    var tester:Boolean = true;
    var xval:int = Math.floor(Math.random()*(1+520))+14;
    var looper:int = 0;
    while(looper < dotList.length)
    {
        if(Math.abs(xval - dotList[looper].x) > 30)//minimum spacing
        {
            looper++;
        }
        else
        {
            looper = 0;
            xval = Math.floor(Math.random()*(1+520))+14;
        }
    }
    dot.x = xval;
    dot.y = 187;
    invisoDot.x = xval;
    invisoDot.y = 187;
    invisoDot.alpha = 0;
    dotList[loop] = dot;
    invisoDotList[loop] = invisoDot;
    addChild(invisoDot);
    addChild(dot);
}
//trace(dotList); test to ensure that dots are added to the array
button.addEventListener(MouseEvent.CLICK, hideDots);

function hideDots(e:MouseEvent)
{
    for(var loop:int = 0; loop < dotList.length; loop++)
    {
        dotList[loop].alpha = 0;//make dots disappear
    }
    nextFrame();
}
stop();

Frame 2:

import flash.events.MouseEvent;

button.addEventListener(MouseEvent.CLICK, next);

function next(e:MouseEvent)
{
    nextFrame();
}
stop();

Frame 3:

import flash.events.MouseEvent;
removeChild(button);
var clicks:int = -1;
//trace(dotList.length);
stage.addEventListener(MouseEvent.CLICK, clickCount);
for(var loopvar:int = 0; loopvar < dotList.length; loopvar++)
{
    //trace("loop");
    dot = dotList[loopvar];
    invisoDot = invisoDotList[loopvar];
    dot.addEventListener(MouseEvent.CLICK, onClick);
    invisoDot.addEventListener(MouseEvent.CLICK, onClick);
    //trace("event");
}
//trace(dotList.length);
function onClick(e:MouseEvent)
{
    //e.currentTarget.alpha = .5;
    for(var hitcheck:int = 0; hitcheck < dotList.length; hitcheck++)
    {
        if(dotList[hitcheck].x == e.currentTarget.x)
        {
            dotList[hitcheck].alpha = 1;
        }
    }
    //trace("check");
}
function clickCount(e:MouseEvent)
{
    clicks++;
    //trace(clicks);
    var numChanged:int = 0;
    for(var index:int = 0; index < dotList.length; index++)//check whether the user has gotten all the dots
    {
        if(dotList[index].alpha == 1)
        {
            numChanged++;
        }
    }
    if(numChanged == level)//if the user has gotten all the dots
    {
        trace("next screen for sucess");
        trace(clicks);
    }
    else if((clicks - numChanged) >= 2)//this ends the session as soon as 2 mistakes are made
    {
        trace("next screen for failed number of clicks");
        trace(clicks);
    }
    /*else if((clicks - level) >= 2)//if the user has made too many mistakes. This ends the session after the maximum number of tries have been used
    {
        trace("next screen too many clicks");
        trace(clicks);
    }*/

}
trace("end");
stop();

Frame 4:

stop();

Link to the .FLA file: https://www.dropbox.com/s/x1vim49tnz227id/Game.fla

If any of the conventions I've used in this question are wrong or frowned upon, please let me know and I'll correct them. It's been over a year since I've last posted on StackOverflow.

1

1 Answers

2
votes

Does the same button instance exist on both frame 1 and 2?

If so you will end up with two click event handlers on the button on frame 2 (hideDots() and next()). If you click on the button then they both call nextFrame() which would skip frame 3.


Possible solutions:

  1. Remove the first event listener before moving to the next frame:

    button.addEventListener(MouseEvent.CLICK, hideDots);
    
    function hideDots(e:MouseEvent)
    {
        for(var loop:int = 0; loop < dotList.length; loop++)
        {
            dotList[loop].alpha = 0;//make dots disappear
        }
    
        // Remove the event listener here:
        button.removeEventListener(MouseEvent.CLICK, hideDots);
    
        nextFrame();
    }
    

    OR

  2. Have different instances of the button on frame 1 and 2. You can do this by having a keyframe for the button on frame 1 and 2 - Flash will create a new instance of the button when it hits that frame.