views:

76

answers:

1

Hi,

I have a simple standalone application written in Visual Basic that I'm porting to a browser based application using PHP/javascript.

The original VB application has some simple embedded flash games with token and point counters. The token and point values are being passed as variables between the application and the game.

I'm trying to achieve the same effect in my PHP port without modifying the actionscript code( using the variables in actionscript that already exist).

Below is Visual Basic code that's loading a value from a database and posting that value to flash using FlashVars:

Private Sub loadPlayer()

    Try

        If CtblPoints.CheckPointsByID(mCard) Then

            objPoints = CtblPoints.GettblPointsByID(mCard)
            objPlayerAc = CtblPlayerAccount.GettblPlayerAccountByPlayerID(objPoints.AccountId)
            objPlayer = CtblPlayer.GettblPlayerByID(objPlayerAc.PlayerID)
            objPlayerBal = CtblPlayerBalance.GettblPlayerBalanceByID(objPlayerAc.PlayerID)


            objPlayerAcDetail = CtblPlayerAccountDetail.GettblPlayerAccountDetailByAmount(objPoints.AccountId)
            strTotalPoints = Convert.ToString(objPlayerAc.Points)
            strTotalWin = Convert.ToString(objPlayerBal.TokenAmount)

            'Dim intTokenAmount As Decimal = Convert.ToDecimal(objPlayerBal.TokenAmount)
            'strTotalWin = Convert.ToString(Convert.ToInt64(intTokenAmount * 100))

            flashPlayer.Size = panelGame.Size
            flashPlayer.FlashVars = "totalEntries=" & strTotalPoints & "&credit=" & strTotalWin
            flashPlayer.LoadMovie(0, strGameFile)
            flashPlayer.Play()

        Else
            Me.Close()
            Dim frmInvCrd As New frmInvalidCard
            frmInvCrd.ShowDialog()
        End If


    Catch ex As Exception

    End Try

I'm trying to recreate this in PHP, but I'm at a loss as to how to begin implementing it.

The variables in flash are declared publicly, and global imports used:

import com.atticmedia.console.*;
import flash.display.*;
import flash.events.*;
import flash.geom.*;
import flash.media.*;
import flash.net.*;
import flash.system.*;
import flash.utils.*;

First declaration of variable 'totalEntries' is:

public var totalEntries:int = 0;

and this is a snip of totalEntries being used in the actionscript

    public function notifyServerOfUnwonCredits(param1)
    {
        var remainder:* = param1;
        if (this.useServer)
        {
            this.targetWinAmount = 0;
            this.cancelUpdateOverTime = F9.setEnterFrame(this.updateOverTime);
            fscommand("OverTime", "" + remainder);
            this.flashVarsUpdatedAction = function ()
        {
            originalTotalWin = totalWin;
            return;
        }// end function
        ;
        }
        else
        {
            this.setTotalEntries(100000);
            this.setTotalWin(0);
        }
        return;
    }// end function

Eventually I'll be passing these values back to a mySQL database.

Any insight into this would be extremely helpful, Thanks!

A: 

PHP Doesn't work like that; it's not going to be sitting around, in a persistent state that you can always contact and talk to. You'll need a mechanism to send and receive data to the stateless backend. Now, luckily, flash works perfectly well with cookies, so using PHP sessions doesn't produce an issue. So, the question is whether you want to pass the data to PHP via Flash, or pass the variables to Javascript via flash and then send them to PHP.

Direct-via-flash route (bear with me, this is AS 2.0, but should work):

Actionscript:

function getData (path:String, req:Object, fn:Function) {
   var q:Object = new LoadVars();
   for (var prop in req) {
       q[prop] = req[prop];
   }
   q.action = path;
   q.onLoad = fn;
   q.sendAndLoad("/hooks/integrate.php",q,"POST");
}

in this example, I pass to a single file and specify a POST parameter, "action", that tells the handling file what it is I want to do.

<?php
if (!isset($_POST['action'])) { die; }
switch($_POST['action']) {
    case 'something':
// snip...
}

And then calling the function is a snap:

getData('saveVar',{varname: 'foo', varvalue: foo},mySuccessFunction);

Alternatively, you could set something up using XHR/AJAX and pass the variables to Javascript by using the flash.external.ExternalInterface.call() method, but setting up such a solution is beyond the scope of this answer.

Long story short, there is no quick and easy way to do it like you did in VB due to PHP's nature as a stateless request-based web service; you will have to employ another mechanism.

Dereleased
Thanks for the response, this clears some things up for me.On more question though, if you don't mind.If I used javascript as the middleman to pass a variable to PHP, would I still need to make changes the existing actionscript? I understand you're point about PHP being a stateless request-based web service, but is javascript the same way?I guess this question kind of resolves to: Is there anything can I do to avoid modifying the existing actionscript codebase?
Vorathe
It might be possible, since the javascript will remain active and persistent as long as the page is not refreshed, thought if you're passing the variables to PHP it shouldn't matter. I'm assuming that your flash app automatically makes whatever variables you want available as properties of itself, so you could perhaps poll for them, but to be honest I typically use `flash.external.ExternalInterface.call` for communicating with JavaScript, so I am not entirely sure.
Dereleased