Jump to content

Passing Variables in functions/state


dthrasher90
 Share

Recommended Posts

How can I pass a variable from a function to a state?  I am trying to log the new position of a ball on a field.  So i have a closure that moves the ball around.  The outer function moves the players and the inner function tweens the football to the players, based on a switch statement.  Inside the switch, the function is correctly logging the new location.  How can I pass that new location variable to my main game state?  It looks like this, basically, Im trying to pass that ball.y to make it easily accessable.  Ive tried to make it a global, but that didnt seem to work.   I may add, these functions are in another js file, than my index.html.  Any thoughts?

function play1 (){

//move players

   function move ball to players()

 {

 switch :

//switch stuff here

case 1:

//move ball to player 

console.log(ball.y)

}

 

}

Link to comment
Share on other sites

  <title>MEAN FOOTBALL</title>

    <!-- <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js"></script> -->
    <link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Press+Start+2P" ></script>
    <script src="phaser.js">  </script>
    <script src="offensiveplays.js"></script>
    <script src= "defensiveplays.js"></script>
    <script src="DefenseState.js"> </script>


    <!-- <script src="offensiveplays.js"></script> -->
    <link rel="stylesheet" href="main.css">
  </head>
  <body ng-app>
    <h1>HEAD COACH</h1>



    <div class="roster">
     <h3>On Field</h3>
     QB <br>
     RB <br>
     WR1 <br>
    </div>

  </body>
</html>

<script type="text/javascript">


            var wr1, wr2, wr3, wr4, football, yards, passing;
            var scoreTextHome, scoreTextAway, Time, GameLog, DownLog;
            var lineOfScrimmage;


            var game = new Phaser.Game(1000, 600, Phaser.AUTO);
                game.state.add('GameActionState', GameActionState);
                defense();
                hi();
                hithere();

                

 

Link to comment
Share on other sites

function passPlayLeft() {


    var ct = this.game.add.sprite(100, 220, 'ct');
    ct.scale.setTo(0.03);
    var QBx = ct.x - 20;
    var QBy = ct.y;
    var qb = this.game.add.sprite(QBx, QBy, 'qb');
    qb.scale.setTo(0.02);

    var RGx = ct.x;
    var RGy = ct.y + 20;

    var rg = this.game.add.sprite(RGx, RGy, 'rg');
    rg.scale.setTo(0.03);
    var spriteTextRG = game.add.text(RGx, RGy, 'RG', {
        font: '9px Press Start 2P'
    });


    var RTx = ct.x;
    var RTy = rg.y + 20;
    var rt = this.game.add.sprite(RTx, RTy, 'rt');
    rt.scale.setTo(0.03);
    var spriteTextRT = game.add.text(RTx, RTy, 'RT', {
        font: '9px Press Start 2P'
    });

    var LGx = ct.x;
    var LGy = ct.y - 20;
    var lg = this.game.add.sprite(LGx, LGy, 'lg');
    lg.scale.setTo(0.03);
    var spriteTextLG = game.add.text(LGx, LGy, 'LG', {
        font: '9px Press Start 2P'
    });


    var LTx = ct.x;
    var LTy = lg.y - 20;
    var lt = this.game.add.sprite(LTx, LTy, 'lt');
    lt.scale.setTo(0.03);
    var spriteTextLT = game.add.text(LTx, LTy, 'LT', {
        font: '9px Press Start 2P'
    });


    var WR1x = ct.x;
    var WR1y = ct.y - 150;
    var wr1 = this.game.add.sprite(WR1x, WR1y, 'wr1');
    wr1.scale.setTo(0.02);
    var spriteTextWR1 = game.add.text(WR1x, WR1y, 'WR1', {
        font: '9px Press Start 2P'
    });


    var WR2x = ct.x - 20;
    var WR2y = ct.y - 80;
    var wr2 = this.game.add.sprite(WR2x, WR2y, 'wr2');
    wr2.scale.setTo(0.02);
    var spriteTextWR2 = game.add.text(WR2x, WR2y, 'WR2', {
        font: '9px Press Start 2P'
    });

    var WR3x = ct.x - 20;
    var WR3y = ct.y - 50;
    var wr3 = this.game.add.sprite(WR3x, WR3y, 'wr3');
    wr3.scale.setTo(0.02);
    var spriteTextWR3 = game.add.text(WR3x, WR3y, 'WR3', {
        font: '9px Press Start 2P'
    });






    var football = this.game.add.sprite(ct.x, ct.y, 'football');
          football.scale.setTo(0.08);

    // lineOfScrimmage = football.y;
    // console.log("los = " + lineOfScrimmage);



    var tweenA = game.add.tween(wr1).to({
        x: '+200'
    }, 1700);
    var tweenB = game.add.tween(wr2).to({
        x: '+200'
    }, 1500);
    var tweenC = game.add.tween(wr3).to({
        x: '+200'
    }, 3000);

    var tweenD = game.add.tween(lt).to({
        x: '-35'
    }, 2000);
    var tweenE = game.add.tween(rt).to({
        x: '-35'
    }, 2000)
    var tweenF = game.add.tween(lg).to({
        x: '-20'
    }, 2000);
    var tweenG = game.add.tween(rg).to({
        x: '-20'
    }, 200);


    var tweenH = game.add.tween(qb).to({
        x: '-50'
    }, 1800);





    tweenA.start();
    tweenB.start();
    // tweenC.start();

    tweenD.start();
    tweenE.start();
    tweenF.start();
    tweenG.start();
    tweenH.start();

    Base43Defense(passPlayLeft, ct);
    game.physics.arcade.enable([qb, wr1, wr2, wr3, ct, rg, rt, lg, lt]);
    qbPass();
    function qbPass() {

        ;
        var x = Math.floor((Math.random() * 3) + 1);
        console.log(x);
        switch (x) {

            case 1:

            var tweenFootballA = game.add.tween(football).to({
                x: '+100',
                y: '-150'
            }, 3000);
                tweenFootballA.start();
                tweenFootballA.onComplete.add(los, this);
                function los(){
                  var lineOfScrimmage = football.y;
                  console.log("line of scrimmage = ", lineOfScrimmage);
                }
                console.log('1');
                break;

            case 2:


            var tweenFootballB = game.add.tween(football).to({
                x: '+100',
                y: '-50'
            }, 3000);
                tweenFootballB.start();
                tweenFootballB.onComplete.add(los, this);
                function los(){
                  var lineOfScrimmage = football.y;
                  console.log("line of scrimmage = ", lineOfScrimmage);
                }
                console.log('2');
                break;

            case 3:

            var tweenFootballC = game.add.tween(football).to({
                x: '+100',
                y: '-100'
            }, 3000);
                tweenFootballC.start();
                tweenFootballC.onComplete.add(los, this);
                function los(){

                  var lineOfScrimmage = football.y;
                  console.log("line of scrimmage = ", lineOfScrimmage);
                }
                console.log('3');
                break;

        }
    }

 

Link to comment
Share on other sites

My code is super huge, is there another way to do this?   Basically i am lining everyone up to the center and the center to the football, tracking the distance of the football to move the line of scrimmage (more functions to come for this).  the idea is to use the case to simulate a qb choosing which reciever to throw the ball to.  The code is fine and dandy and it works well, with little lag, maybe there is a better way?  I want to get football.y so I can start keeping global stats. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...