Results 1 to 7 of 7

Thread: Having Trouble Using Variable in More than One Conditional

  1. #1

    Having Trouble Using Variable in More than One Conditional

    It's a little more complex than that, actually.

    Inside a function, which is called by a Timer Event, I have many conditional statements based on CurrentCount of my Timer. Nested within these conditionals are more conditionals.

    <<<I know it's amateur programming, and I'm sure there are much more elegant ways, but I'm just learning (via attempting a difficult challenge).>>>

    One way I'm trying to control all these conditionals is via a random number, from 1 to 3.
    My problem is that after generating the number within one "if (currentCount == )" statement, the variable is no longer available to be used by the next "if (currentCount == )" statement.

    Is this some terrible side effect of using the Timer class??


    Code:
        var timerMain:Timer = new Timer(900, 25)
        timerMain.addEventListener(TimerEvent.TIMER, takeTurn);
        timerMain.start();
            
        function takeTurn(e:TimerEvent):int {
            if (timerMain.currentCount == 4) {
                initMove()
            }
            if (timerMain.currentCount == 5) {
                addTile()
            }
            if (timerMain.currentCount == 6) {
                var turn = Math.floor(Math.random()*3 + 1);
                if (turn == 1) {
                    revealTile();
                    initMove();
                } else {
                    smartMove();
                }
    
            }
            if (timerMain.currentCount == 7) {           ////testing here, but like I said,
                addTile()                                         //the variable won't carry over.
                if (turn == 1) {
                    trace ("w00t")
                }
                if (turn == 2) {
                    trace ("woot! w00t!");
                }
                if (turn == 3) {
                    trace ("whoomp!")
                }
            }

  2. #2
    TheCanadian's Avatar
    10,251
    posts
    Noo doot aboot it, eh?
    No it's a side effect of using a local variable which is deleted after the function has finished executing. Do you want a new random number between calls or do you want the same one generated when currentCount equals 6?
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  3. #3
    I want to retain the variable over a few currentCount statements.

    I've learned that variables can't "leave" functions without a Return, but I didn't think it would be a problem here. Is it because each currentCount conditional is like an iteration of the overall function?
    Could I use the variable as an initial parameter and then return it??

  4. #4
    TheCanadian's Avatar
    10,251
    posts
    Noo doot aboot it, eh?
    No the problem is caused by what I already said it was caused by Declare it outside the function.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  5. #5
    Thanks, Canadian -- you've been a great help throughout.

    When I asked
    Could I use the variable as an initial parameter and then return it??
    what I meant was should I
    Declare it outside the function
    ?

    I see now though that I can declare it outside without having to use it as a parameter.

    However, while considering how to circumvent what seemed to me an insurmountable problem, I came up with a more elegant solution. Why not just do all the randomizing PRIOR to the function and store it as an Array?

    I've taken it upon myself to make an Intro animation for the game I'm creating, which has proven to be an incredible challenge but a great learning lesson.
    The intro features three random players -- lines drawing across a grid.
    Each player gets from 1 to 3 moves per round. I want to animate 11 moves.
    But rather than doing all the randomizing on the fly as the timer's "ticking," I had the sudden inspiration to randomize the turns beforehand and use the resulting Array as my control.
    Like this:

    Code:
    	var turnSeq:Array = [];
    	var r1Turn:String = "r1";
    	var r2Turn:String = "r2";
    	var r3Turn:String = "r3";
    	var turn:int = new int;
    	while (turnSeq.length < 11) {
    		turn = Math.floor(Math.random()*3 + 1);
    		while (turn > 0) {
    			turnSeq.push (r1Turn);
    			turn --;
    		}
    		turn = Math.floor(Math.random()*3 + 1);
    		while (turn > 0) {
    			turnSeq.push (r2Turn);
    			turn --;
    		}
    		turn = Math.floor(Math.random()*3 + 1);
    		while (turn > 0) {
    			turnSeq.push (r3Turn);
    			turn --;
    		}
    		if (turnSeq.length > 11) {
    			turnSeq.splice (11)
    		}
    	}

  6. #6
    TheCanadian's Avatar
    10,251
    posts
    Noo doot aboot it, eh?
    I don't really understand what you're trying to do, but that doesn't seem like the best solution.
    Proud Montanadian
    We tolerate living and breathing. And niches.

    Name Brand Watches

    Maybe getTimer() or TweenMax is the answer to your problem . . .

  7. #7
    Well, it's not the total solution -- just a simple setup of an Array I can hopefully use in my solution, but I was excited about it.

    Trace the array, and you get something like
    r1,r1,r1,r2,r2,r2,r3,r1,r1,r2,r3

    That's randomPlayer1 takes 3 moves, randomPlayer2 takes 3 moves, randomPlayer3 takes 1 move; randomPlayer1 takes 2 more moves, then randomPlayer2 gets 1 final move as does randomPlayer3

    I'm planning to use this information to map out all my functions through 11 steps of a timer.
    Not sure how many permutations I'll have to account for in my Conditionals, but my functions are pretty flexible and most of the parameters are already cycling through pretty well.

    I was planning to "roll my three-sider" as the Timer was going, which simply would have made all the Permutations even more unwieldy.


    But, overall, yeah I know my code is clunky and verbose right now -- there's a lot I don't know; I'm just hammering away at my project, poring through reference materials, using a lot of trial and error. One month in, I think I've learned a lot. But there's a crapload more!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012