Avatar billede christoffer535 Nybegynder
19. maj 2005 - 15:54 Der er 2 kommentarer og
1 løsning

Begrænse område i actionScript

Hey.

Nedenstående kode er en del af koden til et puzzle spil.

// see whether there is a tile below
    if (tiley < tileDist*3) {
        if (!tileThere(tilex, tiley+tileDist)) {
            return("below");
        }

I koden skal den søge et et ledigt felt, men hvad skriver man for at begrænse det område hvor den søger?

På forhånd tak.

Chris
Avatar billede pyroman Nybegynder
19. maj 2005 - 23:01 #1
Det kan vi nok ikke gætte os til ud fra de 4 linjer dér... ;)
Avatar billede christoffer535 Nybegynder
19. maj 2005 - 23:37 #2
så får i da bare lidt mere at lege med :)
Jeg tror bare at det skal skrives til det lille stykke med kode.



function initGame () {
    // set the horizontal and vertical distance
    // between tiles
    tileDist = 77;

    // set all tiles in exactly the correct spot
    for (x=1;x<=3;x++) {
        for (y=0;y<=2;y++) {
            tile = x+y*3;
            _root["tile"+tile]._x = x*tileDist;
            _root["tile"+tile]._y = y*tileDist+tileDist;
        }
    }

    // make 10 random but valid moves
    for(tilenum=0;tilenum<10;tilenum++) {
        do {
            // pick a random tile
            tile = "tile"+(random(8)+1);
            // see whether there is an empty space near it
            emptySpace = findEmpty(tile);
        // keep looping until a tile is found that
        // has an empty space near it
        } while ( emptySpace == "none" );

        // move this tile to the empty space
        moveTile(tile,findEmpty(tile));           
    }
}

// given a tile, see if the empty space is near it
function findEmpty (tile) {
    // get location of tile
    tilex = _root[tile]._x;
    tiley = _root[tile]._y;

    // see whether there is a tile to the left
    if (tilex > tileDist) {
        if (!tileThere(tilex-tileDist, tiley)) {
            return("left");
        }
    }

    // see whether there is a tile to the right
    if (tilex < tileDist*3) {
        if (!tileThere(tilex+tileDist, tiley)) {
            return("right");
        }
    }

    // see whether there is a tile above
    if (tiley > tileDist) {
        if (!tileThere(tilex, tiley-tileDist)) {
            return("above");
        }
    }

    // see whether there is a tile below
    if (tiley < tileDist*3) {
        if (!tileThere(tilex, tiley+tileDist)) {
            return("below");
        }
    }

    // tiles are in all directions
    return("none");
}

// check to see whether there is a tile at a certain location
function tileThere (thisx, thisy) {
    // loop through tiles
    for (i=1;i<=8;i++) {
        // see if x matches
        if (_root["tile"+i]._x == thisx) {
            // se if y matches
            if (_root["tile"+i]._y == thisy) {
                return true;
            }
        }
    }

    // no tile there
    return false;
}

// move a tile in a certain direction
function moveTile (tile, direction) {
    if (direction == "above") {
        _root[tile]._y -= tileDist;
    } else if (direction == "below") {
        _root[tile]._y += tileDist;
    } else if (direction == "left") {
        _root[tile]._x -= tileDist;
    } else if (direction == "right") {
        _root[tile]._x += tileDist;
    }
}

// utility function to see on which tile the
// player clicked
function tileUnderMouse () {
    for (i=1; i<=8; i++) {
        if (_root["Tile"+i].hitTest(_xmouse, _ymouse)) {
            return (i);
        }
    }
}

initGame();
stop();
Avatar billede christoffer535 Nybegynder
20. maj 2005 - 10:16 #3
Har fundet ud af det. Ellers tak
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
IT-kurser om Microsoft 365, sikkerhed, personlig vækst, udvikling, digital markedsføring, grafisk design, SAP og forretningsanalyse.

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester