使用as3通过LocalConnection将分数变量传递给另一个swf [英] Passing score variable to another swf with LocalConnection using as3

查看:126
本文介绍了使用as3通过LocalConnection将分数变量传递给另一个swf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三场比赛 - swf1,swf2和swf3。我需要将swf1的分数变量传递给swf2,以便合并这些分数。然后,我需要通过swf1& swf2新组合了swf3的分数,这样我就可以将所有三场比赛的总分加起来。我正在使用一个Loader类来连接swfs,我尝试使用LocalConnection来传递分数变量而没有运气。

Swf1 :(发送swf )

 
{
import flash.display。*;
导入flash.events。*;
import flash.net.URLLoader;
import flash.net.navigateToURL;
import flash.net.URLRequest;
导入flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
导入flash.net.LocalConnection; //导入/导出分数**************************************** *****

public class Map扩展MovieClip
{
var errorCount = 0;
var dragdrops:Array;
var numOfMatches:uint = 0;
var speed:Number = 25;
var eventSound:event_sound = new event_sound();
var winSound:赢家=新赢家();
//从swf发送得分到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)
}
}

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


函数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);



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

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

//发送swf1得分到下一个swf2游戏。 ************************************************** *************
send_score = new LocalConnection();
send_score.send('myConnection','methodtoexecute',errorCount_Game1);

win.y - =速度;
if(win.y <= 0)
{
win.y = 0; //将赢屏幕移到顶部位置
win.removeEventListener(Event.ENTER_FRAME,winGame);
win.addEventListener(MouseEvent.MOUSE_UP,nextGame);
winSound.play();



$ b函数nextGame(event:MouseEvent):void
{
var myLoader:Loader = new Loader();
var url:URLRequest = new URLRequest(Game2.swf);
myLoader.load(url);
addChild(myLoader);




$ b

Swf2 :(接收并发送swf)

 
{
import flash.display。* ;
导入flash.events。*;
import flash.net.URLLoader;
import flash.net.navigateToURL;
import flash.net.URLRequest;
导入flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
导入flash.net.LocalConnection; //导入/导出分数

公共类Map2扩展MovieClip
{
var errorCount = 0;
var dragdrops:Array;
var numOfMatches:uint = 0;
var speed:Number = 25;
var eventSound:event_sound = new event_sound();
var winSound:赢家=新赢家();

//将swf1的分数加载到这个swf2游戏中******************************** *****************
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);



函数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)
}
}

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


函数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);



函数winGame(事件:事件):void
{
//将swf 1的分数加载到这个swf2游戏中。 ********************************************************** b $ b 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();
}

//将这个组合的swf2分数发送到下一个swf3游戏。 ************************************************** *************
send_score = new LocalConnection();
send_score.send('myConnection','methodtoexecute',errorCount_Game2);


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




$ b

Swf3 :(收到瑞士法郎)

 
{
import flash.display。*;
导入flash.events。*;
import flash.net.URLLoader;
import flash.net.navigateToURL;
import flash.net.URLRequest;
导入flash.net.URLRequestMethod;
import flash.net.URLLoaderDataFormat;
import flash.net.URLVariables;
导入flash.net.LocalConnection; //导入/导出分数

公共类Map3扩展MovieClip
{
var errorCount = 0;
var dragdrops:Array;
var numOfMatches:uint = 0;
var speed:Number = 25;
var eventSound:event_sound = new event_sound();
var winSound:赢家=新赢家();
var miss_drop:uint = 0;

//将swf2的组合分数加载到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);



$ b 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)
}
}

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


函数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);



函数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;



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





增量得分为所有的swf

 
{
import flash.display。*;
导入flash.events。*;

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

公共函数DragDrop()
{
origX = x;
origY = y;
addEventListener(MouseEvent.MOUSE_DOWN,drag);
buttonMode = true;


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


函数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();
//运行匹配方法(在Map类文件中)
Object(parent).match();

isit = true;
}
else
{
var list = Object(parent).dragdrops;
Object(parent).errorCount ++; (var i = 0; i< list.length; i ++)
{
var o = MovieClip(root).getChildByName(list [i] .name + '_目标');
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:。水平=错误,代码=连续几次



感谢您提供的任何帮助!

解决方案

要在两个swf之间传递信息,您可以使用 LocalConnection LoaderContext 对象。

使用LocalConnection对象


$ b

game_01.swf:

  var game01_score:int = 1000; 

var connection:LocalConnection = new LocalConnection();

var loader:Loader = new Loader();
//你必须等到swf完成加载
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,_onLoad);
函数_onLoad(e:Event):void {
//将game01_score发送到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从game_01.swf接收game01_score
var game01_connection:LocalConnection = new LocalConnection();
game01_connection.client = this;
game01_connection.connect('Game01_To_Game02');
函数get_Game01_Score(game01_score:String):void {
trace(game01_score); //给出:1000
game02_score + = int(game01_score);
trace(game02_score); // give:1800
//在接收到game01_score $ b $载入game_03.swf loader.load(new URLRequest('game_03.swf'));
}

// LocalConnection将game01_score + game02_score发送到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 {
//发送game02_score(game01_score + game02_score)到game_03.swf
game03_connection.send('Game02_To_Game03','get_Game02_Score',game02_score);
}
addChild(loader);

game_03.swf:

  // LocalConnection从game_02.swf接收game02_score 
var game02_connection:LocalConnection = new LocalConnection();
game02_connection.client = this;
game02_connection.connect('Game02_To_Game03');
函数get_Game02_Score(game02_score:String):void {
trace(game02_score); //给出:1800

code $ p



使用LoaderContext对象
$ b game_01.swf

  var game01_score:int = 1000; 

var loader_context:LoaderContext = new LoaderContext();
//通过LoaderContext传递game01_score
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; 
//从game_01.swf获取game01_score
game02_score + = int(this.loaderInfo.parameters.game01_score)|| 0;

trace(game02_score); //给出: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'); //给出:1800 

希望可以帮到忙。


I have three games - swf1, swf2 and swf3. I need to pass the score variable from swf1 into swf2 in order to combine those scores. Then, I need to pass the swf1 & swf2 newly combined scores into swf3 so that I can total scores from all three games combined. I am using a Loader class to link the swfs together and I am attempting to use LocalConnection to pass the score variables with no luck.

Swf1: (sending 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: (receiving & sending 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: (receiving 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);                                     
    }
}
}

Class that increments score for all 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;
    }
}
}

I am hoping someone can recognize the error that I am making with the code so that I can properly pass the score variable into each respective swf as the games progress along. Currently swf1 plays through perfect then once swf2 loads I receive the error message "Error #2044: Unhandled StatusEvent:. level=error, code=" several times in a row.

Thank you for any help you might be able to provide with this!

解决方案

To pass an information between two swfs, you can use a LocalConnection or a LoaderContext object.

Using LocalConnection object :

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
    }


Using LoaderContext object :

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

Hope that can help.

这篇关于使用as3通过LocalConnection将分数变量传递给另一个swf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