School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,154 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,612 people online right now. Registration is fast and FREE... Join Now!




help to score counter

2 Pages V  1 2 >  

help to score counter

copenhagen

21 Mar, 2008 - 12:13 PM
Post #1

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi

I have some problems making a score counter in my flash 8 movie, so I hope somebody can help me!

The score counter should give the player a point, each time the player reach frame 40 or 50 in my movie.

First I created one dynamic textfield on frame 1 and called it scorecounter in the var field.

Then I wrote this code in frame one of my movie:

scorecounter = 0;



After this I wrote this code on frame 40 and 50:



onEnterFrame = function () {

scorecounter++; //by the way it doesn`t help if I write: _root.scorecounter += 1 or ++;

}

anybody who can help me to solve my problem?


User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >  
Reply to this topicStart new topic
Replies(1 - 19)

thehat

RE: Help To Score Counter

21 Mar, 2008 - 01:45 PM
Post #2

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
Hi copenhagen,

First of all, I don't think you want to use onEnterFrame. That executes the function you define once every frame at the framerate of your movie until you stop it again. I also suggest not using the var facility of you text box. Instead, give it the instance name of scorecounter. Now put this code on frame 1:

as

var score = 0;
scorecounter.text = score;

function updateScore() {
scorecounter.text = ++score;
}


What is going on here is that you create the variable score, set it to 0 and make your textbox display that value. The function, when called, will update the text in scorecounter with an incremented score (putting the ++ first makes the increment happen before the update). I find using the text property of a text field to be a lot more reliable than the var.

Then on each frame that you want to increase the score you simply do this:

as

updateScore();


Hope that helps smile.gif
User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

22 Mar, 2008 - 10:49 AM
Post #3

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

and thanks for your help once again!

In the beginning the code didn`t work, but thanks to your explanation about the use of the onEnterFrame function, I called my functions instead and boom then the counter worked. However I still have some problems concerning the counter.
You see I think the problem is that I still have an onEnterFrame function on frame 1 saying:
onEnterFrame = function() {
raw = Math.random();
times6 = raw * 6;
die = Math.ceil(times6);
if(die == 4) {
gotoAndStop(10);

}
}

The reason I still have this onEnterFrame function is that I don`t know how to make this frame repeat again and again until the condition is true. A loop will time out maybe after 15 seconds, and in theori the condition could be false for 90 seconds, before I want it to move to the end screen. If I write this code instead and try to make it into a timeline loop:
function chance() {
raw = Math.random();
times6 = raw * 6;
die = Math.ceil(times6);
if(die == 4) {
gotoAndStop(10);
}else{
gotoAndPlay(1);
}
}
chance();

then after less than a second ( each time)the movie goes to a another frame 40, 50, 60 or 70, and if I write:
gotoAndStop(1)

on frame 40, 50, 60 or 70 then my movie goes crazy.
Is it my onEnterFunction who mess up everything or what do I do wrong?

User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

22 Mar, 2008 - 05:42 PM
Post #4

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
Hey,

If you put your code in code tags ( code.gif ) it makes it a lot easier to read!

So anyway, I'm not sure I understand what you're trying to do, but what currently happens in the function version of your code is that if die doesn't equal 4, the playhead just carries on and doesn't execute the function again. I'm only guessing, but if you want to hold the playhead on frame one until die equals four you can do something like this:

as

//halt the playhead
stop();

//execute the following code
//until the while condition is met.
do {
die = Math.ceil(Math.random() * 6);
//output the value of die
trace(die);
} while (die != 4)

//executes when the loop is finished
//and die equals 4
gotoAndStop(10);


A loop like this will execute very quickly though, so if you are wanting to create a delay, you can use setInterval to create a loop that executes more slowly. This example will check if die equals 4 once every half second:

as

//halt the playhead
stop();

//prepare the function
function chance() {
die = Math.ceil(Math.random() * 6);
trace(die);
if(die == 4) {
//stop the loop by clearing the interval
//when die equals 4
clearInterval(intID);
gotoAndStop(10);
}
}

