Question

Hey everyone so I am having some trouble here. Been at it for an hour now and can't find a solution.

So I Have a movie clip named _Bunny added to the stage like so:

        _Bunny = new mcBunny;
        stage.addChild(_Bunny);
        _Bunny.x = (stage.stageWidth / 2) - 225;
        _Bunny.y = (stage.stageHeight / 2) - 330;

Now what this _Bunny does is move across the stage horizontally from right to left in a loop which i have set up like so in a Enter_Frame event listener:

private function bunnyView():void 
    {
        _Bunny.x += nBunnySpeed;

        if (_Bunny.x >=(stage.stageWidth / 2) + 215)
        {
            _Bunny.gotoAndStop("leftView");
            nBunnySpeed--;

        }
        if (_Bunny.x <=(stage.stageWidth / 2) - 215)
        {
            _Bunny.gotoAndStop("rightView");
            nBunnySpeed++;
        }


    }

It's speed is the nBunnySpeed which is equal to 5. Now I have another function that I am trying to change the value of the nBunnySpeed to say 20 whenever the nScore is equal to 1 like so:

private function updateDifficulty():void 
    {
        if (nScore >= 1)
        {
            //Increase Speed

            nBunnySpeed = 20;


        }

but the bug that in which is produces is the Bunny shooting off to the right side of the screen which is the "+x" no matter what I do this always happens.

Can anyone see what I might be doing wrong? I don't understand why this is happening. Please help!

Was it helpful?

Solution

You need some kind of a flag that indicates that the difficulty has been already updated. Then, when you call updateDifficulty it first checks if there's a real need to update speed now, if there's none, it just returns. If yes, however, then you update your bunny's speed and set that flag so that the next time the function will not alter the bunny's speed.

var diffUpdated:Boolean=false;
private function updateDifficulty():void 
{
    if (diffUpdated) return; // here
    if (nScore >= 1)
    {
        //Increase Speed
        if (nBunnySpeed<0) nBunnySpeed=-20;
          else nBunnySpeed = 20; // retain the direction of bunny's movement
    }
    diffUpdated=true;
}

Now, whenever you want your difficulty to be updated, you do diffUpdated=false; and voila, bunny's speed will be updated by this. For this, however, you will need more than two levels of speed, maybe one for 10 score, one for 50 and one for say 200.

OTHER TIPS

What you do in this function

private function bunnyView():void 
{
    _Bunny.x += nBunnySpeed;

    if (_Bunny.x >=(stage.stageWidth / 2) + 215)
    {
        _Bunny.gotoAndStop("leftView");
        nBunnySpeed--;

    }
    if (_Bunny.x <=(stage.stageWidth / 2) - 215)
    {
        _Bunny.gotoAndStop("rightView");
        nBunnySpeed++;
    }
}

is check whether the _Bunny is offscreen or not. And if so, the nBunnySpeed will be nBunnySpeed - 1. But since BunnySpeed = 20, it will be 20 + 19 + 18 + 17, still going right. If you'd to turn it to BunnySpeed = -BunnySpeed, it will reverse immediately and go back.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top