2015-03-30 37 views
1

我有三個遊戲 - swf1,swf2和swf3。我需要將swf1的分數變量傳遞給swf2,以便合併這些分數。然後,我需要將SWF2新組合分數傳遞給swf3,以便我可以將所有三場比賽的總分加起來。我正在使用一個Loader類將swfs鏈接在一起,我嘗試使用LocalConnection傳遞分數變量而沒有運氣。使用as3將分數變量傳遞給使用LocalConnection的另一個swf

SWF1:(發送SWF)

package 
{ 
import flash.display.*; 
import flash.events.*; 
import flash.net.URLLoader; 
import flash.net.navigateToURL; 
import flash.net.URLRequest; 
import flash.net.URLRequestMethod; 
import flash.net.URLLoaderDataFormat; 
import flash.net.URLVariables; 
import flash.net.LocalConnection; // import/export score ************************************************* 

public class Map extends MovieClip 
{ 
    var errorCount = 0; 
    var dragdrops: Array; 
    var numOfMatches: uint = 0; 
    var speed: Number = 25; 
    var eventSound: event_sound = new event_sound(); 
    var winSound: winner = new winner(); 
    // Send score from this swf into swf 2. ************************************************* 
    var send_score:LocalConnection; 

    public function Map() 
    { 
     dragdrops = [i_double, i_triple, i_increase, i_reduce, i_diamonds, i_skus, i_platinums, i_abos, i_50, i_2020, i_2025, i_10000]; 
     var currentObject: DragDrop; 
     for (var i: uint = 0; i < dragdrops.length; i++) 
     { 
      currentObject = dragdrops[i]; 
      currentObject.target = getChildByName(currentObject.name + "_target"); 
     } 
     start.addEventListener(Event.ENTER_FRAME, startGame); 
    } 

    function startGame(event: Event): void 
    { 
     start.y -= speed; 
     if (start.y <= 0) 
     { 
      start.y = 0; 
      start.removeEventListener(Event.ENTER_FRAME, startGame); 
      start.addEventListener(MouseEvent.CLICK, clickStart) 
     } 
    } 

    function clickStart(event: MouseEvent): void 
    { 
     start.removeEventListener(MouseEvent.CLICK, clickStart) 
     start.addEventListener(Event.ENTER_FRAME, animateUp); 
     eventSound.play(); 
     addChild(start); 
    } 

    function animateUp(event: Event): void 
    { 
     start.y -= speed; 
     if (start.y >= stage.stageHeight) 
     { 
      start.y = stage.stageHeight; 
      start.removeEventListener(Event.ENTER_FRAME, animateUp); 
     } 
    } 

    public function match(): void 
    { 
     numOfMatches++; 
     if (numOfMatches == 3) 
     { 
      win.addEventListener(Event.ENTER_FRAME, winGame); 

     } 
    } 

    function winGame(event: Event): void 
    { 
     var errorCount_Game1 = errorCount; 

     if(win.playerErrorText.text != String(errorCount_Game1)) { 
      win.playerErrorText.text = String(errorCount_Game1); 
     } 

     // Send swf1 score to next swf2 game. *************************************************************** 
     send_score = new LocalConnection(); 
     send_score.send('myConnection', 'methodtoexecute', errorCount_Game1); 

     win.y -= speed; 
     if (win.y <= 0) 
     { 
      win.y = 0; // move win screen to top position. 
      win.removeEventListener(Event.ENTER_FRAME, winGame); 
      win.addEventListener(MouseEvent.MOUSE_UP, nextGame); 
      winSound.play(); 
     } 
    } 


    function nextGame(event: MouseEvent): void 
    { 
     var myLoader:Loader = new Loader();      
     var url:URLRequest = new URLRequest("Game2.swf"); 
     myLoader.load(url);          
     addChild(myLoader);          
    } 
} 
} 

Swf2:(接收&發送SWF)

