2009-06-22 3 views
0

Ich versuche einen Movieclip zu erstellen, der in anderen Projekten verwendet wird. Es ist eigentlich das 15-Puzzle-Spiel. Ich habe alles, außer das Ausführen des Movieclips, wenn jemand das Spiel gewinnt. Die Überprüfung auf den Gewinn funktioniert, damit ich weiß, wann ich den Clip spielen muss, ich weiß einfach nicht wie. Alles muss unter dem Puzzle-Movieclip bleiben. Ich werde den Code anhängen. Ich verwende den "falschen" Gewinn, um zu testen, also muss ich das Spiel nicht jedes Mal spielen, wenn ich es testen möchte. Ich bin ein 40-jähriger Tierarzt der COBOL-Programmierung, aber sehr neu in Flash. Was ich wissen muss ist: 1) Wo die win_mc 2) Wie man es von der mousedown Handler, wo ich für einen Gewinn Test 3) Kommentare zur Codierung sehr geschätzt. Ich möchte Flash richtig lernen.Ausführen eines Movieclips in einem anderen, der sich in der Hauptzeitleiste befindet

Danke für jede Hilfe. Ray

Action Layer of "Puzzle" 

    Function to initialize a new game. 
// Should include: 
//  tile size 
//  x Offset 
//  y Offset 
//  Reset any counters used to record statistics for any given game. 
function initGame() 
{ 
    _global.numMoves = 0; 
    _global.hours = 0; 
    _global.minutes = 0; 
    _global.seconds = 0; 

    var _loc1 = this; 
    // 
    // Place tiles on the board in the "solved" state. 
    // 
    tileDist = 52; // Tile size 
    xOffset = -16; // Tile x offset, controls where the left side of the tiles start. 
    yOffset = 115; // Tile y offset, controls where the top side of the tiles start. 
    _global.solutionTileName = new Array; 
    _global.solutionTileX = new Array; 
    _global.solutionTileY = new Array; 
    SliderFrame._x = 114; 
    SliderFrame._y = 245; 
    // 
    for (x = 1; x <= 4; x++) 
    { 
     for (y = 0; y <= 3; y++) 
     { 
      tile = x + y * 4; 
      _loc1["tile" + tile]._x = xOffset + (x * tileDist); 
      _loc1["tile" + tile]._y = yOffset + (y * tileDist + tileDist); 
// 
//  Build a solution matrix for the puzzle. 
// 
     _global.solutionTileName[tile] = "Tile" + tile; 
     _global.solutionTileX[tile] = xOffset + (x * tileDist); 
     _global.solutionTileY[tile] = yOffset + (y * tileDist + tileDist); 
     // 
     } // end of for 
    } // end of for 
    //trace(solutionTileName); 
    //trace(solutionTileX); 
    //trace(solutionTileY); 

// 
// Randomize the tiles after placing them on the board. 
// NOTE: According to references, about half of the initial random configurations are unsolvable. 
//   This puzzle is always solvable because the shuffle algorithm starts from the "final" 
//   image and makes a series of random legal moves. 
// 
    for (tilenum = 0; tilenum < 100; tilenum++) 
    { 
     do 
     { 
      tile = "tile" + (random(15) + 1); 
      emptySpace = findEmpty(tile); 
     } while (emptySpace == "none") 
     moveTile(tile, findEmpty(tile)); 
    } // end of for 
      _global.numMoves = 0; 
     this.txt = numMoves; 
} // End of the function 
// 
// 
// Function to find an empty slot adjacent to a given tile. 
//  Returns : 
//   "left", "right", "above", "below", or "none" 
// 
function findEmpty(tile) 
{ 
    tilex = this[tile]._x; 
    tiley = this[tile]._y; 
// trace("findEmpty - Tile=" + tile + "(" + tilex + "," + tiley + ")"); 
// Check for empty slot - LEFT 
    if (tilex > xOffset + tileDist) 
    { 
     if (!tileThere((tilex - tileDist), tiley)) 
     { 
      //trace("tile not there LEFT - (" + (tilex - tileDist) + "," + tiley + ")"); 
      return ("left"); 
     } // end if 
    } // end if 

// Check for empty slot - RIGHT 
    if (tilex < (xOffset + (tileDist * 4))) 
    { 
     if (!tileThere((tilex + tileDist), tiley)) 
     { 
      //trace("tile not there RIGHT - (" + (tilex + tileDist) + "," + tiley + ")"); 
      return ("right"); 
     } // end if 
    } // end if 

// Check for empty slot - ABOVE 
    if (tiley > (yOffset + tileDist)) 
    { 
     if (!tileThere(tilex, (tiley - tileDist))) 
     { 
      //trace("tile not there ABOVE - (" + tilex + "," + (tiley - tileDist) + ")"); 
      return ("above"); 
     } // end if 
    } // end if 

// Check for empty slot - BELOW 
    if (tiley < (yOffset + (tileDist * 4))) 
    { 
     if (!tileThere(tilex, (tiley + tileDist))) 
     { 
      //trace("tile not there BELOW - (" + tilex + "," + (tiley + tileDist) + ")"); 
      return ("below"); 
     } // end if 
    } // end if 
    return ("none"); 
} // End of the function 
// 
// Function to test if there is a tile in a given slot. 
//  Returns : 
//   "true" or "false" 
// 
function tileThere(thisx, thisy) 
{ 
    var _loc1 = this; 
    var _loc2 = thisx; 
    var _loc3 = thisy; 
    for (i = 1; i <= 15; i++) 
    { 
     if (_loc1["tile" + i]._x == _loc2) 
     { 
      if (_loc1["tile" + i]._y == _loc3) 
      { 
       return (true); 
      } // end if 
     } // end if 
    } // end of for 
    return (false); 
} // End of the function 
// 
// Function to move a given tile left, right, up, or down depending on direction passed. 
//  Returns : 
//   nothing 
// 

