PDA

View Full Version : [AS2] Problem with Scoring system


janhung
04-18-2009, 02:27 PM
Hi. Hopefully this isn't too confusing and I really hope that someone could help!!!

I am trying to make a game where 2 types of images would drop from the sky at regular intervals. One type ("c" below) would add points and the other ("f" below) would deduct points if the player's character gets "hit" by the images. ("Player" moves left and right with cursor) And if either 'hits' the player, it would immediately disappear (as if it's dropped into the player's cart). If it is not 'caught' by the player, it would fall through the ground.

I put the following as the AS for a layer. Now, when the the first "c" reaches the horizontal level of the "player", the second "c", which would be on its way down, would disappear (instead of going all the way down) and when the third "c" reaches the horizontal level of the "player", it disappears no matter where the "player" is. After that, all images ("c" and "f") just fall through the "player" even if the "player" is right below them.

Also, only the first "c" gets counted towards "points". All "f" gets counted.

I would appreciate it if you could let me know what's wrong. Or if you have done something similar / have suggestions for a better scoring system... Thanks!!!

points = 0;

//variables for "c"
var delay_c = 1200;
var now_c = getTimer();
var tc = 1;
var ID_c = 1;
var cIN = 0;

//variables for "f"
var delay_f = 2000;
var now_f = getTimer();
var tf = 1;
var ID_f = 1;
var fIN = 0;

onEnterFrame = function(){
stop();

//score calculation
points = cIN*100 - fIN*50;
score.text = points;

//x-range of mouse cursor
if (_root._xmouse > (600-player._width)) {
target = 600-player._width;
} else if (_root._xmouse < 0) {
target = 0;
} else {
target = _root._xmouse;
};

//movement of player
player._x = player._x + (target-player._x)*0.3;

//duplicate falling "c" at random x
if ((getTimer()-now_c) >= delay_c) {
tc++;
duplicateMovieClip(c1,"c"+tc,tc);
_root["c"+tc]._x = random(565) + 25;
now_c = getTimer();
};

//when "c" falls into cart
if ((_root["c"+ID_c].cBegin._y + _root["c"+ID_c].cBegin._height) > 354) {
trace(ID_c);
if ((_root["c"+ID_c]._x >= _root.player._x) && ((_root["c"+ID_c]._x+_root["c"+ID_c]._width) <= (_root.player._x+_root.player._width))) {
trace(ID_c+"in");
cIN = cIN + 1;
trace(cIN);
_root["c"+ID_c]._alpha = 0;
}
ID_c = ID_c + 1;
trace(ID_c);
_root["c"+ID_c]._alpha = 100;
};

//duplicate falling "f" at random x
if ((getTimer()-now_f) >= delay_f) {
tf++;
duplicateMovieClip(f1,"f"+tf,tf);
_root["f"+tf]._x = random(565) + 25;
now_f = getTimer();
};

//when "f" falls into cart
if ((_root["f"+ID_f].fBegin._y + _root["f"+ID_f].fBegin._height) > 354) {
if (((_root["f"+ID_f]._x+_root["f"+ID_f]._width) > _root.player._x) && (_root["f"+ID_f]._x <= (_root.player._x+_root.player._width))) {
fIN++;
_root["f"+ID_f]._alpha = 0;
}
ID_f++;
_root["f"+ID_f]._alpha = 100;
};

//gameover
if (points < 0) {
nextScene();
};
};

DJRoberts
04-18-2009, 06:12 PM
Here is an example that might help.

You should use hitTest method works much easier for this sort of thing.

kkbbcute
04-19-2009, 03:23 AM
Also, one thing to point out from your code:
if (points < 0) {
nextScene();
};
Don't use scenes. Based on common sense, I'd guess that the next scene would be showing something like "You Lose/Win!, you score is ... " But the problem with scenes is this. 50% of the time, the variable that you used to store the player score etc would be claered off by jumping to the next scene. I haven't found out the actual cause yet but it happens. So if you happen to be unlucky, you'd end up with a undefined variable as the player score.

It's actually more of a good practice, scenes may work fine for you now, but it won't always work. So that's just something to kep in mind. ;)

janhung
04-19-2009, 01:15 PM
Thanks DJRoberts for the example and kkbbcute for the advice, I'll keep that in mind!! Two more things tho
1. altho the scoring system is working, for some reason, the score shown is the absolute value of the actual score, i.e. even when the output shows that it's -20, the number shown on the flash game itself is 20.
2. After I moved the results page to frame 2 of the same scene, the icons that were falling from above would still be falling down. Is there a way to make them go away for that frame?

Thanks in advance for your help!!!

kkbbcute
04-19-2009, 01:21 PM
To make the icons "go away", just activate a function on that frame which says something like "button1._visible = false", etc...

DJRoberts
04-19-2009, 01:26 PM
1) The reason for this is because you need to embed the "-" symbol to your score textfield:

Locate the score textfield (its in the score movieclip in the library)
Highlight the textfield and then click embed loacted in the properties tab at the bottom
locate and highlight the correct option (with minus symbol "-" in). Select "Embed".

Your textfield will now display negative numbers. Flash itself was always storing the correct score.


2) The example I showed you uses setInterval, therefore to stop the targets from dropping you need to clear the interval on the frame where the results show or preferably before:

i.e. in my game example
//an example
if(score == 100){
clearInterval(myInt);
clearInterval(myIntTwo);
_root.gotoAndPlay("results");
}

kkbbcute
04-19-2009, 01:27 PM
2) The example I showed you uses setInterval, therefore to stop the targets from dropping you need to clear the interval on the frame where the results show or preferably before:

i.e. in my game example
//an example
if(score == 100){
clearInterval(myInt);
clearInterval(myIntTwo);
_root.gotoAndPlay("results");
}

But unless you change the visible property, it won't go away, would it? ;)

DJRoberts
04-19-2009, 01:35 PM
yes it will, because the setInterval makes a call to the function which attaches the target movieclips, therefore if the interval is cleared the calls made to this function will be stopped.

i.e. there will be no need to use _visible as nothing will be attached to the stage.

janhung
04-19-2009, 01:46 PM
Thank you both again!

DJRoberts, regarding #2, it would stop new ones from generating, but the ones that are already there, they would continue going down. Is there anyway to make those disappear as well?? Thanks a lot!!!!!

DJRoberts
04-19-2009, 01:53 PM
Obviously you wouldn;t want them to just disappear as this would look jumpy and unneat. To make it look better I would disbale movement of the ghost character and calculate when the last target has reached the Stage.height before playing the results.

DJRoberts
04-19-2009, 02:48 PM
This example uses kkbbcutes suggestion which is in fact much easier to implement. The other way is possible but takes more time.

Here is an example using _visible.

kkbbcute
04-20-2009, 01:37 PM
This example uses kkbbcutes suggestion which is in fact much easier to implement. The other way is possible but takes more time.

Here is an example using _visible.

Told you it'll be better (time wise) :p