package 
{ 
import flash.display.*; 
import flash.events.*; 
import flash.net.URLLoader; 
import flash.net.navigateToURL; 
import flash.net.URLRequest; 
import flash.net.URLRequestMethod; 
import flash.net.URLLoaderDataFormat; 
import flash.net.URLVariables; 
import flash.net.LocalConnection; // to import/export score 

public class Map2 extends MovieClip 
{ 
    var errorCount = 0; 
    var dragdrops: Array; 
    var numOfMatches: uint = 0; 
    var speed: Number = 25; 
    var eventSound: event_sound = new event_sound(); 
    var winSound: winner = new winner(); 

    // Load score from swf1 into this swf2 game ************************************************* 
    var get_score:LocalConnection; 

    public function Map2() 
    { 
     dragdrops = [i_energize, i_experience, i_growth, i_increase, i_renewabo, i_partner, i_share, i_winat, i_winwith]; 
     var currentObject: DragDrop; 
     for (var i: uint = 0; i < dragdrops.length; i++) 
     { 
      currentObject = dragdrops[i]; 
      currentObject.target = getChildByName(currentObject.name + "_target"); 
     } 
    } 

    function startGame(event: Event): void 
    { 
     start.y -= speed; 
     if (start.y <= 0) 
     { 
      start.y = 0; 
      start.removeEventListener(Event.ENTER_FRAME, startGame); 
      start.addEventListener(MouseEvent.CLICK, clickStart) 
     } 
    } 

    function clickStart(event: MouseEvent): void 
    { 
     start.removeEventListener(MouseEvent.CLICK, clickStart) 
     start.addEventListener(Event.ENTER_FRAME, animateUp); 
     eventSound.play(); 
     addChild(start); 
    } 

    function animateUp(event: Event): void 
    { 
     start.y -= speed; 
     if (start.y >= stage.stageHeight) 
     { 
      start.y = stage.stageHeight; 
      start.removeEventListener(Event.ENTER_FRAME, animateUp); 
     } 
    } 

    public function match(): void 
    { 
     numOfMatches++; 
     if (numOfMatches == 3) 
     { 
      win.addEventListener(Event.ENTER_FRAME, winGame); 
     } 
    } 

    function winGame(event: Event): void 
    { 
     // Load score from swf 1 into this swf2 game. ************************************************* 
     get_score = new LocalConnection(); 
     get_score.methodtoexecute = function(errorCount_Game1) 
     { 
      errorCount_Game2 = errorCount + errorCount_Game1; 
     } 
     get_score.connect('myConnection'); 

     if(win.playerErrorText.text != String(errorCount_Game2)) { 
      win.playerErrorText.text = String(errorCount_Game2); 
     } 

     win.y -= speed; 
     if (win.y <= 0) 
     { 
      win.y = 0; 
      win.removeEventListener(Event.ENTER_FRAME, winGame); 
      win.addEventListener(MouseEvent.MOUSE_UP, nextGame); 
      winSound.play(); 
     } 

     // Send this combined swf2 score to next swf3 game. *************************************************************** 
     send_score = new LocalConnection(); 
     send_score.send('myConnection', 'methodtoexecute', errorCount_Game2); 
    } 

    function nextGame(event: MouseEvent): void 
    { 
     var myLoader:Loader = new Loader();      
     var url:URLRequest = new URLRequest("Game3.swf"); 
     myLoader.load(url);          
     addChild(myLoader);          
    } 
} 
} 

Swf3:(接收SWF)

package 
{ 
import flash.display.*; 
import flash.events.*; 
import flash.net.URLLoader; 
import flash.net.navigateToURL; 
import flash.net.URLRequest; 
import flash.net.URLRequestMethod; 
import flash.net.URLLoaderDataFormat; 
import flash.net.URLVariables; 
import flash.net.LocalConnection; // import/export score 

public class Map3 extends MovieClip 
{ 
    var errorCount = 0; 
    var dragdrops: Array; 
    var numOfMatches: uint = 0; 
    var speed: Number = 25; 
    var eventSound:event_sound = new event_sound(); 
    var winSound:winner = new winner(); 
    var miss_drop: uint = 0; 

    // Load combined scores from swf2 into this swf3 ************************************************* 
    var get_final_score:LocalConnection; 

    public function Map3() 
    { 
     dragdrops = [i_positive, i_integrated, i_rewards, i_leaderdev, i_leaderalign, i_focused, i_fast, i_fun]; 
     var currentObject: DragDrop; 
     for (var i: uint = 0; i < dragdrops.length; i++) 
     { 
      currentObject = dragdrops[i]; 
      currentObject.target = getChildByName(currentObject.name + "_target"); 
     } 
    } 


    function startGame(event: Event): void 
    { 
     start.y -= speed; 
     if (start.y <= 0) 
     { 
      start.y = 0; 
      start.removeEventListener(Event.ENTER_FRAME, startGame); 
      start.addEventListener(MouseEvent.CLICK, clickStart) 
     } 
    } 

    function clickStart(event: MouseEvent): void 
    { 
     start.removeEventListener(MouseEvent.CLICK, clickStart) 
     start.addEventListener(Event.ENTER_FRAME, animateUp); 
     eventSound.play(); 
     addChild(start); 
    } 

    function animateUp(event: Event): void 
    { 
     start.y -= speed; 
     if (start.y >= stage.stageHeight) 
     { 
      start.y = stage.stageHeight; 
      start.removeEventListener(Event.ENTER_FRAME, animateUp); 
     } 
    } 

    public function match():void 
    { 
     numOfMatches++; 
     if(numOfMatches == dragdrops.length) 
     { 
      win.addEventListener(Event.ENTER_FRAME, winGame); 
     } 
    } 

    function winGame(event: Event): void 
    { 
     get_final_score = new LocalConnection(); 
     get_final_score.methodtoexecute = function(errorCount_Game2) 
     { 
      errorCount_Game3 = errorCount + errorCount_Game2; 
     } 
     get_score.connect('myConnection'); 

     if(win.playerErrorText.text != String(errorCount_Game3)) { 
      win.playerErrorText.text = String(errorCount_Game3); 
     } 

     win.y -= speed; 
     if (win.y <= 0) 
     { 
      win.y = 0; 
      win.removeEventListener(Event.ENTER_FRAME, winGame); 
      //win.addEventListener(MouseEvent.CLICK, clickWin) 
      win.replay_button.addEventListener(MouseEvent.MOUSE_UP, nextGame); 
      winSound.play(); 
      missDrop(); 
     } 
    } 

    public function missDrop():void 
    { 
     if (errorCount_Game3 == 0 || errorCount_Game3 == 1) 
     { 
      win.fourstars.alpha = 1;   
     } 
     else if (errorCount_Game3 == 2 || errorCount_Game3 == 3) 
     { 
      win.threestars.alpha = 1; 
     } 
     else if (errorCount_Game3 == 4 || errorCount_Game3 == 5) 
     { 
      win.twostars.alpha = 1; 
     } 
     else if (errorCount_Game3 > 5) 
     { 
      win.onestar.alpha = 1; 
     } 
    } 

    function nextGame(event: MouseEvent): void 
    { 
     var myLoader:Loader = new Loader();      
     var url:URLRequest = new URLRequest("Game1.swf"); 
     myLoader.load(url);          
     addChild(myLoader);          
    } 
} 
} 

類,爲所有主權財富基金

package 
{ 
import flash.display.*; 
import flash.events.*; 

public class DragDrop extends Sprite 
{ 
    var origX: Number; 
    var origY: Number; 
    var target: DisplayObject; 
    var successSound:sound_correct = new sound_correct(); 

    public function DragDrop() 
    { 
     origX = x; 
     origY = y; 
     addEventListener(MouseEvent.MOUSE_DOWN, drag); 
     buttonMode = true; 
    } 

    function drag(evt: MouseEvent): void 
    { 
     stage.addEventListener(MouseEvent.MOUSE_UP, drop); 
     startDrag(); 
    } 

    function drop(evt: MouseEvent): void 
    { 
     var n = (evt.target.name).split('i_').join(''); 
     var s = 'null'; 
     var isit = false; 
     stage.removeEventListener(MouseEvent.MOUSE_UP, drop); 
     stopDrag(); 

     if (hitTestObject(target)) 
     { 
      visible = false; 
      target.alpha = 1; 
      successSound.play(); 
      //run match method (within the Map Class file) 
      Object(parent).match(); 

      isit = true; 
     } 
     else 
     { 
      var list = Object(parent).dragdrops; 
      Object(parent).errorCount++; // Increments scoring 
      for (var i = 0; i < list.length; i++) 
      { 
       var o = MovieClip(root).getChildByName(list[i].name + '_target'); 
       if (o.hitTestObject(evt.target)) 
       { 
        var temp = o.name.split('i_').join(''); 
        temp = temp.split('_target').join(''); 
        s = temp 
       } 
      } 
     } 
     x = origX; 
     y = origY; 
    } 
} 
} 