function moveTile(tile, direction) 
{ 
    var _loc1 = tile; 
    var _loc2 = this; 
    var _loc3 = direction; 
    if (_loc3 == "above") 
    { 
     _loc2[_loc1]._y = _loc2[_loc1]._y - tileDist; 
     _global.numMoves = _global.numMoves + 1; 
     this.txt = numMoves; 
     return; 
    } // end if 
    if (_loc3 == "below") 
    { 
     _loc2[_loc1]._y = _loc2[_loc1]._y + tileDist; 
     _global.numMoves = _global.numMoves + 1; 
     this.txt = numMoves; 
     return; 
    } // end if 
    if (_loc3 == "left") 
    { 
     _loc2[_loc1]._x = _loc2[_loc1]._x - tileDist; 
     _global.numMoves = _global.numMoves + 1; 
     this.txt = numMoves; 
     return; 
    } // end if 
    if (_loc3 == "right") 
    { 
     _loc2[_loc1]._x = _loc2[_loc1]._x + tileDist; 
     _global.numMoves = _global.numMoves + 1; 
     this.txt = numMoves; 
    } // end if 
} // End of the function 
// 
// 
// Function to find which tile is under the mouse when clicked. 
//  Returns : 
//   i an integer indicating Tile1, Tile2,...,Tile15 
// 
function tileUnderMouse() 
{ 
    var _loc1 = this; 
    for (i = 1; i <= 15; i++) 
    { 
     if (_loc1["Tile" + i].hitTest(_xmouse, _ymouse)) 
     { 
      return (i); 
     } // end if 
    } // end of for 
} // End of the function 
function GetElapsedTime() 
{ 
    _global.hours; 
    _global.minutes; 
    _global.seconds; 
    _global.elapsedTime; 
    seconds = seconds + 1; 
    if (seconds == 60) 
     { 
      minutes = minutes + 1; 
      seconds = 0; 
     } 
    if (minutes == 60) 
     { 
      hours = hours + 1; 
      minutes = 0; 
     } 
    tSeconds = seconds; 
    tMinutes = minutes; 
    tHours = hours; 
    if (Seconds < 10) 
     { 
      tSeconds = "0" + tSeconds; 
     } 
    if (Minutes < 10) 
     { 
      tMinutes = "0" + tMinutes; 
     } 
    if (minutes < 1) 
    { 
     tMinutes = "00"; 
    } 
    if (hours < 10) 
     { 
      tHours = "0" + tHours; 
     } 
    if (hours < 1) 
    { 
     tHours = "00"; 
    } 
    elapsedTime = tHours + ":" + tMinutes + ":" + tSeconds; 
} // End of the function 
// 
// Function to test if the puzzle is solved. 
//  Returns : 
//   "true" or "false" 
// 
function isWin() 
{ 
    var win = 1; 
    for (i = 1; i <= 15; i++) 
    { 
     if (("Tile" + i) != solutionTileName[i]) 
     { 
      win = 0; 
     } // end if 
     if (this["Tile" + i]._x != solutionTileX[i]) 
     { 
      win = 0; 
     } // end if 
     if (this["Tile" + i]._y != solutionTileY[i]) 
     { 
      win = 0; 
     } // end if 
    } // end of for 
    if (win == 1) 
    { 
     return(true); 
    } 
     else 
     { 
      return(false); 
     } 
} // End of the function 
// 
// Entry point to movie clip Puzzle_mc 
// 
    _global.solutionTileName = new Array; 
    _global.solutionTileX = new Array; 
    _global.solutionTileY = new Array; 