//create an interval calling chance() every 500 milliseconds
var intID = setInterval(this, "chance", 500);


Finally, you could use onEnterFrame if you want the loop to execute at the actual frame rate of your movie, but make sure you clear it when you have finished with it:

as

//halt the playhead
stop();

//prepare the function
function check() {
die = Math.ceil(Math.random() * 6);
trace(die);
if(die == 4) {
//clear the onEnterFrame when die equals 4
this.onEnterFrame = null;
gotoAndStop(10);
}
}

//Create an onEnterFrame and assign it
//to the check function
this.onEnterFrame = check;


Like I say though, I'm only guessing about what you're trying to achieve. If this doesn't help you out could you explain a bit more about what you are trying to create?

This post has been edited by thehat: 22 Mar, 2008 - 05:44 PM
User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

22 Mar, 2008 - 06:58 PM
Post #5

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

Sorry about the code tags, I am very new to this forum, so I wasn`t aware of it, but will remember it next time!

I appreciate your help very much, but something in the code I already have written down is messing up everything in my movie incl. your code, I just don`t know what it is.
The thing I want to do is to make a simulation of a soccer match based on variables. Meaning Player A has 3 variables: midtfield, attack and defense the same goes with player B.
if the number 4 is the random number on frame 1, then the movie will move on to frame 10, where a new random number will decide who gets the chance. If the chance goes to player A the movie will go to frame 20 and if player B gets the chance the movie goes to frame 30. If the movie is on frame 20 then a random number decides weather PlayerA scores or Not and the same goes on frame 30 for player B. then the movie either goes to a frame who shows a random movie clip where the player scores or misses. After that the process starts over until the timer reaches 90.

The problems I have left before my game is complete is to make the counter work, go back to frame 1 without messing up my movie and to play a random movie clip.
I don`t know if you would be interested to see my code which basically consist of to find a random number and then if statements?
User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

23 Mar, 2008 - 07:49 AM
Post #6

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
I think it will help if you post all you code so I can see where the conflicts are.

You might find it useful to not go back to frame 1 every time. Try using frame one to set up your 90 minute timer and to declare functions, then use a different frame as your return point after every goal attempt.
User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

23 Mar, 2008 - 09:11 AM
Post #7

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

First of all, thanks for taken the time to help me icon_up.gif

I tried to make a different return point after a goal attempt but without any luck, so I will try to post my game sofar.
First give frame one the frame label "chance". Then make a dynamic field and put the number 0 into it and call it "mySeconds" in the var field.
After that make a new dynamic field and place it like the other dynamic field on the "chance" frame. Call this field "scorecounter" in the instance place.
Then make a keyframe on frame 10 and call it "tilhvem" another keyframe on frame 20 and call it "spillerA" another keyframe on frame 30 and call it "spillerB", another keyframe on frame 40 and call it "Ascorer", another keyframe on frame 50 and call it "Bscorer", another keyframe on frame 60 and call it "Amisses", another keyframe on frame 70 and call it "Bmisses", and make the last keyframe on frame 80 and don`t call it anything.
Okay the stage has been set, so now the code. On the "chance" frame place this code:
CODE

stop();
var score = 0;
scorecounter.text = score;
function updateScore() {
    scorecounter.text = ++score;
}
    
function wait() {
    mySeconds++;
    if (mySeconds == 91) {
        gotoAndStop(80);
    }
}
myTimer = setInterval(wait, 1000);


function chance() {
    raw = Math.random();
    times6 = raw * 100;
    die = Math.ceil(times6);
    if(die == 4) {
        gotoAndStop("tilhvem");
    }else{
        gotoAndPlay("chance");
    }
}
chance();


On the "tilhvem" frame place this code:
CODE

stop();

var playerA = 85;
var playerB = 83;
var total = playerA - playerB;

