Skip to content

Instantly share code, notes, and snippets.

@sumlah
Created April 4, 2014 22:24
Show Gist options
  • Select an option

  • Save sumlah/9984272 to your computer and use it in GitHub Desktop.

Select an option

Save sumlah/9984272 to your computer and use it in GitHub Desktop.
Cash Cow Carnival - Food Tent
import flash.media.Sound;
import flash.media.SoundChannel;
import flash.net.URLRequest;
import flash.events.MouseEvent;
import flash.ui.Keyboard;
var randomFood:Number;
var buy:Boolean=false;
var beep:Sound=new Sound();
var error:Sound=new Sound();
var chaching:Sound=new Sound();
beep.load(new URLRequest("beep.mp3"));
error.load(new URLRequest("error.mp3"));
chaching.load(new URLRequest("cashregister.mp3"));
var speaker:SoundChannel = new SoundChannel();
var highest:Number=1.25;
var foodPrices:Array = new Array();
var totalCost:Number=0;
var foodCost:Number=0;
var randomChange:Number=0;
stop();
var level:String="menu";
lev1_btn.buttonMode=true;
lev2_btn.buttonMode=true;
lev3_btn.buttonMode=true;
lev1_btn.addEventListener(MouseEvent.CLICK, getLevel);
lev2_btn.addEventListener(MouseEvent.CLICK, getLevel);
lev3_btn.addEventListener(MouseEvent.CLICK, getLevel);
function getLevel(event:MouseEvent):void
{
switch (event.target.name){
case "lev1_btn":
level="level1";
break;
case "lev2_btn":
level="level2";
break;
case "lev3_btn":
level="level3";
break;
}
gotoAndStop(1,"levels");
}
function changeLevel(event:MouseEvent){
gotoAndStop(1,"Menu");
}
mm_mc.visible=false;
dothemath_mc.visible=false;
// randomize the food display and order
function pickTheFood():void {
// Hide all 12 food items
for(var f = 1; f < 13; f++){
this[ "food"+f].visible=false;
this["foodtxt"+f].visible=false;
}
var startFood:Number=Math.floor(Math.random() * (1+11-1)) + 1;
var startX=63;
var startXtxt=25;
var startY=45;
var startYtxt=95;
this["food"+startFood].x=startX;
this["food"+startFood].y=startY;
this["foodtxt"+startFood].x=startXtxt;
this["foodtxt"+startFood].y=startYtxt;
this["foodtxt"+startFood].visible=true;
this["food"+startFood].visible=true;
this["food"+startFood].buttonMode=true;
for (var ff=0;ff<8;ff++){
if (startFood>11){
startFood=0;
}
startFood++;
if (startX==403){
startX=-107;
startY+=135;
startXtxt=-150;
startYtxt+=132;
}
startX+=170;
startXtxt+=175;
this["food"+startFood].x=startX;
this["food"+startFood].y=startY;
this["foodtxt"+startFood].x=startXtxt;
this["foodtxt"+startFood].y=startYtxt;
this["food"+startFood].visible=true;
this["foodtxt"+startFood].visible=true;
}
}
pickTheFood();
import flash.events.MouseEvent;
mm_mc.visible=true;
mm_mc.buttonMode=true;
mm_mc.addEventListener(MouseEvent.CLICK, changeLevel);
broke_mc.visible=false;
instructions_txt.text="Hello";
if (level!=="level3"){
cashreg_txt.text="0.00";
} else {
cashreg_txt.text="";
dothemath_mc.visible=true;
}
broke_mc.addEventListener(MouseEvent.CLICK, prePlayAgainMouse);
broke_mc.buttonMode=true;
function getPocketChange():void{
randomChange = highest; // make a product of .25
while (randomChange <= highest){
randomChange = .25*Math.floor((Math.random()*(10+20))); // make a product of .25
var pocketChange = randomChange.toFixed(2); // put to two decimals
}
instructions_txt.text="You have $" + pocketChange + " in your pocket. Select the food items you want to buy. How much money do you have left over? Enter it below to check your answer!";
}
function checkAnswer():void{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, enterKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, prePlayAgainKey);
var comparedChange=randomChange.toFixed(2);
var SA = Number(studentAnswer.text);
var CC = Number(comparedChange);
if (SA==CC){
speaker=chaching.play();
broke_mc.visible=true;
broke_mc.alert_txt.text="Great job! You're right. You have $" + comparedChange + " left over!";
} else {
speaker=error.play();
broke_mc.visible=true;
broke_mc.alert_txt.text="Sorry, that's incorrect. You have $" + comparedChange + " left over. Try again.";
}
checkAnswer_mc.removeEventListener(MouseEvent.CLICK, preCheckAnswerMouse);
stage.removeEventListener(KeyboardEvent.KEY_DOWN, preCheckAnswerKey);
checkAnswer_mc.buttonMode=false;
removeEventListeners();
}
function enterKey(event:KeyboardEvent) : void {
if (event.keyCode == Keyboard.ENTER){
if (studentAnswer.text!==""){
if (buy==true){
checkAnswer();
}
}
}
}
function preCheckAnswerMouse(event:MouseEvent):void{
checkAnswer();
}
function preCheckAnswerKey(event:KeyboardEvent):void{
if (event.keyCode == Keyboard.ENTER){
checkAnswer();
}
}
function buyItem():void{
buy=true;
stage.addEventListener(KeyboardEvent.KEY_DOWN, preCheckAnswerKey);
checkAnswer_mc.addEventListener(MouseEvent.CLICK, preCheckAnswerMouse);
checkAnswer_mc.buttonMode=true;
totalCost=totalCost+foodCost;
if (level!=="level3"){
cashreg_txt.text=totalCost.toFixed(2);
}
randomChange-=foodCost;
if (randomChange < 0){
removeEventListeners();
broke_mc.visible=true;
broke_mc.addEventListener(MouseEvent.CLICK, prePlayAgainMouse);
stage.addEventListener(KeyboardEvent.KEY_DOWN, prePlayAgainKey);
broke_mc.alert_txt.text="You ran out of money! You owe $" + ((randomChange*-100)/100).toFixed(2) + " more than you have. Do not buy more food than you can afford! Hint: Look at the cash register to see how much you owe.";
speaker=error.play();
}
}
function addValue(event:MouseEvent):void{
stage.focus = studentAnswer;
speaker=beep.play();
switch(event.currentTarget.name){
case "food1":
foodCost=foodPrices[0];
food1.alpha=0.5;
food1.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food2":
foodCost=foodPrices[1];
food2.alpha=0.5;
food2.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food3":
foodCost=foodPrices[2];
food3.alpha=0.5;
food3.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food4":
foodCost=foodPrices[3];
food4.alpha=0.5;
food4.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food5":
foodCost=foodPrices[4];
food5.alpha=0.5;
food5.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food6":
foodCost=foodPrices[5];
food6.alpha=0.5;
food6.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food7":
foodCost=foodPrices[6];
food7.alpha=0.5;
food7.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food8":
foodCost=foodPrices[7];
food8.alpha=0.5;
food8.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food9":
foodCost=foodPrices[8];
food9.alpha=0.5;
food9.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food10":
foodCost=foodPrices[9];
food10.alpha=0.5;
food10.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food11":
foodCost=foodPrices[10];
food11.alpha=0.5;
food11.removeEventListener(MouseEvent.CLICK, addValue);
break;
case "food12":
foodCost=foodPrices[11];
food12.alpha=0.5;
food12.removeEventListener(MouseEvent.CLICK, addValue);
break;
}
buyItem();
}
function removeEventListeners():void{
for (var re=1;re<13;re++){
this[ "food"+re].removeEventListener(MouseEvent.CLICK, addValue);
this["food"+re].buttonMode=false;
}
checkAnswer_mc.removeEventListener(MouseEvent.CLICK,preCheckAnswerMouse);
checkAnswer_mc.buttonMode=false;
stage.removeEventListener(KeyboardEvent.KEY_DOWN,preCheckAnswerKey);
}
function randomCost():void {
for (var ae=1;ae<13;ae++){
this[ "food"+ae].addEventListener(MouseEvent.CLICK, addValue);
this["food"+ae].buttonMode=true;
}
for (var obj:Number=0;obj<12;obj++){
if (level=="level1"){
randomFood =.25*Math.floor((Math.random()*(5+8))); // make a random product of .25
} else if (level=="level2" || level=="level3"){
randomFood = Math.random() * (1+3.75-0.01); // for level 2
}
if (randomFood==0 || randomFood==0.00){ // dont let value be 0.00
obj-=1;
} else {
foodPrices.push(randomFood); // store random value in foodPrices array
}
if (randomFood>highest){
highest=randomFood;
}
}
// display the quantity in two decimal points for easy reading:
foodtxt1.text=foodPrices[0].toFixed(2);
foodtxt2.text=foodPrices[1].toFixed(2);
foodtxt3.text=foodPrices[2].toFixed(2);
foodtxt4.text=foodPrices[3].toFixed(2);
foodtxt5.text=foodPrices[4].toFixed(2);
foodtxt6.text=foodPrices[5].toFixed(2);
foodtxt7.text=foodPrices[6].toFixed(2);
foodtxt8.text=foodPrices[7].toFixed(2);
foodtxt9.text=foodPrices[8].toFixed(2);
foodtxt10.text=foodPrices[9].toFixed(2);
foodtxt11.text=foodPrices[10].toFixed(2);
foodtxt12.text=foodPrices[11].toFixed(2);
}
function prePlayAgainMouse(event:MouseEvent):void{
pickTheFood();
playAgain();
}
function prePlayAgainKey(event:KeyboardEvent):void{
pickTheFood();
if (event.keyCode == Keyboard.ENTER){
stage.removeEventListener(KeyboardEvent.KEY_DOWN, prePlayAgainKey);
playAgain();
}
}
function playAgain():void{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, prePlayAgainKey);
buy=false;
checkAnswer_mc.removeEventListener(MouseEvent.CLICK,preCheckAnswerMouse);
checkAnswer_mc.buttonMode=false;
highest=1.25;
studentAnswer.text="";
food1.alpha=1;
food2.alpha=1;
food3.alpha=1;
food4.alpha=1;
food5.alpha=1;
food6.alpha=1;
food7.alpha=1;
food8.alpha=1;
food9.alpha=1;
food10.alpha=1;
food11.alpha=1;
food12.alpha=1;
pickTheFood();
broke_mc.visible=false;
foodPrices = new Array();
totalCost=0;
foodCost=0;
if (level!=="level3"){
cashreg_txt.text="0.00";
} else {
cashreg_txt.text="";
dothemath_mc.visible=true;
}
randomCost();
getPocketChange();
}
randomCost();
getPocketChange(); // randomly generate amount of money student has in pocket to spend
stage.addEventListener(KeyboardEvent.KEY_DOWN, enterKey);
stop();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment