-1

Hello I am trying to learn Actionscript for the first time. I am trying to make a game like Flappy Bird. Now my game works fine without a start menu. But now I am unable to start game after putting the start button. I am getting an error:

1046: Type was not found or was not a compile-time constant: BtnPlay.

But I have created an instance of the button named 'BtnPlay' and is also linked to Actionscript.

This is my TimeLine enter image description here

I am using external script to control the game.

What I want is-

  1. Start the game after I click the start button.
  2. Hide the button after click.
  3. At the end of the game show the button again and hide the game character(bird).

My actionscript is also given below.

    package{
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event

public class Main extends MovieClip{

    //constants
    const gravity:Number = 1.5;            //gravity of the game
    const dist_btw_obstacles:Number = 300; //distance between two obstacles
    const ob_speed:Number = 8;             //speed of the obstacle
    const jump_force:Number = 15;          //force with which it jumps

    //variables
    var player:Player = new Player();      
    var lastob:Obstacle = new Obstacle();  //varible to store the last obstacle in the obstacle array
    var obstacles:Array = new Array();     //an array to store all the obstacles
    var yspeed:Number = 0;                 //A variable representing the vertical speed of the bird
    var score:Number = 0;                  //A variable representing the score

    public function Main(){
        init();
    }

    function init():void {
        //initialize all the variables
        player = new Player();
        lastob = new Obstacle();
        obstacles = new Array();
        yspeed = 0;
        score = 0;

        //add player to center of the stage the stage
        player.x = stage.stageWidth/2;
        player.y = stage.stageHeight/2;
        addChild(player);

        //create 3 obstacles ()
        createObstacle();
        createObstacle();
        createObstacle();

        //Add EnterFrame EventListeners (which is called every frame) and KeyBoard EventListeners
        addEventListener(Event.ENTER_FRAME,onEnterFrameHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
    }

    private function key_up(event:KeyboardEvent){
        if(event.keyCode == Keyboard.SPACE){
            //If space is pressed then make the bird
            yspeed = -jump_force;
        }
    }

    function restart(){
        if(contains(player))
            removeChild(player);
            for(var i:int = 0; i < obstacles.length; ++i){
                if(contains(obstacles[i]) && obstacles[i] != null)
                removeChild(obstacles[i]);
                obstacles[i] = null;
            }
            obstacles.slice(0);
            init();
    }

    function onEnterFrameHandler(event:Event){
        //update player
        yspeed += gravity;
        player.y += yspeed;

        //restart if the player touches the ground
        if(player.y + player.height/2 > stage.stageHeight){
            restart();
        }

        //Don't allow the bird to go above the screen
        if(player.y - player.height/2 < 0){
            player.y = player.height/2;
        }

        //update obstacles
        for(var i:int = 0;i<obstacles.length;++i){
            updateObstacle(i);
        }

        //display the score
        scoretxt.text = String(score);
    }

    //This functions update the obstacle
    function updateObstacle(i:int){
        var ob:Obstacle = obstacles[i];

        if(ob == null)
        return;
        ob.x -= ob_speed;

        if(ob.x < -ob.width){
            //if an obstacle reaches left of the stage then change its position to the back of the last obstacle
            changeObstacle(ob);
        }

        //If the bird hits an obstacle then restart the game
        if(ob.hitTestPoint(player.x + player.width/2,player.y + player.height/2,true)
           || ob.hitTestPoint(player.x + player.width/2,player.y - player.height/2,true)
           || ob.hitTestPoint(player.x - player.width/2,player.y + player.height/2,true)
           || ob.hitTestPoint(player.x - player.width/2,player.y - player.height/2,true)){
            restart();
        }

        //If the bird got through the obstacle without hitting it then increase the score
        if((player.x - player.width/2 > ob.x + ob.width/2) && !ob.covered){
            ++score;
            ob.covered = true;
        }
    }

    //This function changes the position of the obstacle such that it will be the last obstacle and it also randomizes its y position
    function changeObstacle(ob:Obstacle){
        ob.x = lastob.x + dist_btw_obstacles;
        ob.y = 100+Math.random()*(stage.stageHeight-200);
        lastob = ob;
        ob.covered = false;
    }

    //this function creates an obstacle
    function createObstacle(){
        var ob:Obstacle = new Obstacle();
        if(lastob.x == 0)
        ob.x = 800;
        else
        ob.x = lastob.x + dist_btw_obstacles;
        ob.y = 100+Math.random()*(stage.stageHeight-200);
        addChild(ob);
        obstacles.push(ob);
        lastob = ob;
    }


}

}

I apologize if there is any mistake. I am totally new to actionscript. Any help is appreciated. Thank you

ratul keot
  • 29
  • 1
  • 12
  • Maybe this helps: https://stackoverflow.com/questions/6047701/how-to-hide-a-button-after-clicking-another-button-in-flash – Ruud Helderman Aug 23 '17 at 13:29
  • 2
    Just curious: why teach yourself [outdated](https://blogs.adobe.com/conversations/2017/07/adobe-flash-update.html) technology? – Ruud Helderman Aug 23 '17 at 13:36
  • 2
    @RuudHelderman your link is talking about a browser plugin. He's not learning to code for a plugin, rather he's using the AS3 language which can be compiled to native/mobile apps too. As long as it's not needed for browser after December 2020 he's fine. – VC.One Aug 23 '17 at 14:31
  • 1
    @ratulkeot I'm not seeing any `BtnPlay` in your code that could cause such error. Also keep everything on one frame (can be multiple layers). Use commands like `addChild(someMC)` or `someMC.visible = true;` instead of jumping frames (avoids issues like a var created in one frame not meaning anything to a different frame, since each frame is like a new blank page) – VC.One Aug 23 '17 at 14:44
  • 1
    @RuudHelderman Just curious: Why not do [research](https://forums.adobe.com/thread/2362234) before commenting on questions? – sfxworks Aug 23 '17 at 15:06
  • @VC.One My bad; the 'flash' tag fooled me. – Ruud Helderman Aug 23 '17 at 15:59
  • 1
    You posted all code exept code you need to post (how and where you created an instance of the button BtnPlay), wow. – User 987 Aug 23 '17 at 19:31
  • Sorry about the late reply. `var BtnPlay:BtnPlay = new BtnPlay();` Initialised with other variable And inside the init function `BtnPlay.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler); function mouseUpHandler(evt:MouseEvent):void { gotoAndPlay(2); BtnPlay.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler); }` – ratul keot Aug 24 '17 at 04:58
  • I apologize again for any mistake done. As I have said earlier I am new to it and just trying to learn. Thank you – ratul keot Aug 24 '17 at 05:02
  • Is function `mouseUpHandler` also inside that same `init` function? – VC.One Aug 24 '17 at 05:38
  • @ VC.One Thank you for the tip. I followed what you said, I initialized the `BtnPlay` inside the init function and moved the `createObtacle()` and `addChild(player)` inside the click function. The button is working now. All that is left to do is remove the button `BtnPlay` after it is clicked. Thank you everyone for the tips and advice. – ratul keot Aug 24 '17 at 06:06
  • @VC.One `playbtn = new BtnPlay(); player.x = stage.stageWidth/2; player.y = stage.stageHeight/2; addChild(playbtn); playbtn.x = 385; playbtn.y = 220; playbtn.enabled = true; playbtn.addEventListener(MouseEvent.MOUSE_UP,mouseUpHandler); function mouseUpHandler(evt:MouseEvent):void { gotoAndPlay(2); playbtn.removeEventListener(MouseEvent.MOUSE_UP,mouseUpHandler); addChild(player); createObstacle(); createObstacle(); createObstacle() } ` This is what I added inside `init()` – ratul keot Aug 24 '17 at 06:11

1 Answers1

1

Look at this example setup (see it helps you) :

package{

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event; //used for ENTER_FRAME event

public class Main extends MovieClip{

    //variables

    var btnPlay:BtnPlay = new BtnPlay(); //Var name must be different from Class name

    //These don't need "new" until when used (initialized)
    var player:Player; var lastob:Obstacle; var obstacles:Array;


    public function Main()
    {
        if (stage) { init(); } //# Check if Stage is ready
        else { addEventListener(Event.ADDED_TO_STAGE, init); }

    }

    public function init():void 
    {
        btnPlay.x = stage.stageWidth/4; //testing position
        btnPlay.y = stage.stageHeight/4; //testing position
        addChild(btnPlay); //now add button to Stage
        btnPlay.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler)‌​;
    }

    public function mouseUpHandler(evt:MouseEvent):void 
    {
        btnPlay.removeEventListener(MouseEvent.MOUSE_UP, mouseUpHandler)‌​;
        btnPlay.visible = false; //hide button from system
        initGame(); //add characters etc

        ////# Use "MovieClip(this.root)." to control existing Stage items that were not added by code
        //gotoAndPlay(2); //why go to frame 2 now?
        MovieClip(this.root).gotoAndStop(2); //or try: MovieClip(root).gotoAndStop(2);

    }

    function initGame():void 
    {
        //initialize all the variables
        player = new Player();
        lastob = new Obstacle();
        obstacles = new Array();

        yspeed = score = 0;

        //add player to center of the stage the stage
        player.x = stage.stageWidth/2;
        player.y = stage.stageHeight/2;
        addChild(player);

        //create 3 obstacles ()
        for(var i:int = 1; i <= 3; i++) { createObstacle(); }

        //Add EnterFrame EventListeners (which is called every frame) and KeyBoard EventListeners
        addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);
        stage.addEventListener(KeyboardEvent.KEY_UP, key_up);
    }

    function restart()
    {
        //# reset object positions for new game instead making "new" instances of same thing (each adds to memory usage)

        if( MovieClip(this.root).contains(player) )
        {
            //removeChild(player); //why remove fom Stage but then re-create and re-add more in memory?

            player.x = stage.stageWidth/2; player.y = stage.stageHeight/2; //reset starting position
            score = yspeed = 0; //reset values to zero
            btnPlay.visible = true; //show Play button

            for(var i:int = 0; i < obstacles.length; ++i)
            {
                if(contains(obstacles[i]) && obstacles[i] != null)
                removeChild(obstacles[i]);
                obstacles[i] = null;
            }
            obstacles.slice(0);

            //create 3 obstacles
            for(var i:int = 1; i <= 3; i++) { createObstacle(); }

            //init(); //you already created first time, just re-use not create more new ones.
        }
    }

} //end Class

} //end Package
VC.One
  • 14,790
  • 4
  • 25
  • 57
  • Thank you. The code looks cleaner. but I am getting an error `Line 37 1084: Syntax error: expecting rightparen before er.` – ratul keot Aug 24 '17 at 07:06
  • @ratulkeot Show me how it looks from where you've written it. Also I forgot to add `btnPlay.visible = true;` in function `restart` so check the new update. – VC.One Aug 24 '17 at 07:24