Search code examples
actionscript-3actionscriptadobeflash

Simple Points & Lives Displaying Incorrectly


so I'm working on this simple game, in which you dodge falling boulders. Every time a boulder doesn't hit you (reaches a y coordinate below) you get 30 pts. And when a boulder does hit you you lose a life. Unfortunately, it seems to glitch out seemingly unpredictable.

LINK TO TEST OUT THE GAME: http://fozgamez.com/a (only the 1p mouse works)

I do not know how to fix the problem, since I cannot figure out how/ when the problem happens.

My code for the 2nd scene (the one with the rules):

import flash.events.MouseEvent;

stop();
var livesSelected:Number;

m1Select.addEventListener(MouseEvent.MOUSE_UP, m1Selected)
function m1Selected (e:MouseEvent)
{
    livesSelected = 01;
    gotoAndStop(3);
}

m3Select.addEventListener(MouseEvent.MOUSE_UP, m3Selected)
function m3Selected (e:MouseEvent)
{
    livesSelected = 03;
    gotoAndStop(3);
}

m5Select.addEventListener(MouseEvent.MOUSE_UP, m5Selected)
function m5Selected (e:MouseEvent)
{
    livesSelected = 05;
    gotoAndStop(3);
}

m9Select.addEventListener(MouseEvent.MOUSE_UP, m9Selected)
function m9Selected (e:MouseEvent)
{
    livesSelected = 09;
    gotoAndStop(3);
}

CODE FOR THE 3RD SCENE (where you actually play the game):

import flash.events.Event;
import flash.events.TouchEvent;
import flash.events.MouseEvent;
import flash.utils.Timer;
var points:int = 0;
var lifeTimer:Timer = new Timer(1000, 1)
var lives:Number = livesSelected;

livesText.text = lives.toString();
pointsText.text = points.toString();


lifeTimer.stop()

stage.addEventListener(Event.ENTER_FRAME, enterFrame);
function enterFrame(e:Event){
        mChar.x = mouseX;
        mChar.y = mouseY;



        b1.y += 20;
        b2.y += 40;
        b3.y += 15;
        b4.y += 25;
        b5.y += 20;
        bFast.y += 50;

        if(mChar.y <= 20)
        {
            mChar.y = 20;
        }
        if(mChar.x >= 700)
        {
            mChar.x = 700;
        }
        if(mChar.y <= 0)
        {
            mChar.y = 700;
        }

        if(b1.y >= 730) {
            b1.y = (Math.random() + .001) * -200;
            b1.x = (Math.random() + .001) * 700;
            points += 15;
        }

        if(b2.y >= 730) {
            b2.y = (Math.random() + .001) * -200;
            b2.x = (Math.random() + .001) * 700;
            points += 30;
        }
        if(b3.y >= 730) {
            b3.y = (Math.random() + .001) * -200;
            b3.x = (Math.random() + .001) * 700;
            points += 15;
        }
        if(b4.y >= 730) {
            b4.y = (Math.random() + .001) * -200;
            b4.x = (Math.random() + .001) * 700;
            points += 15;
        }
        if(b5.y >= 730) {
            b5.y = (Math.random() + .001) * -200;
            b5.x = (Math.random() + .001) * 700;
            points += 15;
        }
        if(bFast.y >= 730) {
            bFast.y = (Math.random() + .001) * -200;
            bFast.x = (Math.random() + .001) * 700;
            points += 15;
        }

if(!lifeTimer.running) {
        livesText.text = lives.toString();
        mInvin.x = -66;
        mInvin.y = 560;
        pointsText.text = points.toString();

            if(mChar.hitTestObject(b1)) {
            lives--;
            livesText.text = lives.toString();
            lifeTimer.start();

            }
            if(mChar.hitTestObject(b2)) {
            lives--;
            livesText.text = lives.toString();
            lifeTimer.start();
            }
            if(mChar.hitTestObject(b3)) {
            lives--;
            livesText.text = lives.toString();
            lifeTimer.start();
            }
            if(mChar.hitTestObject(b4)) {
            lives--;
            livesText.text = lives.toString();
            lifeTimer.start();
            }
            if(mChar.hitTestObject(b5)) {
            lives--;
            livesText.text = lives.toString();
            lifeTimer.start();
            }
            if(mChar.hitTestObject(bFast)) {
            lives--;
            livesText.text = lives.toString();
            lifeTimer.start();  
            }
                if(lives <= 0)
                    {
                    gotoAndStop(7);
                    }
        }

            if(lifeTimer.running)
            {
                mInvin.x = mChar.x;
                mInvin.y = mChar.y;
            }

        }

Thanks for reading: I know this is kind of a tough problem to figure out, so thanks for the help!


Solution

  • Your problem here, from what I can derive from your code, is your event listeners. I don't know what code you have on frame 7, but unless you remove the event listeners, they will keep on listening and running code, even though you have moved the playhead forward on the timeline (e.g. when calling gotoAndStop())