if (total == 0) {
    var playerAM1 = 1;
    var playerAM2 = 114;
    var playerBM1 = 115;
    var playerBM2 = 230;
} else if (total > 0 && total <= 10) {
    var playerAM1 = 111;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 110;
} else if (total > 10 && total <= 20) {
    var playerAM1 = 101;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 100;
} else if (total > 20 && total <= 30) {
    var playerAM1 = 91;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 90;
} else if (total > 30 && total <= 40) {
    var playerAM1 = 81;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 80;
}else if (total > 40 && total <= 50) {
    var playerAM1 = 71;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 70;
}else if (total > 50 && total <= 60) {
    var playerAM1 = 61;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 60;
}else if (total > 60 && total <= 70) {
    var playerAM1 = 51;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 50;
}else if (total > 70 && total <= 80) {
    var playerAM1 = 41;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 40;
}else if (total > 80 && total <= 90) {
    var playerAM1 = 31;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 30;
}else if (total > 90 && total <= 100) {
    var playerAM1 = 21;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 20;
}else if (total > 100) {
    var playerAM1 = 11;
    var playerAM2 = 230;
    var playerBM1 = 1;
    var playerBM2 = 10;
// Husk minus 10 er større end minus 90, hvis du satte et mindre end tegn der hvis "total" var minus 10
//ville "what" ikke blive vist fordi minus 10 er større end minus 90
}else if (total < 0 && total >= -10) {
    var playerAM1 = 1;
    var playerAM2 = 110;
    var playerBM1 = 111;
    var playerBM2 = 230;
}else if (total < -10 && total >= -20) {
    var playerAM1 = 1;
    var playerAM2 = 100;
    var playerBM1 = 101;
    var playerBM2 = 230;
}else if (total < -20 && total >= -30) {
    var playerAM1 = 1;
    var playerAM2 = 90;
    var playerBM1 = 91;
    var playerBM2 = 230;
}else if (total < -30 && total >= -40) {
    var playerAM1 = 1;
    var playerAM2 = 80;
    var playerBM1 = 81;
    var playerBM2 = 230;
}else if (total < -40 && total >= -50) {
    var playerAM1 = 1;
    var playerAM2 = 70;
    var playerBM1 = 71;
    var playerBM2 = 230;
}else if (total < -50 && total >= -60) {
    var playerAM1 = 1;
    var playerAM2 = 60;
    var playerBM1 = 61;
    var playerBM2 = 230;
}else if (total < -60 && total >= -70) {
    var playerAM1 = 1;
    var playerAM2 = 50;
    var playerBM1 = 51;
    var playerBM2 = 230;
}else if (total < -70 && total >= -80) {
    var playerAM1 = 1;
    var playerAM2 = 40;
    var playerBM1 = 41;
    var playerBM2 = 230;
}else if(total < -80 && total >= -90) {
    var playerAM1 = 1;
    var playerAM2 = 30;
    var playerBM1 = 31;
    var playerBM2 = 230;
}else if(total < -90 && total >= - 100) {
    var playerAM1 = 1;
    var playerAM2 = 20;
    var playerBM1 = 21;
    var playerBM2 = 230;
} else {
    var playerAM1 = 1;
    var playerAM2 = 10;
    var playerBM1 = 11;
    var playerBM2 = 230;
}


function tilhvem() {
    raw = Math.random();
    times6 = raw * 230;
    die = Math.ceil(times6);
    if(die >= playerAM1 && die <= playerAM2) {
    gotoAndStop("spillerA");
    }
    if(die >= playerBM1 && die <= playerBM2) {
        gotoAndStop("spillerB");
    }
}
tilhvem();


On the frame called "spillerA" place this code:
CODE

stop();

var playerAA = 84;
var playerBF = 86;
var total = playerAA - playerBF;

