Forums → Programming Forum → structure for scoring stystem
11 | 6361 |
- 11 Replies
first you will need to create a score box simply get out your square tool draw a box then, insert a text box from your text tool convert to symbol be sure to embed the font you plan on using. Name the symbol and the text box within it.
for simplicity I have named my symbol scoreBox and the internal text box digits
now for the code
package
{
import flash.display.*;
import flash.events.*;
public class document extends MovieClip
{
var avatarGuy:avatar;
var fallFruit:fallingObjects;
var points:Number;
function document():void
{
points = 0;
fallFruit = new fallingObjects();
addChild(fallFruit);
avatarGuy = new avatar();
avatarGuy.x = 100;
avatarGuy.y = 267;
addChild(avatarGuy);
var score:scoreBox = new scoreBox();
score.x = 30;
score.y = 25;
addChild(score);
score.digits.text = points.toString();
addEventListener(Event.ENTER_FRAME, loop);
function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
var fruit = fallFruit.getChildAt(i);
if(fruit is apple)
{
scoreSystem(5)
}
fallFruit.removeChild(fallFruit.getChildAt(i));
}
}
}
function scoreSystem(n):void
{
points = points+n;
score.digits.text = points.toString();
}
}
}
}
Ok, scoring goes like this:
first you need a "score" variable to hold your scores, so:
var score:Int = 0;
Then, when you hit whatever is your coin or something, you can do:
score += 1;
Finally, when you want to show the score you can do:
score_text.text = ("Score: " + score);
That should work, it has not been tested, though.
How would I set different scores to add to the scores depending on which fruit I hit?
package {
import flash.display.MovieClip;
import flash.utils.*;
import flash.events.TimerEvent;
public class FallingObjects extends MovieClip {
public var appleArray:Array;
public var strawberryArray:Array;
public var orangeArray:Array;
public var watermelonArray:Array;
public var gameTimer:Timer;
public function FallingObjects()
{
appleArray = new Array();
strawberryArray = new Array();
orangeArray = new Array();
watermelonArray = new Array();
gameTimer = new Timer(25);
gameTimer.addEventListener(TimerEvent.TIMER, dropFruit);
gameTimer.start();
}
private function dropFruit(event:TimerEvent):void
{
if (Math.random() < 0.009)
{
var randomXApple:Number = Math.random() * 550;
var randomXStrawberry:Number = Math.random() * 550;
var randomXOrange:Number = Math.random() * 550;
var apples:apple = new apple();
var strawberrys:strawberry = new strawberry();
var orangesrange = new orange();
apples.x = randomXApple;
apples.y = -30;
strawberrys.x = randomXStrawberry;
strawberrys.y = -30;
oranges.x = randomXOrange;
oranges.y = -30;
appleArray.push(apples);
addChild(apples);
strawberryArray.push(strawberrys);
addChild(strawberrys);
orangeArray.push(oranges);
addChild(oranges);
}
for each(apples in appleArray)
{
apples.y += 1.4;
}
for each(strawberrys in strawberryArray)
{
strawberrys.y += 2.2;
}
for each (oranges in orangeArray)
{
oranges.y++;
}
if (Math.random() < 0.003)
{
var randomXWatermelon:Number = Math.random() * 550;
var watermelons:watermelon = new watermelon();
watermelons.x = randomXWatermelon;
watermelons.y = -30;
watermelonArray.push(watermelons);
addChild(watermelons);
}
for each(watermelons in watermelonArray)
{
watermelons.y += 3.8;
}
}
}
}
Sorry just realized what I had to do Thanks for all the help. I will show you the game when its finished
Could you simply explain what
score.digits.text = points.toString(); means
and
.getChildAt(i);
It came up with this error
TypeError: Error #1010: A term is undefined and has no properties.
at Document()
I created the box with the square tool. Made a text box and typed 78 into it made it so I could see the text and made only the text box into a symbol with the name of digits. Then I selected the text box and the rectangle together and symbolized it and called it scoreBox. I'm pretty sure I follow your instructions.
public class Document extends MovieClip {
var avatarGuy:avatar;
var fallFruit:FallingObjects;
var points:Number;
public function Document()
{
trace("Document Class Working"
points = 0;
var avatarGuy = new avatar(stage);
avatarGuy.x = 50;
avatarGuy.y = 267;
addChild(avatarGuy);
var fallFruit = new FallingObjects();
addChild(fallFruit);
var score:scoreBox = new scoreBox();
score.x = 250;
score.y = 350;
addChild(score);
score.digits.text = points.toString();
addEventListener(Event.ENTER_FRAME, loop);
function loop(event):void
{
for(var i:int = 0; i < fallFruit.numChildren;i++)
{
if(avatarGuy.hitTestObject(fallFruit.getChildAt(i)))
{
var fruit = fallFruit.getChildAt(i);
if(fruit is apple)
{
scoreSystem(5);
}
fallFruit.removeChild(fallFruit.getChildAt(i));
}
}
}
function scoreSystem(n):void
{
points = points + n;
score.digits.text = points.toString();
}
}
}
}
first I will explain .getChildAt(i);
for(var i:int = 0; i < fallFruit.numChildren;i++)
here is the originating line that creates the variable of i
for(var i:int = 0; i < fallFruit.numChildren;i++)
then if i is less than then number of children
for(var i:int = 0; i < fallFruit.numChildren;i++)
add to i
this is the line that counts the number of containing children and gives them the property of i
.getChildAt(i);
points to the particular movie clip that you are referencing. i could be a number of 0 to unlimited, for each movie clip it adds 1 to i
make sense?
I hope this img helps to understand
the red arrow on the left points to the movie clip symbol, the arrow on the right is the instance name of the text box
another look into the box
oop the picture was squashed click HERE for the full img
if this is too much right now you can add via code
first import at the top
import flash.text.*;
change this
var score:scoreBox = new scoreBox();
score.x = 250;
score.y = 350;
addChild(score);
to this
var scoreB:TextField = new TextField();
scoreB.x = 250;
scoreB.y = 350;
addChild(scoreB);
and both of these
score.digits.text = points.toString();
to this
scoreB.text = points.toString();
I tried to keep on going with the symbol stuff and I typed in 0 in the box and then made called gave it the instance name of digits and than I ran it and as soon as a touched a apple it went blank and when I hit another apple it went back to saying 0.
Oh I got rid of the 0 and it works perfectly now. Hopefully no more problems before finishing the game and you explain things very well. Thanks
You must be logged in to post a reply!