Forums

ForumsProgramming Forum

structure for scoring stystem

Posted Dec 1, '13 at 5:06am

alex57834

alex57834

89 posts

I just wanted a quick structure on a simple scoring system.

 

Posted Dec 1, '13 at 10:18am

weirdlike

weirdlike

656 posts

Knight

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();
}

}
}
}

 

Posted Dec 1, '13 at 10:32am

mightybob

mightybob

367 posts

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.

 

Posted Dec 2, '13 at 2:02pm

alex57834

alex57834

89 posts

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 oranges:orange = 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;
}
}
}

}

 

Posted Dec 2, '13 at 2:25pm

alex57834

alex57834

89 posts

Sorry just realized what I had to do Thanks for all the help. I will show you the game when its finished:)

 

Posted Dec 2, '13 at 2:31pm

alex57834

alex57834

89 posts

Could you simply explain what

score.digits.text = points.toString(); means

and

.getChildAt(i);

 

Posted Dec 2, '13 at 2:53pm

alex57834

alex57834

89 posts

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&quot;);

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();
}
}

}

}

 

Posted Dec 2, '13 at 3:00pm

weirdlike

weirdlike

656 posts

Knight

first I will explain .getChildAt(i);

for(var i:int = 0; i &lt fallFruit.numChildren;i++)

here is the originating line that creates the variable of i

for(var i:int = 0; i &lt fallFruit.numChildren;i++)

then if i is less than then number of children

for(var i:int = 0; i &lt 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?

 

Posted Dec 2, '13 at 3:19pm

weirdlike

weirdlike

656 posts

Knight

I hope this img helps to understand

http://i.imgur.com/UFCeoRJ.jpg

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

http://imageshack.us/a/img163/8462/58787930.jpg
another look into the box

 

Posted Dec 2, '13 at 3:42pm

weirdlike

weirdlike

656 posts

Knight

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();

 
Reply to structure for scoring stystem

You must be logged in to post a reply!