Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package;
- import openfl.display.Sprite;
- import openfl.Lib;
- import sys.db.Mysql;
- import openfl.text.TextField;
- import openfl.text.TextFormat;
- import openfl.text.TextFieldAutoSize;
- import openfl.events.MouseEvent;
- import openfl.events.Event;
- import openfl.events.TextEvent;
- /**
- * ...
- * @author Reka
- */
- class Game extends Sprite
- {
- private var myTextFormat:TextFormat;
- private var amGameOver:Bool = false;
- var tf:TextField = new TextField();
- // this keeps track of the score
- var score:Int = 0;
- // the textfield that displays the score
- var scoreDisplay:TextField;
- // the fields for question and options
- var questionDisplay:TextField;
- var answers:Array<TextField>;
- // the index of the correct answer (0, 1 or 2)
- var correctAnswer:Int;
- // display of the time
- var timerField:TextField;
- // the actual time left (starts at 10 sec. or 10000 milli sec)
- var timer:Int = 19000;
- // the last time the update loop ran (subtract the current time from this number to get the time passed)
- var lastTime:Int;
- // Clean previous screen and setup the current screen
- public function new()
- {
- super();
- removeChildren(); // Clean previous screen
- setup();
- start();
- }
- // Setup the current screen
- public function setup()
- {
- // Setup a default text format
- myTextFormat = new TextFormat("_sans", 14, 0xAA2023, true);
- // Add the neccessary elements
- addScoreField();
- addTimerField();
- addQuestion();
- addAnswers();
- // Start the game
- start();
- }
- // Start the game from the start
- public function start()
- {
- // Reset Values and text
- amGameOver = false;
- score = 0;
- scoreDisplay.text = "SCORE: " + Std.string(score);
- Database.resetQuestionID();
- updateText();
- // Activate the update loop
- addEventListener(Event.ENTER_FRAME, update);
- }
- // Adds the Player Input field
- function addPlayerName()
- {
- var tformat = new openfl.text.TextFormat( "_sans", 24, 0xAA2023, true );
- tf = new TextField();
- tf.border = true;
- tf.defaultTextFormat = tformat;
- tf.text = "Type your nickname here";
- tf.type = openfl.text.TextFieldType.INPUT;
- tf.width = 400;
- tf.height = 50;
- tf.x = 10;
- tf.y = 10;
- addChild( tf );
- }
- // Adds the Score textfield
- function addScoreField()
- {
- scoreDisplay = new TextField();
- scoreDisplay.autoSize = TextFieldAutoSize.LEFT;
- scoreDisplay.defaultTextFormat = myTextFormat;
- scoreDisplay.selectable = false;
- scoreDisplay.text = "SCORE: " + Std.string(score);
- scoreDisplay.x = 10;
- scoreDisplay.y = 60;
- addChild( scoreDisplay );
- }
- // Adds the timer textfield
- function addTimerField()
- {
- timerField = new TextField();
- timerField.defaultTextFormat = myTextFormat;
- timerField.width = 300;
- timerField.x = scoreDisplay.x + 100;
- timerField.y = scoreDisplay.y;
- addChild( timerField );
- }
- // Adds the Question textfield
- function addQuestion()
- {
- questionDisplay = new TextField();
- questionDisplay.x = 10;
- questionDisplay.y = 100;
- questionDisplay.defaultTextFormat = myTextFormat;
- questionDisplay.autoSize = TextFieldAutoSize.LEFT;
- addChild(questionDisplay);
- }
- // Adds the 4 Answer textfields
- function addAnswers()
- {
- answers = new Array<TextField>();
- for (i in 0...3)
- {
- var answerText:TextField = new TextField();
- answerText.autoSize = TextFieldAutoSize.LEFT;
- answerText.defaultTextFormat = myTextFormat;
- answerText.x = questionDisplay.x;
- answerText.y = questionDisplay.y + questionDisplay.height + (i * 50);
- addChild( answerText );
- answerText.addEventListener( MouseEvent.CLICK, answerQuestion);
- // Add the answer textfield to the array list
- answers.push(answerText);
- }
- }
- // Update loop gets called every frame
- function update(e:Event):Void
- {
- // Countdown until 0
- if (timer > 0)
- {
- // Subtract 1 of timer every frame
- var now:Int = Lib.getTimer();
- var deltaTime:Int = now - lastTime;
- lastTime = now;
- timer -= deltaTime;
- timerField.text = Std.string(timer);
- }
- else
- {
- // Stop the update and show game over screen
- gameOver();
- }
- }
- // Stops the update loop and show game over screen
- function gameOver()
- {
- // Stop the update loop and activate Game Over mode
- removeEventListener(Event.ENTER_FRAME, update);
- amGameOver = true;
- // Hide questions and answers
- questionDisplay.text = "";
- for (row in answers)
- {
- row.text = "";
- }
- // Show message
- timerField.text = "GAME OVER! - Click here to restart the game";
- timerField.addEventListener( MouseEvent.CLICK, restart);
- }
- function gameWin()
- {
- // Stop the update loop and activate Game Over mode
- removeEventListener(Event.ENTER_FRAME, update);
- amGameOver = true;
- // Hide questions and answers
- questionDisplay.text = "";
- for (row in answers)
- {
- row.text = "";
- }
- // Show message
- addPlayerName();
- timerField.text = "Submit Score!";
- timerField.addEventListener( MouseEvent.CLICK, submitScore);
- }
- // Cause the game to restart
- function restart(event:MouseEvent)
- {
- start();
- timerField.removeEventListener( MouseEvent.CLICK, restart);
- }
- // Submit score to leaderboard and switch to hall of fame
- function submitScore(event:MouseEvent)
- {
- // Clean up current scene and database
- removeEventListener(Event.ENTER_FRAME, update);
- removeChildren();
- // Submit score
- Database.PostScore(tf.text, score);
- // Change screens
- var changeScreen = new HallofFame();
- addChild(changeScreen);
- }
- // Update the question and answer texts
- function updateText()
- {
- // Reset the timer
- lastTime = Lib.getTimer();
- timer = 19000;
- // Get the answers from the database
- var myAnswers = Database.loadQuestion();
- if (myAnswers.question == "") // Reached an ending, activate GameWin sequence
- {
- if (score > 0)
- {
- gameWin();
- }
- else
- {
- gameOver();
- }
- }
- else
- {
- // Update the question and answer texts
- questionDisplay.text = myAnswers.question;
- if (answers[0] != null){
- answers[0].text = myAnswers.answer1;
- answers[1].text = myAnswers.answer2;
- answers[2].text = myAnswers.answer3;
- correctAnswer = myAnswers.correctAnswer;
- }
- }
- }
- // When one of the answer option textfields is clicked. Index of returns the position of the clicked answer in the array
- public function answerQuestion(e:MouseEvent):Void
- {
- if (amGameOver == false)
- {
- // Retrieve the clicked answer index
- var fieldIndex:Int = answers.indexOf( cast e.currentTarget );
- // Add score if answered correctly
- if (fieldIndex == correctAnswer)
- {
- score += 100;
- }
- else
- {
- score -= 10;
- }
- scoreDisplay.text = "SCORE: " + Std.string(score);
- // Submit answer to the Database and retrieve the new question and answers
- Database.checkAnswer(fieldIndex + 1);
- updateText();
- }
- }
- }
Add Comment
Please, Sign In to add comment