if (total == 0) {
    var playerAA1 = 1;
    var playerAA2 = 114;
    var playerBF1 = 115;
    var playerBF2 = 230;
} else if (total > 0 && total <= 10) {
    var playerAA1 = 111;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 110;
} else if (total > 10 && total <= 20) {
    var playerAA1 = 101;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 100;
} else if (total > 20 && total <= 30) {
    var playerAA1 = 91;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 90;
} else if (total > 30 && total <= 40) {
    var playerAA1 = 81;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 80;
}else if (total > 40 && total <= 50) {
    var playerAA1 = 71;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 70;
}else if (total > 50 && total <= 60) {
    var playerAA1 = 61;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 60;
}else if (total > 60 && total <= 70) {
    var playerAA1 = 51;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 50;
}else if (total > 70 && total <= 80) {
    var playerAA1 = 41;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 40;
}else if (total > 80 && total <= 90) {
    var playerAA1 = 31;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 30;
}else if (total > 90 && total <= 100) {
    var playerAA1 = 21;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 20;
}else if (total > 100) {
    var playerAA1 = 11;
    var playerAA2 = 230;
    var playerBF1 = 1;
    var playerBF2 = 10;
// Husk minus 10 er større end minus 90, hvis du satte et mindre end tegn der hvis "total" var minus 10
//ville "what" ikke blive vist fordi minus 10 er større end minus 90
}else if (total < 0 && total >= -10) {
    var playerAA1 = 1;
    var playerAA2 = 110;
    var playerBF1 = 111;
    var playerBF2 = 230;
}else if (total < -10 && total >= -20) {
    var playerAA1 = 1;
    var playerAA2 = 100;
    var playerBF1 = 101;
    var playerBF2 = 230;
}else if (total < -20 && total >= -30) {
    var playerAA1 = 1;
    var playerAA2 = 90;
    var playerBF1 = 91;
    var playerBF2 = 230;
}else if (total < -30 && total >= -40) {
    var playerAA1 = 1;
    var playerAA2 = 80;
    var playerBF1 = 81;
    var playerBF2 = 230;
}else if (total < -40 && total >= -50) {
    var playerAA1 = 1;
    var playerAA2 = 70;
    var playerBF1 = 71;
    var playerBF2 = 230;
}else if (total < -50 && total >= -60) {
    var playerAA1 = 1;
    var playerAA2 = 60;
    var playerBF1 = 61;
    var playerBF2 = 230;
}else if (total < -60 && total >= -70) {
    var playerAA1 = 1;
    var playerAA2 = 50;
    var playerBF1 = 51;
    var playerBF2 = 230;
}else if (total < -70 && total >= -80) {
    var playerAA1 = 1;
    var playerAA2 = 40;
    var playerBF1 = 41;
    var playerBF2 = 230;
}else if(total < -80 && total >= -90) {
    var playerAA1 = 1;
    var playerAA2 = 30;
    var playerBF1 = 31;
    var playerBF2 = 230;
}else if(total < -90 && total >= - 100) {
    var playerAA1 = 1;
    var playerAA2 = 20;
    var playerBF1 = 21;
    var playerBF2 = 230;
} else {
    var playerAA1 = 1;
    var playerAA2 = 10;
    var playerBF1 = 11;
    var playerBF2 = 230;
}

function spillerA() {
    raw = Math.random();
    times6 = raw * 230;
    die = Math.ceil(times6);
    if(die >= playerAA1 && die <= playerAA2) {
    gotoAndStop("Ascorer");
    }else{
        gotoAndStop("Amisses");
    }
}
spillerA();


On the frame called "spillerB" place this code:
CODE

stop();

var playerAF = 87;
var playerBA = 86;
var total = playerAF - playerBA;

if (total == 0) {
    var playerAF1 = 1;
    var playerAF2 = 114;
    var playerBA1 = 115;
    var playerBA2 = 230;
} else if (total > 0 && total <= 10) {
    var playerAF1 = 111;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 110;
} else if (total > 10 && total <= 20) {
    var playerAF1 = 101;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 100;
} else if (total > 20 && total <= 30) {
    var playerAF1 = 91;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 90;
} else if (total > 30 && total <= 40) {
    var playerAF1 = 81;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 80;
}else if (total > 40 && total <= 50) {
    var playerAF1 = 71;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 70;
}else if (total > 50 && total <= 60) {
    var playerAF1 = 61;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 60;
}else if (total > 60 && total <= 70) {
    var playerAF1 = 51;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 50;
}else if (total > 70 && total <= 80) {
    var playerAF1 = 41;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 40;
}else if (total > 80 && total <= 90) {
    var playerAF1 = 31;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 30;
}else if (total > 90 && total <= 100) {
    var playerAF1 = 21;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 20;
}else if (total > 100) {
    var playerAF1 = 11;
    var playerAF2 = 230;
    var playerBA1 = 1;
    var playerBA2 = 10;
// Husk minus 10 er større end minus 90, hvis du satte et mindre end tegn der hvis "total" var minus 10
//ville "what" ikke blive vist fordi minus 10 er større end minus 90
}else if (total < 0 && total >= -10) {
    var playerAF1 = 1;
    var playerAF2 = 110;
    var playerBA1 = 111;
    var playerBA2 = 230;
}else if (total < -10 && total >= -20) {
    var playerAF1 = 1;
    var playerAF2 = 100;
    var playerBA1 = 101;
    var playerBA2 = 230;
}else if (total < -20 && total >= -30) {
    var playerAF1 = 1;
    var playerAF2 = 90;
    var playerBA1 = 91;
    var playerBA2 = 230;
}else if (total < -30 && total >= -40) {
    var playerAF1 = 1;
    var playerAF2 = 80;
    var playerBA1 = 81;
    var playerBA2 = 230;
}else if (total < -40 && total >= -50) {
    var playerAF1 = 1;
    var playerAF2 = 70;
    var playerBA1 = 71;
    var playerBA2 = 230;
}else if (total < -50 && total >= -60) {
    var playerAF1 = 1;
    var playerAF2 = 60;
    var playerBA1 = 61;
    var playerBA2 = 230;
}else if (total < -60 && total >= -70) {
    var playerAF1 = 1;
    var playerAF2 = 50;
    var playerBA1 = 51;
    var playerBA2 = 230;
}else if (total < -70 && total >= -80) {
    var playerAF1 = 1;
    var playerAF2 = 40;
    var playerBA1 = 41;
    var playerBA2 = 230;
}else if(total < -80 && total >= -90) {
    var playerAF1 = 1;
    var playerAF2 = 30;
    var playerBA1 = 31;
    var playerBA2 = 230;
}else if(total < -90 && total >= - 100) {
    var playerAF1 = 1;
    var playerAF2 = 20;
    var playerBA1 = 21;
    var playerBA2 = 230;
} else {
    var playerAF1 = 1;
    var playerAF2 = 10;
    var playerBA1 = 11;
    var playerBA2 = 230;
}

function spillerB() {
    raw = Math.random();
    times6 = raw * 230;
    die = Math.ceil(times6);
    if(die >= playerAF1 && die <= playerAF2) {
    gotoAndStop("Bmisses");
    }else{
        gotoAndStop("Bscorer");
    }
}
spillerB();


On the frame called "Ascorer" place a circle on the stage and make it into a movieclip called ball and place this code on the frame and not the movieclip:
CODE

clearInterval(myTimer);
    stop();
    updateScore();
    trace ("PLAYER A SCORER");
ball.onEnterFrame = function() {
    ball._x += 5;
}


On the frame called "Bscorer" make another circle and make it into a movieclip and call it ball1 and place this code on the frame and not the movieclip:
CODE

clearInterval(myTimer);
    stop();
    updateScore();
    trace ("PLAYER B SCORER");
ball1.onEnterFrame = function() {
    ball1._x += 5;
}


On the frame called "Amisses" make another circle on the stage, make it into a movieclip and call it ball2 and write this code on the frame and not the movieclip:
CODE

clearInterval(myTimer);
    stop();
    trace ("PLAYER A MISSES");
ball2.onEnterFrame = function() {
    ball2._x += 5;
}


On the frame called "Bmisses" make another circle, make it into a movieclip and call it ball3 and
write this code on the frame and not the movieclip.
CODE

clearInterval(myTimer);
    stop();
    trace ("PLAYER B MISSES");
ball3.onEnterFrame = function() {
    ball3._x += 5;
}


On frame 80 write this code:
CODE

stop();


Hope all the above maked sense smile.gif


User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

24 Mar, 2008 - 09:33 AM
Post #8

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
Hi copenhagen,

There are a few points to make, I'll try and cover everything, and attach the file I've created at the bottom of this post if you want to check it out.

First of all, your timer. You have to set the mySeconds variable initially, otherwise the incremental operator in your wait() function won't work.

Next, the thing about the return point. I moved the 'chance' frame label to frame 2, and on frame to put a call to the chance() function. This ensures that the playhead never goes back to frame 1 and resets all the variables.

Back on frame one, I moved the creation of the interval and the call to chance into a function, and call that at the end of frame 1 instead:

as

function beginAgain() {
myTimer = setInterval(wait, 1000);
chance();
}

beginAgain();


Now you can call this function from any of your results frames to restart the game after a movieclip has played.

On to the results frames then, I made the onEnterFrame look like this (I'll only show one, they're all pretty much the same):

as

clearInterval(myTimer);
stop();
updateScoreA();
trace ("PLAYER A SCORER");

ball.onEnterFrame = function() {
//move the ball
ball._x += 15;
//if the ball is off the stage
if(ball._x > Stage.width) {
//clear the current onEnterFrame
this.onEnterFrame = null;
//restart the game
_root.beginAgain();
}
}


What happens here is that when the ball has moved off the stage the onEnterFrame is stopped and the game is restarted. You may have noticed the updateScore() function has become updateScoreA()? By having two add score functions and two score textfields you show both players scores separately. On frame 1 again:

as

scorecounterA.text = 0;
scorecounterB.text = 0;

function updateScoreA() {
scorecounterA.text = Number(scorecounterA.text) + 1;
}
function updateScoreB() {
scorecounterB.text = Number(scorecounterB.text) + 1;
}


So after all this you will notice that the timer still doesn't go up. This is because the game engine performs it's calculations in less than a second, and each results frame stops the timer, so the timer never gets incremented. If you remove the clearInterval from the misses frames, and change the call to beginAgain() to chance(), then your game works:

as

stop();
trace ("PLAYER A MISSES");

ball2.onEnterFrame = function() {
ball2._x += 15;
if(ball2._x > Stage.width) {
_root.chance();
this.onEnterFrame = null;
}
}


I hope that helps you out, and you don't mind my meddling smile.gif

Attached File  footy_game.zip ( 7.4k ) Number of downloads: 47

User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

24 Mar, 2008 - 12:21 PM
Post #9

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

biggrin.gif I don`t mind your meddling at all, without your help my game would be a disaster!

I tried to download your attached file, but when I try to open it, flash 8 give me a pop up message saying:
CODE

unexpected file format.


Do you know why, or is there another way you can sent the file to me? I think it will make it much easier for me to understand your explanations if I see them inside the movie you maked!

Thanks again for everything smile.gif icon_up.gif

User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

24 Mar, 2008 - 01:35 PM
Post #10

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
Yup, CS3 files will do that biggrin.gif

Try this one.

Attached File  footy_game.zip ( 14.38k ) Number of downloads: 78

User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

25 Mar, 2008 - 06:45 AM
Post #11

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
hi thehat

It worked this time icon_up.gif and the game is exactly what I had in mind smile.gif

I don`t know how I can thank you for all your help, but you really saved my day big time icon_up.gif

Hope to get the same skills as you one day smile.gif
User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

25 Mar, 2008 - 07:22 AM
Post #12

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
You're welcome smile.gif
User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

25 Mar, 2008 - 07:44 AM
Post #13

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

Sorry to write you another question, but I just noticed something in the movie! The frames "Ascorer", "Bscorer", "Amisses" and "Bmisses" are activated constantly, which means that the chance() function must be equal to 4 all the time, even if the code in frame 0ne is saying:
CODE

function chance() {
    raw = Math.random();
    times6 = raw * 100;
    die = Math.ceil(times6);
    if(die == 4) {
        gotoAndStop("tilhvem");
    }else{
        gotoAndPlay("chance");
    }
}


And if I change it to this code:

CODE

function chance() {
    raw = Math.random();
    times6 = raw * 4000;
    die = Math.ceil(times6);
    if(die == 4) {
        gotoAndStop("tilhvem");
    }else{
        gotoAndPlay("chance");
    }
}


then the chance() function is activated just as often? I mean that the random number should be number 4 each second out of 4000 numbers to choose from seems quite unlikely.
Do you know how come this happens?

User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

25 Mar, 2008 - 08:36 AM
Post #14

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
Well, a quick test with a trace before the if shows that the function only actually executes once. This is because the gotoAndPlay prevents code on that frame from executing a second time. Replacing the gotoAndPlay with another call to chance() causes the function to be called multiple times:

as

function chance() {
raw = Math.random();
times6 = raw * 4000;
die = Math.ceil(times6);
trace(die);
if(die == 4) {
gotoAndStop("tilhvem");
}else{
chance();
}
}


The problem you get now is that it is very likely that your function will execute more than 256 times without die equaling 4. Flash doesn't like that happening because it suspects you have created an infinite loop, and so it stops the operation to prevent a crash.

Why do you actually want to perform this random number generation? You aren't using the generated value anywhere, because it is always 4 when the playhead advances to 'tilhvem'. If you are trying to create a random delay then you should use setInterval with a randomly generated time. For the rest of this post, I'll assume that's what you're trying to do.

Delete the call to chance on frame 2, you won't be needing that, then replace the chance and beginAgain functions on frame 1 with these:

as

//return a number between 0 and 4000
function getDelay() {
raw = Math.random();
times6 = raw * 4000;
return Math.ceil(times6);
}

//clear the game delay interval and start the game engine
function proceed() {
clearInterval(gameDelay);
gotoAndStop('tilhvem');
}

//restart the game timer and set a new delay
function beginAgain() {
myTimer = setInterval(wait, 1000);
gameDelay = setInterval(this,'proceed',getDelay());
}


Now you have a choice. If you want to go back to stopping the game timer when a miss happens, you can clear myTimer on the miss frames and change _root.chance() to _root.beginAgain. Otherwise, replace _root.chance() on both miss frames with this:

as

_root.gameDelay = setInterval(_root,'proceed',getDelay());


Hopefully that all makes sense...

User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

25 Mar, 2008 - 11:06 AM
Post #15

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

Well it wasn`t exactly what I had in mind.
When I replace your new code with the old one, I get longer intervals between "Ascorer", "Bscorer", "Amisses" and "Bmisses" but it seems like they come at the same interval each time, meaning each 2 seconds or 4 seconds etc. depending how high I put the number in
CODE

raw * 4000;


It doesn`t seems like I get a random interval. Forinstens, the first time after 3 seconds the next time after 10 seconds etc.

The reason why I used equals to 4 is, that I want to make some code so it is unpredictable when one player gets a chance. Just like a soccer game, where there can be a chance after 4 minutes and then again after 10 minutes and so on, nobody never knows when it happens. If the chance() function in the flash movie is activated, then it will go to the frame I called "tilhvem" which in English means "to who" where a random number will decide if it is to player A or B. If it is to player A then it will go to the frame called "spillerA" meaning "playerA" in English, and if it is to PlayerB then the movie will go to the frame called "spillerB". On the frame called "spillerA" the random number will decide if player A scores or misses, and if the playhead is going to "spillerB" then a random number will decide if player B scores or misses. In both cases taken them to either the frame"Ascorer" or "Amisses (in the case that Player A gets the chance) or "Bscorer" or "Bmisses" (in case player B gets the chance). On those frames I want the timer to stop, while the movieclip is playing and begin again after the movieclip is gone. Then the movie goes back to the "chance" frame, where if the random number is 4, then the same process will happens until the timer reach 90.

My worries now is if you can do this in flash because I noticed too, that flash consider it as an unlimited loop.



User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

25 Mar, 2008 - 11:38 AM
Post #16

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

I just thought I got the answer but no, it didn`t worked out blink.gif

The thing I did was, that I changed the chance() function on frame 1 to this:
CODE

function chance() {
    raw = Math.random();
    times6 = raw * 100;
    die = Math.ceil(times6);
    if(die == 4) {
        gotoAndStop("tilhvem");
    }else{
        chance1();
    }
}


and then I maked a new function called chance1 (in that way the function wouldn`t call on itself again and again) also on frame 1 in my movie looking like this:
CODE

function chance1() {
    raw = Math.random();
    times6 = raw * 100;
    die = Math.ceil(times6);
    if(die == 4) {
        gotoAndStop("tilhvem");
    }else{
        chance();
    }
}


Unfortunately flash didn`t liked that either, but is there another way to make it work????




User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

25 Mar, 2008 - 12:09 PM
Post #17

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
Simple answer is yes, but not with recursive functions. As you've seen, Flash gets upset easily, and this is a good thing because we don't want to overload the users machines. Let's go back to my previous post. If your only problem is that the interval seems very similar each time, then lets increase the possible amount of time. In fact, why not mess with the getDelay() function a bit so we can pass it a minimum and maximum amount of seconds to choose between?

Here is the new getDelay() function:

as

function getDelay(min:Number,max:Number) {
//shameless copy from the help file!
var randomNum = (Math.floor(Math.random() * (max - min + 1)) + min) * 1000;
trace("Delay: "+randomNum);
return andomNum;
}


So now when you call getDelay() during the setting of an interval, you pass it a minimum and maximum number of seconds and it returns a value in milliseconds ready for setInterval to use. It will also write the delay to your output panel so you can assess whether the values need adjustment.

Your setIntervals should look like this:

as

//in the beginAgain function on frame 1:
gameDelay = setInterval(this,'proceed',getDelay(1,5));

//on each of the missed frames:
_root.gameDelay = setInterval(_root,'proceed',getDelay(1,5));


And here's the finished version, have fun smile.gif
Attached File  footy_game.zip ( 14.37k ) Number of downloads: 24


This post has been edited by thehat: 25 Mar, 2008 - 12:10 PM
User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

25 Mar, 2008 - 01:57 PM
Post #18

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
Hi thehat

smile.gif icon_up.gif Thanks that was exactly what I had in mind icon_up.gif

I just added this code to frame 80 cause else the movie would continue a while after the frame 80 screen:
CODE

clearInterval(gameDelay);

Thanks again for all your help I really appreciated it icon_up.gif icon_up.gif icon_up.gif smile.gif
User is offlineProfile CardPM
+Quote Post

thehat

RE: Help To Score Counter

25 Mar, 2008 - 03:52 PM
Post #19

awake ? web();
Group Icon

Joined: 28 Feb, 2008
Posts: 940



Thanked: 99 times
Dream Kudos: 200
My Contributions
No worries, don't forget to post a link when it's all finished!
User is offlineProfile CardPM
+Quote Post

copenhagen

RE: Help To Score Counter

26 Mar, 2008 - 06:53 AM
Post #20

D.I.C Head
**

Joined: 16 Mar, 2008
Posts: 56



Thanked: 1 times
My Contributions
I will smile.gif

but somehow I feel like I will run into some troubles before it`s all done, so hope you keep my game in memory in case I need help rolleyes.gif
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 05:12PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month