Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

AS3 game high score to HTML input field

New Here ,
Jun 05, 2013 Jun 05, 2013

Hi Guys

This problem is troubling me and my friends as we cannot work out how to implement this.

I have a game which saves your highest score locally. What we need to do is take this high score from the flash file and put it in an html input box (which is seperate frm the flash file but on the same web page).

Please have a look at the below code and let me know the best way to achieve this. Is this possible?!?

Here is the AS3:

////Following code brings in the life to the object. All the physical engine has been coded here.

// Defining all variables

var gravity:Number;

var airFriction:Number;

var floorFriction:Number;

var xVelocity:Number = 0;

var yVelocity:Number = 0;

var bgSrc:String;

var objSrc:String;

var musicSrc:String;

var scoreTint:uint;

var kineticEnergyLoss:Number;

var xKineticEnergyLoss:Number;

var bDifference:Number;

var tDifference:Number;

var lDifference:Number;

var rDifference:Number;

var xDifference:Number;

var yDifference:Number;

var objectHeight:Number;

var objectWidth:Number;

var score:Number = 0;

var bestScore:Number = 0;

var rotationVelocity:Number;

var lWall:Number;

var rWall:Number;

var tWall:Number;

var bWall:Number;

var bestScoreSO:SharedObject = SharedObject.getLocal("bestScore");

function define() {

    // defining object width and height according to dynamic image used as object

    objectHeight = object.height;

    objectWidth = object.width;

   

    // Set walls according to stage dimensions

    lWall = objectWidth/2;

    rWall = stage.stageWidth - objectWidth/2;

    if (tWall == 0) tWall = undefined;

    else tWall = -objectWidth/2;

    bWall = stage.stageHeight - objectHeight/2;

   

    // Restore previous best score

    if (bestScoreSO.data.bestScore != undefined)

    bestScore = bestScoreSO.data.bestScore;

    else bestScore = 0;

}

// Vertical Motion of the body OR the action of gravity on the body

function applyGravity(object:MovieClip) {

    // check if floor is nearer than the current velocity of the object and fix it

    if (-yVelocity > bDifference) {

        object.y = bWall;

        // if object strike ground its velocity become inverse

        yVelocity *= -1;

        // reduce part of velocity when it strikes ground

        if (yVelocity > kineticEnergyLoss) yVelocity -= kineticEnergyLoss;

        else {

            yVelocity = 0;

        }

       

        // Determine score

        if (score != 0) {

            if (score > bestScore) {

                bestScore = score;

                saveBestScore();

            }

            score = 0;

            scoreDisplay.gotoAndPlay(2);

            musicCheckBox.MoveIn();

        }

    }

   

    // check if top wall is nearer than the current velocity of the object and fix it

    if (yVelocity > tDifference) {

        object.y = tWall;

        yVelocity *= -1;

        if (yVelocity > kineticEnergyLoss) yVelocity += kineticEnergyLoss;

    }

   

    // move the object with yVelocity and always decrease yVelocity due to gravitational acceleration

    object.y -= yVelocity;

    yVelocity -= gravity;

}

//Movement of the body along horizontal axis

function applyHorizontalMotion(object:MovieClip) {

    // check if right wall is nearer than the current velocity of the object and fix it

    if (-xVelocity > rDifference) {

        object.x = rWall;

        xVelocity *= -1;

        rotationVelocity *= -1;

        if (xVelocity > xKineticEnergyLoss) xVelocity -= xKineticEnergyLoss;

        else xVelocity = 0;

    }

   

    // check if left wall is nearer than the current velocity of the object and fix it

    if (xVelocity > lDifference) {

        object.x = lWall;

        xVelocity *= -1;

        rotationVelocity *= -1;

        if (-xVelocity > xKineticEnergyLoss) xVelocity += xKineticEnergyLoss;

        else xVelocity = 0;

    }

   

    // move the object with xVelocity and reduce all velocity due to airFriction

    object.x -= xVelocity;

    if (yVelocity != -0.5)    xVelocity -= xVelocity*airFriction;

    else xVelocity -= xVelocity*floorFriction;   

}

// Increase or Decrease the rotation of the body according to mouseclick

function applyRotation(object:MovieClip) {

    if (!isNaN(rotationVelocity)) object.rotation += rotationVelocity;

    if (yVelocity != -0.5)  rotationVelocity -= rotationVelocity*airFriction;

    else rotationVelocity -= rotationVelocity*floorFriction;

}

// Distance of object from walls

function Walls(object:MovieClip) {

    bDifference = bWall - object.y;

    tDifference = object.y - tWall;

    lDifference = object.x - lWall;

    rDifference = rWall - object.x;

}

// Apply all properties to the object including Gravity, Rotation, Horizontal Motion, and Reaction force from the walls

function applyPhysics(ev:Event) {

    applyGravity(MovieClip(ev.target));

    applyHorizontalMotion(MovieClip(ev.target));

    applyRotation(MovieClip(ev.target));

    Walls(MovieClip(ev.target));

    updateScore();

}

// Update current score and best score

function updateScore() {

    scoreDisplay.score.text = score;

    scoreDisplay.bestScore.text = "Best Score : " + bestScore;

}

// Save best score into local memory via ShareObjects

function saveBestScore() {

    bestScoreSO.data.bestScore = bestScore;

    bestScoreSO.flush();

}

// Checks where the mouse has been click with reference to the centre of the object

function checkClick(ev:Event) {

    // distance of mouseClick from the centre of the object

    yDifference = (mouseY - object.y);

    if (yDifference < 20 && yDifference > 0) yDifference += 40;

    if (yDifference > -20 && yDifference < 0) yDifference -= 40;

    if (yDifference < 0) {

        yDifference *= -1;

    }

   

    // set the velocity of the object according to distance between mouseClick and object centre

    yVelocity = yDifference/4;

   

    xDifference = (object.x - mouseX);

   

    xVelocity = -xDifference/4;

   

    rotationVelocity = xDifference/4;

   

    // Increase score with each mouseclick and move the music Check box away

    score++;

    scoreDisplay.gotoAndPlay(2);

    if (score == 1) musicCheckBox.MoveAway();

}

TOPICS
ActionScript
643
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Jun 06, 2013 Jun 06, 2013

You can use the ExternalInterface class to have a Flash file communicate with the html page that embeds it via javascript in that html page.  If you have no familarity with it, search Google using terms like "AS3 ExternalInterface tutorial" - it will yield several helpful results.

Translate
LEGEND ,
Jun 06, 2013 Jun 06, 2013
LATEST

You can use the ExternalInterface class to have a Flash file communicate with the html page that embeds it via javascript in that html page.  If you have no familarity with it, search Google using terms like "AS3 ExternalInterface tutorial" - it will yield several helpful results.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines