Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8630479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:04:26+00:00 2026-06-12T09:04:26+00:00

I’ve been following a tutorial over the web but it somehow did not show

  • 0

I’ve been following a tutorial over the web but it somehow did not show something about creating a game over function. I am new to the Starling framework and Actionscript so I’m kind of still trying to find a way to make it work. Here’s the complete snippet of the code.

package screens

{


import flash.geom.Rectangle;
import flash.utils.getTimer;
import events.NavigationEvent;

import objects.GameBackground;
import objects.Hero;
import objects.Item;
import objects.Obstacle;

import starling.display.Button;
import starling.display.Image;
import starling.display.Sprite;
import starling.events.Event;
import starling.events.Touch;
import starling.events.TouchEvent;
import starling.text.TextField;
import starling.utils.deg2rad;

public class InGame extends Sprite
{
    private var screenInGame:InGame;
    private var screenWelcome:Welcome;
    private var startButton:Button;
    private var playAgain:Button;
    private var bg:GameBackground;
    private var hero:Hero;

    private var timePrevious:Number;
    private var timeCurrent:Number;
    private var elapsed:Number;

    private var gameState:String;

    private var playerSpeed:Number = 0;
    private var hitObstacle:Number = 0;
    private const MIN_SPEED:Number = 650;

    private var scoreDistance:int;
    private var obstacleGapCount:int;

    private var gameArea:Rectangle;

    private var touch:Touch;
    private var touchX:Number;
    private var touchY:Number;

    private var obstaclesToAnimate:Vector.<Obstacle>;
    private var itemsToAnimate:Vector.<Item>;

    private var scoreText:TextField;
    private var remainingLives:TextField;
    private var gameOverText:TextField;
    private var iconSmall:Image;

    static private var lives:Number = 2;

    public function InGame()
    {
        super();
        this.addEventListener(starling.events.Event.ADDED_TO_STAGE, onAddedToStage);
    }

    private function onAddedToStage(event:Event):void {
        this.removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage); 
        drawGame();

        scoreText = new TextField(300, 100, "Score: 0", "MyFontName", 35, 0xD9D919, true);
        remainingLives = new TextField(600, 100, "Lives: " + lives +" X ", "MyFontName", 35, 0xD9D919, true);
        iconSmall = new Image(Assets.getAtlas().getTexture("darnahead1"));
        iconSmall.x = 360;
        iconSmall.y = 40;
        this.addChild(iconSmall);

        this.addChild(scoreText);
        this.addChild(remainingLives);


    }

    private function drawGame():void {
        bg = new GameBackground();

        this.addChild(bg);
        hero = new Hero();
        hero.x = stage.stageHeight / 2;
        hero.y = stage.stageWidth / 2;
        this.addChild(hero);

        startButton = new Button(Assets.getAtlas().getTexture("startButton"));
        startButton.x = stage.stageWidth * 0.5 - startButton.width * 0.5;
        startButton.y = stage.stageHeight * 0.5 - startButton.height * 0.5; 

        this.addChild(startButton);

        gameArea = new Rectangle(0, 100, stage.stageWidth, stage.stageHeight - 250);        
    }

    public function disposeTemporarily():void {
        this.visible = false;
    }

    public function initialize():void {
        this.visible = true;

        this.addEventListener(Event.ENTER_FRAME, checkElapsed);

        hero.x = -stage.stageWidth;
        hero.y = stage.stageHeight * 0.5;

        gameState ="idle";

        playerSpeed = 0;
        hitObstacle = 0;
        bg.speed = 0;
        scoreDistance = 0;
        obstacleGapCount = 0;

        obstaclesToAnimate = new Vector.<Obstacle>();
        itemsToAnimate = new Vector.<Item>();

        startButton.addEventListener(Event.TRIGGERED, onStartButtonClick);
        //var mainStage:InGame =InGame.current.nativeStage;
        //mainStage.dispatchEvent(new Event(Event.COMPLETE));
        //playAgain.addEventListener(Event.TRIGGERED, onRetry);
    }

    private function onStartButtonClick(event:Event):void {
        startButton.visible = false;

        startButton.removeEventListener(Event.TRIGGERED, onStartButtonClick);
        launchHero();
    }

    private function launchHero():void {
        this.addEventListener(TouchEvent.TOUCH, onTouch);
        this.addEventListener(Event.ENTER_FRAME, onGameTick);
    }

    private function onTouch(event:TouchEvent):void {
        touch = event.getTouch(stage);

        touchX = touch.globalX;
        touchY = touch.globalY;
    }

    private function onGameTick(event:Event):void {
        switch(gameState) {
            case "idle":
                if(hero.x < stage.stageWidth * 0.5 * 0.5) {
                    hero.x += ((stage.stageWidth * 0.5 * 0.5 + 10) - hero.x) * 0.05;
                    hero.y = stage.stageHeight * 0.5;

                    playerSpeed += (MIN_SPEED - playerSpeed) * 0.05;
                    bg.speed = playerSpeed * elapsed;
                } else {
                    gameState = "flying";

                }
                break;
            case "flying":
                if(hitObstacle <= 0) {
                    hero.y -= (hero.y - touchY) * 0.1;

                    if(-(hero.y - touchY) < 150 && -(hero.y - touchY) > -150) {
                        hero.rotation = deg2rad(-(hero.y - touchY) * 0.2);
                    }

                    if(hero.y > gameArea.bottom - hero.height * 0.5) {
                        hero.y = gameArea.bottom - hero.height * 0.5;
                        hero.rotation = deg2rad(0);
                    }

                    if(hero.y < gameArea.top + hero.height * 0.5) {
                        hero.y = gameArea.top + hero.height * 0.5;
                        hero.rotation = deg2rad(0);
                    }
                } else {    
                    hitObstacle--
                        cameraShake();
                }

                playerSpeed -= (playerSpeed - MIN_SPEED) * 0.01;
                bg.speed = playerSpeed * elapsed;

                scoreDistance += (playerSpeed * elapsed) * 0.1;
                scoreText.text = "Score: " + scoreDistance;

                initObstacle();
                animateObstacles();

                createEggItems();
                animateItems();

                remainingLives.text = "Lives: "+lives + " X ";

                if(lives == 0) {
                    gameState = "over";
                }

                break;
            case "over":
                gameOver();
                break;

        }
    }

    private function gameOver():void {

        gameOverText = new TextField(800, 400, "Hero WAS KILLED!!!", "MyFontName", 50, 0xD9D919, true);
        scoreText = new TextField(800, 600, "Score: "+scoreDistance, "MyFontName", 30, 0xFFFFFF, true);
        this.addChild(scoreText);

        this.addChild(gameOverText);

        playAgain = new Button(Assets.getAtlas().getTexture("button_tryAgain"));
        playAgain.x = stage.stageWidth * 0.5 - startButton.width * 0.5;
        playAgain.y = stage.stageHeight * 0.75 - startButton.height * 0.75; 

        this.addChild(playAgain);
        playAgain.addEventListener(Event.TRIGGERED, onRetry);

    }

    private function onRetry(event:Event):void {

        playAgain.visible = false;
        gameOverText.visible = false;
        scoreText.visible = false;

        var btnClicked:Button = event.target as Button;

        if((btnClicked as Button) == playAgain) {

            this.dispatchEvent(new NavigationEvent(NavigationEvent.CHANGE_SCREEN, {id: "playnow"}, true));
        } 

        disposeTemporarily();
    }

    private function animateItems():void {
        var itemToTrack:Item;

        for(var i:uint = 0; i < itemsToAnimate.length; i++) {
            itemToTrack = itemsToAnimate[i];

            itemToTrack.x -= playerSpeed * elapsed;

            if(itemToTrack.bounds.intersects(hero.bounds)) {
                itemsToAnimate.splice(i, 1);
                this.removeChild(itemToTrack);
            }

            if(itemToTrack.x < -50) {
                itemsToAnimate.splice(i, 1);
                this.removeChild(itemToTrack);
            }
        }
    }

    private function createEggItems():void {
        if(Math.random() > 0.95){
            var itemToTrack:Item = new Item(Math.ceil(Math.random() * 10));
            itemToTrack.x = stage.stageWidth + 50;
            itemToTrack.y = int(Math.random() * (gameArea.bottom - gameArea.top)) + gameArea.top;
            this.addChild(itemToTrack);

            itemsToAnimate.push(itemToTrack);
        }
    }

    private function cameraShake():void {

        if(hitObstacle > 0) {
            this.x = Math.random() * hitObstacle;
            this.y = Math.random() * hitObstacle;
        } else if(x != 0) {
            this.x = 0;
            this.y = 0;
            lives--;
        }
    }

    private function initObstacle():void {
        if(obstacleGapCount < 1200) {
            obstacleGapCount += playerSpeed * elapsed;
        } else if(obstacleGapCount !=0) {
            obstacleGapCount = 0;
            createObstacle(Math.ceil(Math.random() * 5), Math.random() * 1000 + 1000);
        }
    }

    private function animateObstacles():void {
        var obstacleToTrack:Obstacle;

        for(var i:uint = 0; i<obstaclesToAnimate.length; i++) {
            obstacleToTrack = obstaclesToAnimate[i];

            if(obstacleToTrack.alreadyHit == false && obstacleToTrack.bounds.intersects(hero.bounds)) {
                obstacleToTrack.alreadyHit = true;
                obstacleToTrack.rotation = deg2rad(70);

                hitObstacle = 30;
                playerSpeed *= 0.5;
            }

            if(obstacleToTrack.distance > 0) {
                obstacleToTrack.distance -= playerSpeed * elapsed;
            } else {
                if(obstacleToTrack.watchOut) {
                    obstacleToTrack.watchOut = false;
                } 

                obstacleToTrack.x -= (playerSpeed + obstacleToTrack.speed) * elapsed;
            }

            if(obstacleToTrack.x < -obstacleToTrack.width || gameState == "over") {
                obstaclesToAnimate.splice(i, 1);
                this.removeChild(obstacleToTrack);
            }
        }
    }

    private function checkElapsed(event:Event):void {
        timePrevious = timeCurrent;
        timeCurrent = getTimer();

        elapsed = (timeCurrent - timePrevious) * 0.001;
    }

    private function createObstacle(type:Number, distance:Number):void{
        var obstacle:Obstacle = new Obstacle(type, distance, true, 300);
        obstacle.x = stage.stageWidth;

        this.addChild(obstacle);

        if(type >= 4) {
            if(Math.random() > 0.5) {
                obstacle.y = gameArea.top;
                obstacle.position = "top"
            } else {
                obstacle.y = gameArea.bottom - obstacle.height;
                obstacle.position = "bottom";
            }
        } else {
            obstacle.y = int(Math.random() * (gameArea.bottom - obstacle.height - gameArea.top)) + gameArea.top;
            obstacle.position = "middle";
        }

        obstaclesToAnimate.push(obstacle);
    }

}

}

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T09:04:27+00:00Added an answer on June 12, 2026 at 9:04 am

    You’re not calling initialize() anywhere, which is where the gameState is initially set to “idle” it seems… what does this code do currently when you run it?

    The goal here is to get the onGameTick(event) function running every frame, which is going to switch between idle/flying/over game states.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Seemingly simple, but I cannot find anything relevant on the web. What is the
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
This could be a duplicate question, but I have no idea what search terms

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.