我希望有人能認識到我對代碼作出錯誤,這樣我可以得分變量正確傳遞到每個相應的SWF的遊戲進度遞增得分沿。目前swf1通過完美播放,然後一旦swf2加載,我會連續幾次收到錯誤消息「錯誤#2044:未處理的StatusEvent :. level = error,code =」。

非常感謝您提供的任何幫助!

回答

0

要在兩個swf之間傳遞信息,可以使用LocalConnectionLoaderContext對象。

使用LocalConnection對象:

game_01.swf:

var game01_score:int = 1000; 

var connection:LocalConnection = new LocalConnection(); 

var loader:Loader = new Loader(); 
    // you have to wait until the swf is completly loaded 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoad); 
    function _onLoad(e:Event): void { 
     // send game01_score to game_02.swf 
     connection.send('Game01_To_Game02', 'get_Game01_Score', game01_score); 
    } 
    loader.load(new URLRequest('game_02.swf')); 
addChild(loader); 

game_02.swf:

var game02_score:int = 800; 

// LocalConnection to receive game01_score from game_01.swf 
var game01_connection:LocalConnection = new LocalConnection(); 
    game01_connection.client = this; 
    game01_connection.connect('Game01_To_Game02'); 
    function get_Game01_Score(game01_score:String):void { 
     trace(game01_score); // gives : 1000 
     game02_score += int(game01_score); 
     trace(game02_score); // gives : 1800 
     // load the game_03.swf after receiving game01_score 
     loader.load(new URLRequest('game_03.swf')); 
    } 

// LocalConnection to send game01_score + game02_score to game_03.swf 
var game03_connection:LocalConnection = new LocalConnection(); 

var loader:Loader = new Loader(); 
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, _onLoad); 
    function _onLoad(e:Event): void { 
     // send game02_score (game01_score + game02_score) to game_03.swf 
     game03_connection.send('Game02_To_Game03', 'get_Game02_Score', game02_score); 
    } 
addChild(loader); 

game_03.swf:

// LocalConnection to receive game02_score from game_02.swf 
var game02_connection:LocalConnection = new LocalConnection(); 
    game02_connection.client = this; 
    game02_connection.connect('Game02_To_Game03'); 
    function get_Game02_Score(game02_score:String):void { 
     trace(game02_score); // gives : 1800 
    } 

使用LoaderContext對象:

game_01.swf:

var game01_score:int = 1000; 

var loader_context:LoaderContext = new LoaderContext(); 
    // pass game01_score via the LoaderContext 
    loader_context.parameters = { 'game01_score' : game01_score.toString() }; 

var loader:Loader = new Loader(); 
    loader.load(new URLRequest('game_02.swf'), loader_context); 
    addChild(loader); 

game_02.swf:

var game02_score:int = 800; 
    // get game01_score from game_01.swf 
    game02_score += int(this.loaderInfo.parameters.game01_score) || 0; 

trace(game02_score); // gives : 1800 

var loader_context:LoaderContext = new LoaderContext(); 
    loader_context.parameters = { 'game02_score' : game02_score.toString() }; 

var loader:Loader = new Loader(); 
    loader.load(new URLRequest('game_03.swf'), loader_context); 
    addChild(loader); 

game_03。swf:

​​

希望能有所幫助。

+0

太棒了!我遵循你的指導,使用LoaderContext對象。我導入flash.system.LoaderContext;並放置代碼,它的工作!非常感謝您的幫助!我現在明白了爲什麼我以前很困惑。再次感謝! – HyperionMirage 2015-04-01 15:22:34

相關問題