Try to coin toss;)
Full Sources.



package
{
import flash.display.*;
import flash.events.*;
import flash.display.MovieClip;
import fl.controls.RadioButtonGroup;
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.ui.Mouse;
import flash.display.Sprite;
import flash.utils.*;
import flash.media.Sound;
import flash.media.SoundChannel;
public class CoinToss extends MovieClip
{
var radioGroup1:RadioButtonGroup=new RadioButtonGroup("Coin Loss");
var randomNumber:Number;
var points:Number = 500;
// set up sounds
var theCorrectSound:CorrectSound = new CorrectSound();
var theWrongSound:WrongSound = new WrongSound();
//constructor
public function CoinToss()
{
//changing existing cursor to our custom
Mouse.hide();
addEventListener(Event.ENTER_FRAME, cursorPosition);
my_cursor.mouseEnabled = false;
gameMenu();
}
function cursorPosition(event:Event)
{
my_cursor.x = mouseX;
my_cursor.y = mouseY;
}
//******************************************************************************
//main game menu
public function gameMenu():void
{
gotoAndStop('homeScreen');
trace("************ We are on HOME Screen ********************");
homeBtn.addEventListener(MouseEvent.CLICK, homeScreen);
playGameBtn.addEventListener(MouseEvent.CLICK, gameScreen);
howToPlayBtn.addEventListener(MouseEvent.CLICK, howToPlayScreen);
moreGamesBtn.addEventListener(MouseEvent.CLICK, moreGamesScreen);
thanksBtn.addEventListener(MouseEvent.CLICK, thanksScreen);
}
public function homeScreen(event:MouseEvent):void
{
gotoAndStop('homeScreen');
trace("************ We are on HOME Screen ********************");
var homeScreen:Tween = new Tween(homeScreen_mc, "x", Elastic.easeOut, -200, 10, 2, true);
}
public function gameScreen(event:MouseEvent):void
{
startGame();
}
public function howToPlayScreen(event:MouseEvent):void
{
gotoAndStop('howToPlayScreen');
trace("************ We are on HOW TO PLAY Screen ********************");
var howToPlayScreen:Tween = new Tween(howToPlayScreen_mc, "x", Elastic.easeOut, -200, 10, 2, true);
}
public function moreGamesScreen(event:MouseEvent):void
{
gotoAndStop('moreGamesScreen');
trace("************ We are on MORE GAMES Screen ********************");
var moreGamesScreen:Tween = new Tween(moreGamesScreen_mc, "x", Elastic.easeOut, -200, 10, 2, true);
}
public function thanksScreen(event:MouseEvent):void
{
gotoAndStop('thanksScreen');
trace("************ We are on THANKS Screen ********************");
var thanksScreen:Tween = new Tween(thanksScreen_mc, "x", Elastic.easeOut, -200, 10, 2, true);
}
//******************************************************************************
public function chooseAnswersGroup():void
{
gameScreen_mc.eagle_rb.group = radioGroup1;
gameScreen_mc.tails_rb.group = radioGroup1;
}
function startGame():void
{
stage.stageFocusRect = false;
gotoAndStop('gameScreen');
trace("************ We are on GAME Screen ********************");
var gameScreen:Tween = new Tween(gameScreen_mc, "x", Elastic.easeOut, -200, 10, 2, true);
trace(">> game starting...");
chooseAnswersGroup();
gameScreen_mc.eagle_rb.selected = true;
gameScreen_mc.eagle_rb.visible=true;
gameScreen_mc.tails_rb.visible=true;
gameScreen_mc.coinAnimToss_mc.visible = false;
gameScreen_mc.coin_mc.visible = false;
gameScreen_mc.coin_first.visible = true;
gameScreen_mc.points_txt.text="500";
gameScreen_mc.eagle_rb.label="Eagle";
gameScreen_mc.tails_rb.label="Tails";
gameScreen_mc.eagle_correct_mc.visible = false;
gameScreen_mc.eagle_wrong_mc.visible = false;
gameScreen_mc.tails_correct_mc.visible = false;
gameScreen_mc.tails_wrong_mc.visible = false;
gameScreen_mc.coinToss_btn.addEventListener(MouseEvent.CLICK, delayCheckAnswer);
points = 500;
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = true;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
public function delayCheckAnswer(event:MouseEvent):void
{
trace("delay going.....wait 1 sec");
setTimeout(checkAnswer, 1000);
gameScreen_mc.coin_mc.visible = false;
gameScreen_mc.coin_first.visible = false;
gameScreen_mc.coinAnimToss_mc.visible = true;
gameScreen_mc.coinAnimToss_mc.gotoAndPlay('playTossCoinAnimation');
}
function checkAnswer():void
{
gameScreen_mc.coin_mc.visible = true;
gameScreen_mc.coinAnimToss_mc.visible = false;
gameScreen_mc.coin_first.visible = false;
randomNumber=Math.random();//generate random number
trace("Random number: " + randomNumber);
if (randomNumber < 0.5)
{
gameScreen_mc.coin_mc.gotoAndPlay("eagle");
}
else
{
gameScreen_mc.coin_mc.gotoAndPlay("tails");
}
if (radioGroup1.selection.label == "Eagle" && randomNumber < 0.5)
{
gameScreen_mc.eagle_correct_mc.visible = true;//eagle pic correct - visible
gameScreen_mc.eagle_correct_mc.gotoAndPlay('correctPic');//plaing correct or wrong pic animation
gameScreen_mc.eagle_wrong_mc.visible = false;//eagle pic wrong - invisible
gameScreen_mc.tails_correct_mc.visible = false;//tails pic correct - invisible
gameScreen_mc.tails_wrong_mc.visible = false;//tails pic correct - invisible
points+=100;//added or deleted 100 points, depends of user guessed or not
gameScreen_mc.points_txt.text = String(points);//changing type(from number to string) and showing user points
gameScreen_mc.add100_mc.gotoAndPlay("add100");//animation for 'added +100 points' or 'deleted -100 points'
playSound(theCorrectSound);//play sound
trace("Eagle - correct");
trace("Player points: " + points);
trace("+++++++++++++++++++++++++++++++++++++++++++++++++");
}
else if (radioGroup1.selection.label == "Eagle" && randomNumber > 0.5)
{
gameScreen_mc.eagle_correct_mc.visible = false;
gameScreen_mc.eagle_wrong_mc.visible = true;
gameScreen_mc.eagle_wrong_mc.gotoAndPlay(’wrongPic’);
gameScreen_mc.tails_correct_mc.visible = false;
gameScreen_mc.tails_wrong_mc.visible = false;
points-=100;
gameScreen_mc.points_txt.text = String(points);
gameScreen_mc.del100_mc.gotoAndPlay(”del100″);
playSound(theWrongSound);
trace(”Eagle - wrong”);
trace(”Player points: ” + points);
trace(”+++++++++++++++++++++++++++++++++++++++++++++++++”);
}
else if (radioGroup1.selection.label == “Tails” && randomNumber > 0.5)
{
gameScreen_mc.eagle_correct_mc.visible = false;
gameScreen_mc.eagle_wrong_mc.visible = false;
gameScreen_mc.tails_correct_mc.visible = true;
gameScreen_mc.tails_correct_mc.gotoAndPlay(’correctPic’);
gameScreen_mc.tails_wrong_mc.visible = false;
points+=100;
gameScreen_mc.points_txt.text=String(points);
gameScreen_mc.add100_mc.gotoAndPlay(”add100″);
playSound(theCorrectSound);
trace(”Tails - correct”);
trace(”Player points: ” + points);
trace(”+++++++++++++++++++++++++++++++++++++++++++++++++”);
}
else
{
gameScreen_mc.eagle_correct_mc.visible = false;
gameScreen_mc.eagle_wrong_mc.visible = false;
gameScreen_mc.tails_correct_mc.visible = false;
gameScreen_mc.tails_wrong_mc.visible = true;
gameScreen_mc.tails_wrong_mc.gotoAndPlay(’wrongPic’);
points-=100;
gameScreen_mc.points_txt.text = String(points);
gameScreen_mc.del100_mc.gotoAndPlay(”del100″);
playSound(theWrongSound);
trace(”Tails - wrong”);
trace(”Player points: ” + points);
trace(”+++++++++++++++++++++++++++++++++++++++++++++++++”);
}
if (points == 100)
{
gameScreen_mc.earn100.visible = true;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 200)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = true;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 300)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = true;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 400)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = true;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 500)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = true;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 600)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = true;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 700)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = true;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 800)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = true;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = false;
}
if (points == 900)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = true;
gameScreen_mc.earn1000.visible = false;
}
if (points == 1000)
{
gameScreen_mc.earn100.visible = false;
gameScreen_mc.earn200.visible = false;
gameScreen_mc.earn300.visible = false;
gameScreen_mc.earn400.visible = false;
gameScreen_mc.earn500.visible = false;
gameScreen_mc.earn600.visible = false;
gameScreen_mc.earn700.visible = false;
gameScreen_mc.earn800.visible = false;
gameScreen_mc.earn900.visible = false;
gameScreen_mc.earn1000.visible = true;
}
if (points == 1000 || points == 0)
{
gameResult();
}
}
function gameResult():void
{
if (points == 1000)
{
gotoAndStop(’finalScreen’);
trace(”************ We are on FINAL Screen ********************”);
finalScreen_mc.message_txt.text = “You win 1000 points!”;
trace(”RESULT: win 1000″);
}
else if (points == 0)
{
gotoAndStop(’finalScreen’);
trace(”************ We are on FINAL Screen ********************”);
finalScreen_mc.message_txt.text = “You are lost your points!”;
trace(”RESULT: lost”);
}
finalScreen_mc.playAgain_btn.addEventListener(MouseEvent.CLICK, playAgain);
finalScreen_mc.homeBtn.addEventListener(MouseEvent.CLICK, goToHomeScreen);
}
function playAgain(event:MouseEvent):void
{
startGame();
}
function goToHomeScreen(event:MouseEvent):void
{
gameMenu();
}
public function playSound(soundObject:Object) {
var soundChannel:SoundChannel = soundObject.play();
}
}
}
Source:
Coin Toss
