A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: [FLASH CS6, AS3] Novice Game Controls Stop Working After First Level

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    2

    [FLASH CS6, AS3] Novice Game Controls Stop Working After First Level

    Hello,

    I am learning Flash CS6. I am currently attempting to create a character movement game using learning materials from the official Adobe website. I am doing project 5 in the “ddcg_cs6_project5.pdf” found here: http://edex.adobe.com/resource/60723ad6c2/.

    I am experiencing a problem with character controls.

    When I first began this project, I used Scene 1 for the creation of “Level 1” in my side-scrolling game. My character has a running animation, a jumping animation and a rolling animation to evade obstacles. I have the actionscript 3.0 code for moving my character left and right in frame 1 of the Actions layer on the main timeline. I placed the actionscipt code for jumping and rolling within the movie symbol of the main character. Now, everything, including the controls of the main character, works fine as long as Scene 1 plays first when I Test the movie. I first noticed a problem with controlling the main character after creating Scene 2, the “Gave Over” screen which contains a “Replay” button. When I click replay I am taken back to Scene 1. Everything seems to be working fine on Scene 1 accept for the fact that I can no longer control the main character. The background is scrolling to the left as expected to create the illusion of movement. The enemies, projectiles and obstacles appear as planned. The timer counts down; the health counter and the score counter work. Everything now works accept for the character controls. After I first noticed this problem, I decided to then create my Title screen and move it to the top of the scene list in order to see if Scene 1 (Level 1) would play correctly upon clicking the “Play Now” button. Unfortunately, I received the same results as I did clicking the “Replay” button on the Game over screen. As it is now, I can only play level one properly for a single time as long level 1, Scene 1, is at the top of the scene list while testing.

    During this project, I have encountered many unexpected hurdles along the way, and I have spent many hours finding solutions myself. However, this one has stumped me. I am hoping there is a simple way to fix this problem, and I appreciate any help.

    If it helps, I will provide here the actionscript used on the Scene 1 timeline and the actionscript used inside the main character’s movie symbol. Please let me know if more information is needed.

    Scene 1 Main Timeline Code:
    Code:
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    
    var timeleft=20;
    var gameTimer = new Timer(1000);
    timetxt.text=String(timeleft);
    gameTimer.addEventListener(TimerEvent.TIMER, countDown);
    
    function countDown(e:TimerEvent):void {
    	timeleft--;
    	timetxt.text=String(timeleft);
    	if (timeleft<0) {
    		gameTimer.stop();
    		gameTimer.removeEventListener(TimerEvent.TIMER, countDown);
    		stage.removeEventListener(Event.ENTER_FRAME, gameloop);
    		stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkkeysdown);
    		stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
    		gotoAndStop(1, "Scene 3");
    	}
    }
    
    gameTimer.start();
    
    var score=0;
    score_txt.text=score.toString();
    
    var hitCrouchingBullet01:Boolean=false;
    var hitObstacle01:Boolean=false;
    var hitStandingBullet01:Boolean=false;
    var health=1;
    health_txt.text=health.toString();
    
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, checkkeysdown);
    stage.addEventListener(KeyboardEvent.KEY_UP, checkkeysup);
    
    var speed=3;
    var moveright=false;
    var moveleft=false;
    
    function checkkeysdown(mykey:KeyboardEvent) {
    	if (mykey.keyCode==Keyboard.RIGHT) {
    		moveright=true;
    	}
    	if (mykey.keyCode==Keyboard.LEFT) {
    		moveleft=true;
    	}
    }
    
    function checkkeysup(mykey:KeyboardEvent) {
    	if (mykey.keyCode==Keyboard.RIGHT) {
    		moveright=false;
    	}
    	if (mykey.keyCode==Keyboard.LEFT) {
    		moveleft=false;
    	}
    }
    
    stage.addEventListener(Event.ENTER_FRAME, gameloop);
    
    function gameloop(evt:Event):void {
    	if (moveright==true) {
    		if (player_mc.x>320) {
    			player_mc.x-=speed - 1.1;
    		}
    		player_mc.x+=speed + 2;
    	}
    	if (moveleft==true) {
    		player_mc.x+=speed - 9;
    	}
    	else {
    		player_mc.x-=speed;
    	}
    	
    	// Standing Bullet 01 Begins
    	standing_bullet_01_mc.x+=0.1;
    	standing_flash_01_mc.x+=1;
    	if (standing_bullet_01_mc.x>-20){
    		standing_bullet_01_mc.x+=11;
    	}
    	if (standing_bullet_01_mc.x>76){
    		standing_flash_01_mc.x=75;
    	}
    	if (standing_bullet_01_mc.x>200){
    		standing_flash_01_mc.x=-75;
    	}
    	if (standing_flash_01_mc.x>80){
    		standing_flash_01_mc.x=650;
    	}
    	if (player_mc.hitTestObject(standing_bullet_01_mc)) {
    		if(hitStandingBullet01==false) {
    			health--;
    		}
    		hitStandingBullet01=true;
    		health_txt.text=health.toString();
    	}
    	// Standing Bullet 01 Ends
    	
    	
    	// Crouching Bullet 01 Begins
    	crouching_bullet_01_mc.x+=0.3;
    	crouching_flash_01_mc.x+=1;
    	if (crouching_bullet_01_mc.x>-20){
    		crouching_bullet_01_mc.x+=8;
    	}
    	if (crouching_bullet_01_mc.x>100){
    		crouching_flash_01_mc.x=100;
    	}
    	if (crouching_bullet_01_mc.x>225){
    		crouching_flash_01_mc.x=-100;
    	}
    	if (crouching_flash_01_mc.x>105){
    		crouching_flash_01_mc.x=650;
    	}
    	if (player_mc.hitTestObject(crouching_bullet_01_mc)) {
    		if(hitCrouchingBullet01==false) {
    			health--;
    		}
    		hitCrouchingBullet01=true;
    		health_txt.text=health.toString();
    	}
    	if (crouching_bullet_01_mc.x>630){
    		score++;
    		score_txt.text=score.toString();
    	}
    	//Crouching Bullet 01 Ends
    	
    	
    	// Obstacle 01 Begins
    	obstacle_01_mc.x-=0.1;
    	if (obstacle_01_mc.x<700){
    		obstacle_01_mc.x-=8.3;
    	}
    	if (player_mc.hitTestObject(obstacle_01_mc)) {
    		if(hitObstacle01==false) {
    			health--;
    		}
    		hitObstacle01=true;
    		health_txt.text=health.toString();
    	}
    	// Obstacle 01  Ends
    	
    	if (health<=0) {
    		gameTimer.removeEventListener(TimerEvent.TIMER, countDown);
    		stage.removeEventListener(Event.ENTER_FRAME, gameloop);
    		stage.removeEventListener(KeyboardEvent.KEY_DOWN, checkkeysdown);
    		stage.removeEventListener(KeyboardEvent.KEY_UP, checkkeysup);
    		gotoAndStop(1, "Scene 2");
    	}
    }
    Scene 1 - Code within the main character's movie symbol (to control jumping and crouching animations within the symbol):
    Code:
    import flash.events.KeyboardEvent;
    import flash.events.Event;
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, checkkeysdown);
    stage.addEventListener(KeyboardEvent.KEY_UP, checkkeysup);
    
    var upArrow:Boolean = false;
    var downArrow:Boolean = false;
    
    function checkkeysdown(mykey:KeyboardEvent) {
       if(mykey.keyCode == 38)
       {
          
          if(!upArrow) 
          {
             upArrow = true;
             gotoAndPlay(19);
          }
       }
       
       if(mykey.keyCode == 40)
       {
          
          if(!downArrow) 
          {
             downArrow = true;
             gotoAndPlay(53);
          }
       }
    }
    
    function checkkeysup(mykey:KeyboardEvent) {
       if(mykey.keyCode == 38)
       {
          upArrow = false;
          gotoAndPlay(1);
       }
       
       if(mykey.keyCode == 40)
       {
          downArrow = false;
          gotoAndPlay(1);
       }
    }
    Thank you.

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    just remove your eventListeners just before it switches to the next level and then readd them to the new level's code
    ~calmchess~

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    2
    Hi, calmchess.

    Thank you for your reply. I’m sure you are right about there being a problem with event listeners somewhere, but here is my problem.

    Currently, I am not looking ahead to level 2. I am trying to get the player controls for level 1 to work properly after the “Play Now” button is clicked on the title scene.

    If my scene stack looks like this:

    title scene
    level 1
    win scene
    lose scene

    When I test the movie, the controls will not work on level 1 once the title scene’s “Play Now” button links me to level 1. Since my controls do not work, I lose and go to the lose scene. If I click the “Try Again” button on the lose scene I am taken back to level 1 where the controls still do not work.

    However, if the scene stack looks like this:

    level 1
    title scene
    win scene
    lose scene

    When I test the movie I have full control of my character, and I will go to the win scene or lose scene depending on how well I perform. If I lose and click the “Try Again” button I am taken back to level 1 where I can no longer control my character. Unfortunately, placing level 1 at the top of the scene stack does not help me when I need the title scene to be at the top.

    For the “level 1” scene, the event handlers are declared and get deleted when either the time expires or the player's health runs out. I am under the impression that the event handlers on level 1 will be declared again on level 1 when a button from another scene links back to level 1.

  4. #4
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    If i remember correctly you need to add event handlers every time the frame changes. I suspect you aren't using oop programming and are doing frame by frame animited game. just duplicate your code and place it on frames that it changes to.....I think that is the solution if I remember right its been a long stretch since I did any game programming. I seem to remember also using a dispatch statement that calls back to the frame where the listeneners and adds them again or something. Hope this helps a little.

    --calmchess
    ~calmchess~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center