How can I graph a series of scores? - actionscript-3

I am trying to make a score graph. I have last seven scores of the game. I could use the graphics.lineTo function but it takes so long.
Is there an easy way to do this ?
Example graph.
Here is what I tried so far. I made 20-40-60-80 lines movieclip and duplicate and change their name of each level.
if(score1 == 40 && score2 == 20 && makeline == true){
line20.x = 184;
line20.y = 411;
yirmiasah2 = true;//yirmi asa
}
if(score1 == 40 && score2 == 40 && makeline == true){
line40.x = 184;
line40.y = 411;
}
if(score1 == 40 && score2 == 60 && makeline == true){
line60.x = 183;
line60.y = 366;
}
if(score1 == 40 && score2 == 80 && makeline == true){
line80.x = 172;
line80.y = 384;
}

The question is not clear but you can try something like this.
Somewhere in your code you undoubtedly have a variable holding the score of the current game. You haven't shown that, so I'll call that current score from the last game currentScore of type int. First this variable is declared outside of any functions.
var currentScore:int;
Then, while the game is being played, this value goes up whenever the player earns a point. In my example, he gets a point when Sprite A touches Sprite B
if (spriteA.hitTestObject(spriteB)){
newScore++; // adds one to the newScore variable
}
Make an array to hold your scores in. Put this outside of any functions. Putting it at the same place you put the currentScore variable makes sense.
var scoreArray:Array = new Array();
As each score is made final (at the end of a game), add that score to the array:
if (gameOver == true){
// make a new variable to add to the array and give it the same value as the score of the last game
var newScore:int = currentScore;
// add the new variable to the array
scoreArray.push(newScore);
// this adds the newScore variable to the array.
// after 7 games, you'll have 7 values in the array.
}
Then to draw the graph:
var scoreGraph:Sprite = new Sprite();
addChild(scoreGraph);
scoreGraph.graphics.moveTo(0,100);
scoreGraph.graphics.lineStyle(1);
for (var i:int = 0; i < scoreArray.length; i++){
scoreGraph.graphics.lineTo(i*10,100-scoreArray[i]);
// this will space out along the x axis by 10 pixels and put the x axis at 100 pixels down.
}

Related

Make MC visible depending on how make key presses AS3

Probably another easy one, but after hours of searching and trying I still cant get this working.
I have a keypad with 10 buttons (0 - 9) - I want to create a variable that stores the number of key presses on the keypad and displays each MC accordingly in sequence depending on how many presses have been made.
For example, if only 1 key has been pressed, then only the first MC displays (and increments the variable to '1'), if 2 keys have been pressed then the first and second MC's are displayed - and so on until all 4 of my MC's are displayed and the MC progresses to frame #33.
I am getting different errors depending on how I structure this (mainly "expecting identifier before greaterthan")
My code for button '0' is below, can anyone assist please?
kpad.b0.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown0);
var clickCount:int = 0;
function onMouseDown0(e:MouseEvent):int {
clickCount++();
if (clickCount <= 0)
as1.visible = true;
if (clickCount == 1)
as1.visible = true;
if (clickCount == 2)
as1.visible = true;
as2.visible = true;
if (clickCount == 3)
as1.visible = true;
as2.visible = true;
as3.visible = true;
if (clickCount == 4)
as1.visible = true;
as2.visible = true;
as3.visible = true;
as4.visible = true;
if (clickCount => 4)
this.gotoAndPlay (33)
}

How to loop through frame number and if true then ignore that frame number?