_global.numMoves = 0; 
_global.hours = 0; 
_global.minutes = 0; 
_global.seconds = 0; 
this.elapsedTime = "00:00:00"; 
var intervalId; 
initGame(); 
intervalId = setInterval(GetElapsedTime,1000); 
stop(); 

Layer 16 of "Puzzle" 

// 
// Action Function to handle the mouse click and move the tile to the appropriate position. 
//  Returns : 
//   nothing 
// 
onClipEvent (mouseDown) 
{ 
    //tileClicked = _root.Puzzle_mc.tileUnderMouse(); 
    //emptySpace = _root.Puzzle_mc.findEmpty("tile" + tileClicked); 
    //_root.Puzzle_mc.moveTile("tile" + tileClicked, emptySpace); 
    tileClicked = _parent.tileUnderMouse(); 
    emptySpace = _parent.findEmpty("tile" + tileClicked); 
    _parent.moveTile("tile" + tileClicked, emptySpace); 

//if (this.isWin()) 
if (_parent.isWin()) 
{ 
    trace(this + "TRUE"); 
} 
else 
{ 
    Win_mc.Play(2); 
    //WinSymbol.gotoAndPlay("WinSymbolL"); 
    //gotoAndPlay("WinSymbolL"); 
    trace(this + "FALSE"); 
} 
} 

Antwort

0

Nun, zunächst von ihm weg, wie dieser Code aussieht, ist Actionscript 2 statt Actionscript 3. Diese völlig in Ordnung ist, die Dinge für das Erhalten begonnen, aber AS2 ist grundsätzlich ein deadend - Adobe wird es nicht mit updaten Neue Eigenschaften.

Wenn Sie nur aus Spaß in Flash einsteigen möchten, ist AS2 in Ordnung. Wenn Sie sich beruflich oder beruflich damit beschäftigen, sollten Sie sich auch AS3 anschauen. Und tatsächlich kann beides heutzutage sehr wertvoll sein, da es eine Mischung von Projekten gibt.

Nun, wie für Ihre Frage!

Im Allgemeinen ist es am besten zu vermeiden, Code irgendwo anders als in der Hauptzeitleiste zu platzieren (noch besser ist die Verwendung externer Klassen). Wenn Code in GUI-Elementen platziert wird, kann der Code aufgrund von Problemen mit dem Umfang schwer zu finden und schwer zu debuggen sein (wie Sie es gefunden haben!).

Der mouseDown-Event-Handler dort ist an ein Kind des Puzzle-MovieClips angehängt, nicht an eine Timeline. Das bedeutet, dass dieser Code im Bereich dieses Clips ausgeführt wird - daher alle Verweise auf _parent. Dies bedeutet, dass Sie einfach den win_mc-Clip zum Puzzle-Clip hinzufügen können (in seiner eigenen Ebene über den Kacheln) und dann mit _parent.Win_mc von Ihrem mouseDown-Handler aus darauf verweisen.

+0

Danke Branden, das ist zum Spaß, machen utils für mein LG wagen Telefon. Ich habe die Win_mc in Puzzle_mc mit Ausnahme der Action-Ebene platziert. Ich habe den Code im mousedown-Handler in _parent.Win_mc.gotoAndPlay (2) geändert; (anim bittet in fr2) Wenn ich in f1 von Win_mc stoppe, funktioniert nichts in Win_mc; wenn ich nicht Win_mc ausgeführt wird sofort ausgeführt, während das Spiel bereit ist, gespielt zu werden. Ich vermisse etwas, aber ich weiß nicht, was es ist. –

+0

Nun, die Stop-Aktion wird entweder dort oder irgendwo anders benötigt, um zu verhindern, dass win_mc vorzeitig abgespielt wird. Ansonsten hört es sich so an, als ob Sie ein relativ einfaches Problem (aber Schmerzen im Hintern zu diagnostizieren/reparieren!) Haben. Wenn Sie möchten, können Sie mir gerne die FLA schicken und ich werde mir die Struktur ansehen. Sie können mir eine E-Mail an bhall auf der Website senden, die ich in meinem Profil aufgeführt habe. –

+0

Es stellte sich heraus, dass es sich um ein IDE-bezogenes Problem handelte und nicht um Code - das _parent-Bit war jedoch korrekt. –

Verwandte Themen