使用as3将得分变量传递给另一个使用LocalConnection的swf

时间:2015-03-30 19:39:15

标签: actionscript-3 flash

我有三场比赛 - swf1,swf2和swf3。我需要将得分变量从swf1传递到swf2,以便将这些得分组合起来。然后,我需要通过swf1& swf2新合并得分为swf3,这样我就可以从三场比赛中得到总分。我正在使用Loader类将swfs链接在一起,我试图使用LocalConnection传递得分变量而没有运气。

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);                                     
    }
}
}

为所有swfs增加得分的类

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 =”。

感谢您提供的任何帮助!

1 个答案:

答案 0 :(得分:0)

要在两个swfs之间传递信息,您可以使用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:

trace(this.loaderInfo.parameters.game02_score || '0');  // gives : 1800

希望可以提供帮助。