Hey guys so I am struggling the best way to approach this situation.
So I have 5 frames in my ovenScreen.mcChar movie clip. Each is a character that the player can unlock. If the player has enough coins then they can go to the prize screen to get a random unlockable character.
Here is how it is setup so far:
private function getPrizeHandler(e:MouseEvent):void
{
//Check if prize is locked or unlocked then unlock item/ loop through random number frames
frameLoop = randomNumber(1, 5);
ovenScreen.mcChar.gotoAndStop(frameLoop);
if (frameLoop == 1 && !sharedObjectCharacters.data.sharedHotDog)
{
sharedHotDog = true;
sharedObjectCharacters.data.sharedHotDog = sharedHotDog;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 2 && !sharedObjectCharacters.data.sharedTaco)
{
sharedTaco = true;
sharedObjectCharacters.data.sharedTaco = sharedTaco;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 3 && !sharedObjectCharacters.data.sharedDonut)
{
sharedDonut = true;
sharedObjectCharacters.data.sharedDonut = sharedDonut;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 4 && !sharedObjectCharacters.data.sharedCoffee)
{
sharedCoffee = true;
sharedObjectCharacters.data.sharedCoffee = sharedCoffee;
sharedObjectCharacters.flush ();
}else
if (frameLoop == 5 && !sharedObjectCharacters.data.sharedPancakes)
{
sharedPancakes = true;
sharedObjectCharacters.data.sharedPancakes = sharedPancakes;
sharedObjectCharacters.flush ();
}
////////////////////////////////////////
ovenScreen.gotoAndPlay(2); //play animations
TweenLite.delayedCall(3.5, prizeConfettie);
ovenScreen.removeEventListener(MouseEvent.CLICK, getPrizeHandler);
}
As you can see I have the var frameLoop which is a random number from 1 - 5. So the character unlocked will be random and show the random unlocked character. I use the if statements to check if the random number lands on that certain frame and its not the case that it is unlocked then unlock it and save the data.
Now this all works fine but How could I go about fixing it to where if the Item is already unlocked to sort through a different frame number. So if the frameLoop lands on 2 and that character is already unlocked then repeat the random frame number until it lands on a locked character. I was thinking of setting up an array of numbers maybe that approach might be a logical one but not sure how to go about doing so.
Any help would be appreciated thanks.
Additional Info on the shared object Booleans:
private function allSharedObjectBooleans():void
{
sharedObjectCharacters = SharedObject.getLocal("Characters");
sharedHotDog = sharedObjectCharacters.data.sharedHotDog != null ? sharedObjectCharacters.data.sharedHotDog : false;
sharedTaco = sharedObjectCharacters.data.sharedTaco != null ? sharedObjectCharacters.data.sharedTaco : false;
sharedDonut = sharedObjectCharacters.data.sharedDonut != null ? sharedObjectCharacters.data.sharedDonut : false;
sharedCoffee = sharedObjectCharacters.data.sharedCoffee != null ? sharedObjectCharacters.data.sharedCoffee : false;
sharedPancakes = sharedObjectCharacters.data.sharedPancakes != null ? sharedObjectCharacters.data.sharedPancakes : false;
}
and I create them like so:
//shared Booleans
private var sharedHotDog:Boolean;
private var sharedTaco:Boolean;
private var sharedDonut:Boolean;
private var sharedCoffee:Boolean;
private var sharedPancakes:Boolean;
If the character is already unlocked, you increment the variable. If it's overboard (greater than the number of unlockable entities), set it to 1. If it looped through all characters and they all are already unlocked, do something else. And you already have an array of sorts for this, it's just located in sharedObjectCharacters.data and its indexes are string, not int. But this can be remedied by providing a map from int to string, and using this[string] syntax to check for properties. An example:
const strUnlockables:Array=["Noone","sharedHotDog","sharedTaco","sharedDonut","sharedCoffee","sharedPancakes"];
const unlockables:int=5;
private function getPrizeHandler(e:MouseEvent):void {
var f:int=randomNumber(1,5); //frameLoop
for (var i:int=0;i<unlockables;i++) { // check all unlockables, starting with f'th
if (sharedObjectCharacters.data[strUnlockables[f]]===false) {
// this "f" is the one to unlock
sharedObjectCharacters.data[strUnlockables[f]]=true;
sharedObjectCharacters.flush();
ovenScreen.mcChar.gotoAndStop(f);
ovenScreen.gotoAndPlay(2); //play animations
TweenLite.delayedCall(3.5, prizeConfettie);
break;
}
f++;
if (f>unlockables) f=1; // loop around
}
// if we're here, we either did a break, or have all characters unlocked
ovenScreen.removeEventListener(MouseEvent.CLICK, getPrizeHandler); // clean up
}

How can hitTestObject work for many objects?

I have a game where player moves forward over (semi)random tiles. I want a hittest for when the player hits a certain tile.
However when the char hits one of the spawned in floor2's nothing happens.
I think this is because there are multiple 'floor2' movieclips on the stage ?
When i trace the bounds "getBounds" for floor2 the positions it shows keeps changing anyway to Hitest with all of them ?
function handleCollision( e:Event ):void {
if(char.hitTestObject(floor2)){
trace("hit detected");
}
This is how the player spawns in:
var char:Char = new Char();
char.x = 275;
char.y = 786;
cam.addChild(char);
This is how floor2 spawns in :
if (randomRounded > 10 && randomRounded <= 50 ){
floor2 = new Floor2();
floor2.x = -8.45;
floor2.y = 786 - tileCounter;
cam.addChildAt(floor2, stage.numChildren-1);
Extra : ( RandomRounded is a randomly generated number ), ( there is a 'Var floor2:Floor2;')
please help :(
A variable can only ever reference up to one value. So your floor2 variable can only reference one Floor2 object. If you assign a new value, the variable will reference that value.
What you should do is to use an Array, which can hold many objects.
var floors:Array = [];
if (randomRounded > 10 && randomRounded <= 50 ){
floor2 = new Floor2();
floor2.x = -8.45;
floor2.y = 786 - tileCounter;
cam.addChildAt(floor2, stage.numChildren-1);
floors.push(floor2); // add the reference to the array, now floor2 can safely be overwritten by a new value without losing the previous one
}
In your function handleCollision you would then iterate over the array to test each individual floor object. Here's a quick untested example of how that could look like:
function handleCollision( e:Event ):void
{
for (var i:uint = 0; i< floors.length; ++i)
{
if(char.hitTestObject(floors[i]))
{
trace("hit detected");
}
}
}

AS3 - Go to random 5 frames out of 7

Hello and thank you so much for your time.
I'm trying to build a quiz for my students, where the start button will go to a random frame out of 7. Then on the landing frame, question appears and the answer is selected via radiobutton then submitted via another button which goes to the next random question. This needs to happen 5 times so it will pick 5 questions randomly out of 7 and not repeating any previous question. If anyone can point me to right direction, it'll be much appreciated.
//Start Button - AS3 Frame #8157
startBtn.addEventListener(MouseEvent.CLICK, startQuiz);
function startQuiz(event:MouseEvent):void{
}
//Submit Button with score count - AS3 Frame #8158
var count:Number = 0;
var mygroup1:RadioButtonGroup = new RadioButtonGroup("group1");
q1a1.group = q1a2.group = q1a3.group = q1a4.group = q1a5.group = mygroup1;
b1.addEventListener(MouseEvent.CLICK, quizHandler1)
function quizHandler1(event:MouseEvent):void{
if(mygroup1.selection.label=="B) 12") {
count = count + 20;
scoreresult.text = (count).toString();
var number_array:Array = [8158,8159,8160,8161,8162,8163,8164 ];
var final_array:Array = [];
var count_selected:int = 5;
var i:int;
for(i = 0; i < count_selected; i++)
{
if(number_array.length == 0)
break;
else
final_array.push(number_array.splice(Math.floor(Math.random() * number_array.length), 1)[0]);
}
trace(final_array);
}
}
Since you don't want to repeat the same value, you need to know what values you've used already. There's a bunch of ways you could do this, but probably the most straight forward is to put all your values in an array, then remove a random value until the array is empty. Here's an example:
// create an array with all the frames you want to visit
var frames:Array = [0, 1, 2, 3, 4, 5, 6];
// when you want to pick one randomly, remove it using splice
var frame:int = frames.splice(Math.random() * frames.length, 1)[0];
// when the array is empty, you've visited every frame
if(frames.length == 0)
trace("all done!");
Here's the docs on splice(): http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html#splice%28%29

Resetting score in AS3

I am creating a "dice spinner" game by using Flash action script 3, The rules are if the player rolls a 7 or 11 on first throw then they win but if the user rolls a 2,3 or 12 on the first throw then they lose. If the first throw is 4,5,6,8,9 or 10 then that sum becomes the point and that user has to make a point by rolling again until they make the point (they win) or they throw a 7 (they lose)
I have succesfully created the functions for it to spin, generate a random between 1-6 on each "dice" and to add the points up from total but my problem is if the user wins or lose. I can't figure out on how to reset the points back to zero when the user rerolls from last attempt
The code is blow.
import flash.events.MouseEvent;
spinner_mc.stop();
spinner2_mc.stop();
roll_btn.addEventListener(MouseEvent.CLICK, startspin);
stop_btn.addEventListener(MouseEvent.CLICK, stopspin);
spinner_mc.addEventListener(Event.ENTER_FRAME, spin);
spinner2_mc.addEventListener(Event.ENTER_FRAME, spin)
var spinning = false
function spin(evt){
if(spinning){
spinner_mc.rotation += 5
spinner2_mc.rotation += 5
}
} //end spin function
function startspin(mEvt:MouseEvent){
spinning = true
} //end startspin function
var points:int = 0;
function stopspin (mEvt){
if (spinning){
spinning = false
var nextVal = newVal()
var nextVal2 = newVal2()
var addPoints = nextVal + nextVal2
points += nextVal + nextVal2
spinner_mc.gotoAndStop(nextVal)
spinner_mc.rotation = 0
spinner2_mc.gotoAndStop(nextVal2)
spinner2_mc.rotation = 0
value_txt.text = "point = " + points + ", new value " + addPoints
}
if ((points == 0) && (addPoints == 7) || (addPoints == 11))
{
info_txt.text = "You win!! You rolled a 7 or 11 on first throw!"
}
if ((points == 0) && (addPoints == 2) || (addPoints == 3) || (addPoints == 12))
{
info_txt.text ="You lose! You rolled a 2, 3 or 12 on first throw!"
}
} // end stopspin function
function newVal(){
var temp:int;
temp = Math.random() * 6 + 1;
return temp;
}
function newVal2(){
var temp:int
temp = Math.random() * 6 + 1;
return temp;
}
You need to check for points and addPoints before you add points to the player, while your code has check for points outside the if (spinning) block. This makes your checks go awry even if points is yet zero. So, you first get the spin values, then check, then assign values.
function stopspin(evt:MouseEvent):void {
if (!spinning) return; // this is shorter than wrapping the entire function
// body into a single "if (spinning)" block. If we don't spin, get out
spinning=false;
var nextVal1:int=newVal();
var nextVal2:int=newVal(); // no need of two functions doing the same :)
var addPoints:int=nextVal1+nextVal2;
spinner_mc.gotoAndStop(nextVal1);
spinner_mc.rotation = 0;
spinner2_mc.gotoAndStop(nextVal2);
spinner2_mc.rotation = 0; // give values to spinners
// now check phase
if (points==0) {
// this means first throw.
if ((addPoints==7) || (addPoints==11)) {
// player wins, insert win code
} else if((addPoints==2)||(addPoints==3)||(addPoints==12)) {
// player loses, insert lose code
} else {
points=addPoints; // otherwise making the player do second throw
// add other code, say inform the player that he has to throw again
}
} else {
// not a first throw. We have the "point" in "points" variable right now
if (addPoints==points) {
// player wins with second throw, add win code
points=0; // okay, back to first throw
} else if (addPoints==7) {
// player loses second throw, add relevant code
points=0; // again, back to first throw
} else {
// else no match, oh well.
}
}
}
Also, you should know better how to use event listeners if you want several objects to listen with a single function (in your case, spin). See, right now you have your spin function to update both spinners, and you have it being called twice per frame, so your spinners are spinning at twice the desired speed. You can get the actively listening movie clip using the Event class object passed into the listener, you need to get its target property. So, your listener can be rewritten like this:
function spin(evt:Event):void {
var mc:DisplayObject=evt.target; // the object that's listening
mc.rotation+=5; // now turn it.
}
And last, please place semicolons at the end of each line. Even if AS3 compiler does not strictly require this, it can save you from some really weird mistakes.