Search code examples
actionscript-3flash

Flash - Clip is repeating and not stopping


I made a memory game. There is some fish in a game, moving randomly until a stop. Player has to try remembering first one's movements. I converted a fish object to button so it's not a movieClip.

The fish appear perfectly but won't stop. I use stop(); within the last frame but it's not working. How I can stop this?

I use this code for Fish 1

var xsekseni:Number=10;

var ysekseni:Number=14;

var speedy:Number=75;

stage.addEventListener(Event.ENTER_FRAME,bhareket);

function bhareket(oly:Event) {

    balik1.x+=xsekseni;

    balik1.y+=ysekseni;

    if ((balik1.x>=stage.stageWidth-balik1.width/2)|| (balik1.x <= balik1.width/2 )) {

        xsekseni*=-1;

    }

    if ((balik1.y>=stage.stageHeight-balik1.height/2)|| (balik1.y <= balik1.height/2 )) {

        ysekseni*=-1;

    }

}



var MovieArray:Array = [balik1];

for (var j:uint = 0; j < MovieArray.length; j++) {

MovieArray[j].x = Math.floor( Math.random()*(stage.stageWidth - MovieArray[j].width) );

MovieArray[j].y = Math.floor( Math.random()*(stage.stageHeight - MovieArray[j].height) );

}

function MovieRandomPositon():void {

MovieRandomPositon();

}

code for Fish 2

var mvkk:Array = [balik2];

for (var i:uint = 0; i < mvkk.length; i++) {

mvkk[i].x = Math.floor( Math.random()*(stage.stageWidth - mvkk[i].width) );

mvkk[i].y = Math.floor( Math.random()*(stage.stageHeight - mvkk[i].height) );

}

function MovieR():void {

MovieR();

}



var xekseni:Number=10;

var yekseni:Number=7;

var speed:Number=75;

stage.addEventListener(Event.ENTER_FRAME,hareket);

function hareket(oly:Event) {

    balik2.x+=xekseni;

    balik2.y+=yekseni;

    if ((balik2.x>=stage.stageWidth-balik2.width/2)|| (balik2.x <= balik2.width/2 )) {

        xekseni*=-1;

    }

    if ((balik2.y>=stage.stageHeight-balik2.height/2)|| (balik2.y <= balik2.height/2 )) {

        yekseni*=-1;

    }

}

Solution

  • Are you moving both Fish by code only? These two lines might be causing the same movement you want to stop...

    stage.addEventListener(Event.ENTER_FRAME, bhareket);
    

    and

    stage.addEventListener(Event.ENTER_FRAME, hareket);
    

    I haven't put code in different frames for many years so I can't be sure if the below code will work. Actually it would be correct if your code was all in one place but if lucky, it might work on your setup...

    Try using code below on last frame.

    stop();
    stage.removeEventListener(Event.ENTER_FRAME, hareket);
    stage.removeEventListener(Event.ENTER_FRAME, bhareket);