Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

ActionScript 3 On Click

Community Beginner ,
Feb 12, 2018 Feb 12, 2018

I only want this code to run when a certain button is pressed, I also want it to stop running when another button is pressed. The code is below:

package{

     import flash.events.MouseEvent;

    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) && obstacles != null)

                    removeChild(obstacles);

                    obstacles = 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;

        

            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;

        }

    

    

    }

}

TOPICS
ActionScript
2.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Feb 16, 2018 Feb 16, 2018

Hi again!

I've updated the file in the same link with the changes you requested.

MyWebsiteEdited.zip - Google Drive

What I did was to add the whole game to a single container so it's a matter of adding/removing this container and the buttons' listeners.

I hope it helps!

Regards,

JC

Translate
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Hi.

Change the beginning of your code to this:

package

{

    import flash.events.MouseEvent;

    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

       

        var paused:Boolean = true;

       

        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);

            stage.addEventListener(KeyboardEvent.KEY_DOWN, key_down);

        }

       

        private function key_down(event: KeyboardEvent)

        {

            if (event.keyCode == Keyboard.A)

            {

                paused = true;

                removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

            }               

           

            if (event.keyCode == Keyboard.S)

            {

                paused = false;

                addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

            }               

        }

       

        private function key_up(event: KeyboardEvent)

        {

            if (event.keyCode == Keyboard.SPACE)

            {

                if (!paused)

                {

                    //If space is pressed then make the bird

                    yspeed = -jump_force;

                }

            }               

        }

// CONTINUES...

And change the keys to the ones you want.

I hope it helps.

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 12, 2018 Feb 12, 2018

sorry, I meant on the click of a button. In this case, the instance name would be button_18

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Sorry. I swear a read key. Haha

Here is a version of the code working with two buttons to pause and unpause (bt_18 and bt_19). I only updated the beginning.

package

{

    import flash.events.MouseEvent;

    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

      

        var paused:Boolean = false;

      

        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);

            bt_18.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);

            bt_19.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);

        }

      

        private function mouseHandler(event: MouseEvent)

        {

            if (!paused && event.currentTarget == bt_18)

            {

                paused = true;

                removeEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

            }          

            else if (paused && event.currentTarget == bt_19)

            {

                paused = false;

                addEventListener(Event.ENTER_FRAME, onEnterFrameHandler);

            }              

        }

      

        private function key_up(event: KeyboardEvent)

        {

            if (event.keyCode == Keyboard.SPACE)

            {

                if (!paused)

                {

                    //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) && obstacles != null)

                    removeChild(obstacles);

                obstacles = 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;

            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;

        }

    }

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 12, 2018 Feb 12, 2018

keeps giving me an error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

at Main/init()

at Main/restart()

at Main/onEnterFrameHandler()

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 12, 2018 Feb 12, 2018

Do you have two instances named bt_18 and bt_19 on the stage?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 12, 2018 Feb 12, 2018

I changed them to my instance names

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 13, 2018 Feb 13, 2018

Would mind sharing your FLA and class(es)?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 13, 2018 Feb 13, 2018
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 13, 2018 Feb 13, 2018

Screen Shot 2018-02-13 at 8.28.43 PM.png

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 14, 2018 Feb 14, 2018

Hi again.

Please give me permission to access the file or just make it public.

Thanks,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 14, 2018 Feb 14, 2018

I gave permission. sorry

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 15, 2018 Feb 15, 2018

Did you ever figure out the problem?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 15, 2018 Feb 15, 2018

Sorry about the long delay!

Here is:

MyWebsiteEdited.zip - Google Drive

Basically, your document class (Main) was initializing in the first scene but the button_18 is in the second scene.

I put an event dispatcher in the second scene to let the document class know that it has to start from there.

I hope it helps!

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 15, 2018 Feb 15, 2018

Great!! and what code would I add to stop the game on button_16 to stop and close the game?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2018 Feb 16, 2018

Hi again!

I've updated the file in the same link with the changes you requested.

MyWebsiteEdited.zip - Google Drive

What I did was to add the whole game to a single container so it's a matter of adding/removing this container and the buttons' listeners.

I hope it helps!

Regards,

JC

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 16, 2018 Feb 16, 2018

quick question: say that i wanted to make a mobile version of this site, would I have to make a completely separate .fla ?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2018 Feb 16, 2018

If you don't want to port your code, what you can do is to export your project as an AIR for Android or iOS and leave a link to your app when the players access your site through a mobile device.

If this is not what you want, then, yes, you're gonna have to create a separate FLA document and port everything to HTML5 (Canvas).

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 17, 2018 Feb 17, 2018

I do NOT want the game on the mobile version of the site. how would I go about getting this website up? would I have to pay for web hosting?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 18, 2018 Feb 18, 2018
LATEST

Hi!

If you don't want a mobile version, you'll only have to code a message for mobile users telling them that your game is unavailable for their devices because SWFs won't play in iOS or Android devices anyway.

And, yes, you'll need either a paid or free web hosting.

Or you can opt for uploading your game to services like Kongregate, Newgrounds, Armor Games, and so on.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Feb 16, 2018 Feb 16, 2018

YOU ARE A LIFESAVER! THANK YOU

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Feb 16, 2018 Feb 16, 2018

Excellent!

You're welcome!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines