mercredi 17 février 2016

Delete dynamically created wordsearch when moving to next frame AS3

I use this code to create a wordsearch at frame 40 in my game. I also have a back button in that frame. I try my wordsearch and it works fine but when I go back to frame 1 I can still see my wordsearch. How can I delete it when leaving frame 40?

import fl.controls.Button;
import fl.transitions.Tween;
import fl.transitions.easing.*;



// paramas
const LINE_WIDTH:int = 35;

// define word seach grid and solutions
var wordSearch:Array = [
    ["Α", "Ν", "Δ", "Π", "Ο", "Λ", "Ε", "Μ", "Ι", "Σ", "Τ", "Ρ", "Α"],
    ["Τ", "Φ", "Ρ", "Α", "Γ", "Κ", "Ο", "Ι", "Ρ", "Χ", "Δ", "Α", "Ι"],
    ];

var solutions:Array = [
    [[3, 0], [12, 0]], // (1,4) to (5,4) = ΠΟΛΕΜΙΣΤΡΑ
    [[2, 6], [8, 6]], // (1,4) to (5,4) = ΦΡΟΥΡΙΑ
]

// build word search buttons
var i:int = 0, j:int = 0, posX:int = 0, posY:int = 0, buttonSize:int = 35;
var buttonTextFormat:TextFormat = new TextFormat("Arial,Verdana",16,0x333333,true);
while(i < wordSearch.length){
    posX = 0;
    j = 0;
    posY = i * buttonSize;

    while(j < wordSearch[0].length) {
        posX = j * buttonSize;

        if (wordSearch[i][j] == "%")
            wordSearch[i][j] = String.fromCharCode(97 + int(26 * Math.random())); // random a-z lowercase

        var b:Button = new Button();
        b.setSize(buttonSize, buttonSize);
        b.move(posX +300, posY+20);
        b.label = wordSearch[i][j];
        b.name = "btn" + j + "_" + i;
        b.setStyle("textFormat",buttonTextFormat);
        addChild(b);

        j++;
    }
    i++;
}

// this graphic will be used to draw graphics around the solved words
var solvedWords:Shape = new Shape();
addChild(solvedWords);

// start and end points when searching for a solution
var startLetter:Array, endLetter:Array;
var startButton:Button, endButton:Button;

// event listeners for click and drag
addEventListener("mouseDown",mouseDown);

function mouseDown(event:MouseEvent){
    stage.addEventListener("mouseMove",mouseMove);
    stage.addEventListener("mouseUp",mouseUp);
    startLetter = getLetterFromName(event.target.name);
    startButton = event.target as Button;
    endButton = startButton;
    updateDrag();
}

function mouseMove(event:MouseEvent){
    updateDrag();
}

function mouseUp(event:MouseEvent){
    checkForSolution();
    stage.removeEventListener("mouseMove",mouseMove);
    stage.removeEventListener("mouseUp",mouseUp);
}

// update the dragging interaction
function updateDrag(){
    // find the button currently under the mouse
    var objects:Array = getObjectsUnderPoint(new Point(mouseX,mouseY));
    while(objects.length && !(objects[0].parent.parent is Button))
        objects.shift();
    if(objects.length)
        var btn:Button = objects[0].parent.parent as Button;

    // if there's a button under the mouse, check to see if it's on a good axis (vertical, horizontal, diaganal
    if(btn){
        var letter:Array = getLetterFromName(btn.name);
        if(letter[0] == startLetter[0] || letter[1] == startLetter[1]
        || Math.abs(letter[0] - startLetter[0]) == Math.abs(letter[1] - startLetter[1])){
            endLetter = letter;
            endButton = btn;
        }
    }

    // draw a visual line between the start button and the end button
    graphics.clear();
    graphics.lineStyle(LINE_WIDTH,0x0077ff);
    graphics.moveTo(startButton.x + startButton.width/2,startButton.y + startButton.height/2);
    graphics.lineTo(endButton.x + endButton.width/2,endButton.y + endButton.height/2);
}

// check to see if the current dragging operation has found a solution
var fadeTween:Tween;
function checkForSolution(){
    // clear temporary visual indicator of user select start/end letter
    graphics.clear();

    // check each solution to see if it matches what user selected
    for(var i in solutions){
        if((compareLetters(solutions[i][0], startLetter) && compareLetters(solutions[i][1], endLetter))
        || (compareLetters(solutions[i][0], endLetter) && compareLetters(solutions[i][1], startLetter))){
            // solution found! 

            // draw perminant graphic around them
            with(solvedWords){
                graphics.lineStyle(LINE_WIDTH,0x0077ff,.25);
                graphics.moveTo(startButton.x + startButton.width/2,startButton.y + startButton.height/2);
                graphics.lineTo(endButton.x + endButton.width/2,endButton.y + endButton.height/2);
            }

            // remove the solution so that there are no repeats
            solutions.splice(i,1);

            break;
        }
    }

    // if all solutions have been found, show a "YOU WIN!" graphic
    if(!solutions.length){
        var w:int = 400, h:int = 200;
        var win:Sprite = new Sprite();
        addChild(win);

        // draw rounded rect with subtle vertical linear gradient fill and blue stroke
        win.graphics.lineStyle(4,0xF5DA41);
        var mat:Matrix = new Matrix();
        mat.createGradientBox(w, h, 90 * (Math.PI / 180));
        win.graphics.beginGradientFill(GradientType.LINEAR,[0xffffff,0xeeeeee],[1.00,1.00],[0,255],mat);
        win.graphics.drawRoundRect(0,0,w,h,15,15);

        // show center "YOU WIN!" text
        var tf:TextField = new TextField();
        tf.autoSize = TextFieldAutoSize.LEFT;
        tf.antiAliasType = AntiAliasType.ADVANCED;
        tf.defaultTextFormat = new TextFormat("Arial, Verdana",36,0x454545,true);
        tf.text = "Κέρδισες!";
        tf.selectable = false;
        win.addChild(tf);
        tf.x = w/2 - tf.width/2;
        tf.y = h/2 - tf.height/2;

        // add a drop shadow
        var dropShadow:DropShadowFilter = new DropShadowFilter(3,45,0,.35,8,8,1,3);
        win.filters = [dropShadow];

        // center the graphic
        win.x = stage.stageWidth/2 - win.width/2;
        win.y = stage.stageHeight/2 - win.height/2;

        // fade the graphic in
        fadeTween = new Tween(win,'alpha',None.easeNone,0.00,1.00,.5,true);

        // remove interaction from cross word
        removeEventListener("mouseDown",mouseDown);
    }
}

// gets a letter array like [x,y] from a Button name like "btnX_Y"
function getLetterFromName(name:String):Array{
    var newName:Array = name.split("btn")[1].split("_")
    newName[0] = int(newName[0]);
    newName[1] = int(newName[1]);
    return newName;
}

// compare two letter arrays to see if they are the same
function compareLetters(letter1, letter2){
    return letter1[0] == letter2[0] && letter1[1] == letter2[1]
}


start6_btn.addEventListener(MouseEvent.CLICK, fl_ClickToGoToAndStopAtFrame_16);

function fl_ClickToGoToAndStopAtFrame_16(event:MouseEvent):void
{
    gotoAndStop(1);

}

Aucun commentaire:

Enregistrer un commentaire