A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 28

Thread: Help with game.

  1. #1
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Question Help with game.

    Hello I've been trying to make a falling and catching game in AS3 I've managed to make the character move and make the objects fall but I cant get them to do a hittest properly. all I want it to do is when my character hits the object the score text will increase and the ring dissapear.
    I have attached all the files im using I hope someone can help.

    Thank you.
    Last edited by FullMetalEdward; 04-02-2009 at 01:32 PM.

  2. #2
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Post some code please...I don't think a lot of us like downloading zips...
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  3. #3
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Sorry thats understandable ok here is the code:

    This bit is on the first frame of the layer 1.

    Code:
    var leftKeyDown:Boolean = false;
    var upKeyDown:Boolean = false;
    var rightKeyDown:Boolean = false;
    var downKeyDown:Boolean = false;
    var mainSpeed:Number = 15;
     
    shadow_mc.step=5;
    shadow_mc.keydown = false; 
    shadow_mc.stance= 1; 
    shadow_mc.crouch=false;
    
    var left:Boolean = false;
    var right:Boolean = false;
    var walking:Boolean =false; 
    
    
    var ringTime:int = 0;
    var ringLimit:int = 16;
    
     
    shadow_mc.addEventListener(Event.ENTER_FRAME, moveChar);
    function moveChar(event:Event):void{
    	
    
    	if(leftKeyDown){
    		shadow_mc.x -= mainSpeed;
    			shadow_mc.scaleX = -1;
    	if (shadow_mc.currentFrame != 2) {
                shadow_mc.gotoAndStop(2);
            } 
    	}
    	if(rightKeyDown){
    		shadow_mc.x += mainSpeed;
    			shadow_mc.scaleX = +1;
    	{
    	if (shadow_mc.currentFrame != 2) {
             shadow_mc.gotoAndStop(2);
            } 
    	}
    }
    	if(upKeyDown || mainJumping){
    		mainJump();
    	}
    			//adding enemies to stage
    	if(ringTime < ringLimit){
    		//if time hasn't reached the limit, then just increment
    		ringTime ++;
    	} else {
    		//defining a variable which will hold the new enemy
    		var newRing = new Ring();
    		//making the enemy offstage when it is created
    		newRing.y = -1 * newRing.height;
    		//making the enemy's x coordinates random
    		//the "int" function will act the same as Math.floor but a bit faster
    		newRing.x = int(Math.random()*stage.stageWidth - newRing.width);
    		//then add the enemy to stage
    		addChild(newRing);
    		//and reset the enemyTime
    		ringTime = 0;
    	}
    }				
    
    
    
    
    stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
    function checkKeysDown(event:KeyboardEvent):void{
    
    	if(event.keyCode == 37 || event.keyCode == 65){
    		leftKeyDown = true;
    		
    	}
    	if(event.keyCode == 38 || event.keyCode == 87){
    		upKeyDown = true;
    		shadow_mc.gotoAndStop("jump");
    	}
    	if(event.keyCode == 39 || event.keyCode == 68){
    		rightKeyDown = true;
    		
    	}
    	if(event.keyCode == 40 || event.keyCode == 83){
    		downKeyDown = true;
    			shadow_mc.gotoAndStop("duck");
    	}
    		if(left || right)
    	{
    		if(walking ==false)
    		{
    			shadow_mc.gotoAndStop(2);
    			walking = true;
    		}
    }
    }
    
    stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
    function checkKeysUp(event:KeyboardEvent):void{
    
    	if(event.keyCode == 37 || event.keyCode == 65){
    		leftKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 38 || event.keyCode == 87){
    		upKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 39 || event.keyCode == 68){
    		rightKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 40 || event.keyCode == 83){
    		downKeyDown = false;
    		shadow_mc.gotoAndStop("stance");
    	}
    }
    
    var mainJumping:Boolean = false;
    var jumpSpeedLimit:int = 15;
    var jumpSpeed:Number = jumpSpeedLimit;
    
    function mainJump():void{
    	if(!mainJumping){
    		mainJumping = true;
    		jumpSpeed = jumpSpeedLimit*-1;
    		shadow_mc.y += jumpSpeed;
    	} else {
    		if(jumpSpeed < 0){
    			jumpSpeed *= 1 - jumpSpeedLimit/75;
    			if(jumpSpeed > -jumpSpeedLimit/5){
    				jumpSpeed *= -1;
    			}
    		}
    		if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
    			jumpSpeed *= 1 + jumpSpeedLimit/50;
    		}
    		shadow_mc.y += jumpSpeed;
    		shadow_mc.gotoAndStop("jump");
    
    		if(shadow_mc.y >= stage.stageHeight - shadow_mc.height){
    			mainJumping = false;
    			shadow_mc.y = stage.stageHeight - shadow_mc.height;
    			shadow_mc.gotoAndStop("stance");
    		}
    	}
    }
    This bit is in a .as file called hittesting

    Code:
    package {
    	import flash.display.MovieClip;
    	import flash.events.*;
    	import flash.text.TextField;
    	
    	public class hittesting extends MovieClip {
    		
    		var scorenum:Number = 0;
    		
    		public function hittesting() {
    			addEventListener(Event.ENTER_FRAME,enterFrameHandler);
    		}
    		public function enterFrameHandler(e:Event):void {
    			if (shadow_mc.hitTestObject(ring)) {
    				scorenum +=1;
    				score.text = (scorenum.toString());
    			}
    		}//End enterFrame event
    	}//End class
    }//End package
    And this bit is in a .as called Ring.

    Code:
    package{
    	
    	import flash.display.MovieClip;
    	import flash.events.*;
    	
    	public class Ring extends MovieClip{
    
    		private var _root:Object;
    		private var speed:int = 5;
    		
    		public function Ring(){
    			addEventListener(Event.ADDED, beginClass);
    			addEventListener(Event.ENTER_FRAME, eFrame);
    		}
    		private function beginClass(event:Event):void{
    			_root = MovieClip(root);
    		}
    		private function eFrame(event:Event):void{
    			y += speed;
    			if(this.y > stage.stageHeight){
    				removeEventListener(Event.ENTER_FRAME, eFrame);
    				_root.removeChild(this);
    			}
    		}
    	}
    }
    Hope this helps

  4. #4
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Alright...

    Why do you need the hittesting class? It's much easier to have that if statement inside your main loop!
    And anyways, I couldn't find one place where you instantiate the hittesting class. Simply do the hitTesting inside the moveChar function! Should work...

    Also, all the variables you're trying to access in the hittesting class, are local to that class, so they're all undefined.
    Plus you'll need to add all the new Rings you make to an array, and then cycle through it in the moveChar function, and hitTest each one.

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  5. #5
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Not sure why you use separate hittesting class, I would put the hittest in Ring class:
    PHP Code:
    ...
    += speed;
    if(
    this.hitTestObject(shadow_mc)){
        
    scorenum +=1;
        
    score.text = (scorenum.toString());
        
    removeEventListener(Event.ENTER_FRAMEeFrame);
        
    _root.removeChild(this);
    }
    if(
    this.stage.stageHeight){
    ... 

  6. #6
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    It should be:
    Code:
    if(this.hitTestObject(_root.shadow_mc)){
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Quote Originally Posted by tonypa View Post
    Not sure why you use separate hittesting class, I would put the hittest in Ring class:
    PHP Code:
    ...
    += speed;
    if(
    this.hitTestObject(shadow_mc)){
        
    scorenum +=1;
        
    score.text = (scorenum.toString());
        
    removeEventListener(Event.ENTER_FRAMEeFrame);
        
    _root.removeChild(this);
    }
    if(
    this.stage.stageHeight){
    ... 
    My code is all over the place, this seems like it should work but how would I conbine the ring code and the hittesting code?
    My hittesting is linked via document class so putting that bit of code in the ring class and tring to link the .fla document class to Ring.as doesnt seem to work as Ring.as is linked to ring MC.
    Doing this in AS2 was easier in my opinion

  8. #8
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Quote Originally Posted by tonypa View Post
    Not sure why you use separate hittesting class, I would put the hittest in Ring class:
    PHP Code:
    ...
    += speed;
    if(
    this.hitTestObject(shadow_mc)){
        
    scorenum +=1;
        
    score.text = (scorenum.toString());
        
    removeEventListener(Event.ENTER_FRAMEeFrame);
        
    _root.removeChild(this);
    }
    if(
    this.stage.stageHeight){
    ... 
    Ok I've managed to put the hittesting code in with the ring.as so the code looks like this:

    Code:
    package{
    	
    	import flash.display.MovieClip;
    	import flash.events.*;
    	
    	public class Ring extends MovieClip{
    
    		private var _root:Object;
    		private var speed:int = 5;
    		var scorenum:Number = 0;
    		
    		public function Ring(){
    			addEventListener(Event.ADDED, beginClass);
    			addEventListener(Event.ENTER_FRAME, eFrame);
    		}
    		private function beginClass(event:Event):void{
    			_root = MovieClip(root);
    		}
    		private function eFrame(event:Event):void{
    			y += speed;
    			if(this.hitTestObject(_root.shadow_mc)){
        scorenum +=1;
        _root.score.text = (scorenum.toString());
    			if(this.y > stage.stageHeight){
    				removeEventListener(Event.ENTER_FRAME, eFrame);
    				_root.removeChild(this);
    				
    			}
    		}
    	}
    }}
    However no when my character hits the ring the score goes up but it also resets and goes up again each ring I hit, im sure I need a for loop but I was never good with them. any ideas as to what i should do?

  9. #9
    Senior Member tonypa's Avatar
    Join Date
    Jul 2001
    Location
    Estonia
    Posts
    8,227
    Sorry, I only make my games using 1 single class, the logic of your game is much too complicated for me.

  10. #10
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Ok I have finaly managed to put all my code into one .as file and all is called onto stage when i load the .swf...now for my next problem...How can I make a start screen for my game?, I simply want to make it so none of my code runs (essepcially the timer function and the character loading) until I click a start button. And I want to make the start screen on the first frame, currently my main stage is on it. So how can I add a button to the first frame that runs my code on click?

    here is my code so far:

    Code:
    package {
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.utils.Timer;
    	import flash.utils.getDefinitionByName;
    
    	//Creates a new character and places it on stage.
    	public class EarlGame extends MovieClip {
    		var earl_mc:Earl;
    		var nextObject:Timer;
    		var objects:Array = new Array();
    		var score:int = 0;
    		var ring:int = 0;
    		const speed:Number = 7.0;
    		
    		public function EarlGame() {
    			earl_mc = new Earl();
    			earl_mc.y = 325;
    			earl_mc.x = 250;
    			addChild(earl_mc);
    			setNextObject();
    			addEventListener(Event.ENTER_FRAME, moveObjects);
    
    
    //Sets the variables for the character's movements.
    var leftKeyDown:Boolean = false;
    var upKeyDown:Boolean = false;
    var rightKeyDown:Boolean = false;
    var downKeyDown:Boolean = false;
    var mainSpeed:Number = 15;
     
    earl_mc.keydown = false; 
    earl_mc.stance= 1; 
    earl_mc.crouch=false;
    
    var left:Boolean = false;
    var right:Boolean = false;
    var walking:Boolean =false; 
    
     
    earl_mc.addEventListener(Event.ENTER_FRAME, moveChar);
    function moveChar(event:Event):void{
    	
    
    	if(leftKeyDown){
    		earl_mc.x -= mainSpeed;
    			earl_mc.scaleX = -1;
    	if (earl_mc.currentFrame != 2) {
                earl_mc.gotoAndStop(2);
            } 
    	}
    	if(rightKeyDown){
    		earl_mc.x += mainSpeed;
    			earl_mc.scaleX = +1;
    	{
    	if (earl_mc.currentFrame != 2) {
             earl_mc.gotoAndStop(2);
            } 
    	}
    }
    	if(upKeyDown || mainJumping){
    		mainJump();
    	}
    }				
    
    
    //Checks if keys are down
    stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
    function checkKeysDown(event:KeyboardEvent):void{
    	
    
    	if(event.keyCode == 37 || event.keyCode == 65){
    		leftKeyDown = true;
    		
    	}
    	if(event.keyCode == 38 || event.keyCode == 87){
    		upKeyDown = true;
    		earl_mc.gotoAndStop("jump");
    	}
    	if(event.keyCode == 39 || event.keyCode == 68){
    		rightKeyDown = true;
    		
    	}
    	if(event.keyCode == 40 || event.keyCode == 83){
    		downKeyDown = true;
    			earl_mc.gotoAndStop("duck");
    	}
    		if(left || right)
    	{
    		if(walking ==false)
    		{
    			earl_mc.gotoAndStop(2);
    			walking = true;
    		}
    	}
    }
    
    //Checks if keys are up and if so puts character in it's stance position.
    stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
    function checkKeysUp(event:KeyboardEvent):void{
    
    	if(event.keyCode == 37 || event.keyCode == 65){
    		leftKeyDown = false;
    		earl_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 38 || event.keyCode == 87){
    		upKeyDown = false;
    		earl_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 39 || event.keyCode == 68){
    		rightKeyDown = false;
    		earl_mc.gotoAndStop("stance");
    	}
    	if(event.keyCode == 40 || event.keyCode == 83){
    		downKeyDown = false;
    		earl_mc.gotoAndStop("stance");
    	}
    }
    
    //Runs the jump code so when the up key is pressed the character jumps.
    var mainJumping:Boolean = false;
    var jumpSpeedLimit:int = 15;
    var jumpSpeed:Number = jumpSpeedLimit;
    
    function mainJump():void{
    	if(!mainJumping){
    		mainJumping = true;
    		jumpSpeed = jumpSpeedLimit*-1;
    		earl_mc.y += jumpSpeed;
    	} else {
    		if(jumpSpeed < 0){
    			jumpSpeed *= 1 - jumpSpeedLimit/75;
    			if(jumpSpeed > -jumpSpeedLimit/5){
    				jumpSpeed *= -1;
    			}
    		}
    		if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
    			jumpSpeed *= 1 + jumpSpeedLimit/50;
    		}
    		earl_mc.y += jumpSpeed;
    		earl_mc.gotoAndStop("jump");
    
    		if(earl_mc.y >= 320){
    			mainJumping = false;
    			earl_mc.y = 325;
    			earl_mc.gotoAndStop("stance");
    	  		}
    		}
    	}
    }
    		
    		
    		//Function that triggers the objects falling, when they fall and if they hit the character they dissapear.
    		public function setNextObject() {
    			nextObject = new Timer(1000+Math.random()*1000,1);
    			nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
    			nextObject.start();
    		}
    
    		public function newObject(e:Event) {
    			var rings:Array = ["Ring1","Ring2"];
    			var bombs:Array = ["Creature1"];
    			var emeralds:Array = ["Emerald1","Emerald2","Emerald3","Emerald4","Emerald5","Emerald6","Emerald7","Emerald8"];
    			
    			if (Math.random() < .5) {
    				var r:int = Math.floor(Math.random()*rings.length);
    				var classRef:Class = getDefinitionByName(rings[r]) as Class;
    				var newObject:MovieClip = new classRef(); 
    				newObject.typestr = "ring";
    			} else if(Math.random() < .5){
    				r = Math.floor(Math.random()*bombs.length);
    				classRef = getDefinitionByName(bombs[r]) as Class;
    				newObject = new classRef(); 
    				newObject.typestr = "bombs";
    			} else {
    				r = Math.floor(Math.random()*emeralds.length);
    				classRef = getDefinitionByName(emeralds[r]) as Class;
    				newObject = new classRef(); 
    				newObject.typestr = "emeralds";
    				
    			}
    
    			newObject.x = Math.random()*500;
    			addChild(newObject);
    			objects.push(newObject);
    			setNextObject();
    		}
    		
    		public function moveObjects(e:Event) {
    			for(var i:int=objects.length-1;i>=0;i--) {
    				objects[i].y += speed;
    				if (objects[i].y > 400) {
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    				if (objects[i].hitTestObject(earl_mc)) {
    					if (objects[i].typestr == "ring") {
    						score += 5;
    						ring += 1;
    					} else if (objects[i].typestr == "emeralds"){
    						score += 10;
    					} else {
    						score -= 3;
    					}
    					if (score < 0) score = 0;
    					scoreDisplay.text = ""+score;
    					ringDisplay.text = ""+ring;
    					removeChild(objects[i]);
    					objects.splice(i,1);
    
    if(score == 100){
    trace("WIN!!!");
    }
    
    				}
    			}
    		}
    	}
    }
    Hope someone can help

  11. #11
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Nevermind I did it

  12. #12
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    Does it seem like maybe this guy didn't need any help?

  13. #13
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    it's strange how I seem to search for ages looking for how to do something then when I cant find it I ask on here then coincidently when I continue searching i find it....Seems to help :P
    But Im still thankfull for those who replied

  14. #14
    Junior Member
    Join Date
    Mar 2009
    Posts
    21

    Removing Events?

    Heya again im wondering if anyone knows how I can stop this code from running when my character's health gets to zero, I want it to go to the next frame without it wanting to keep loading new objects.

    Code:
    package {
    
    
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.utils.Timer;
    	import flash.utils.getDefinitionByName;
    	import flash.media.Sound;
    
    	//Creates a new character and places it on stage.
    	public class MainGame extends MovieClip {
    		var char_mc:Char;
    		var gameOver:GameOver;
    		var nextObject:Timer;
    		var objects:Array = new Array();
    		var coin:int = 0;
    		var present:int = 0;
    		const speed:Number = 7.0;
    
    		public function MainGame() {
    			startButtonChar.addEventListener( MouseEvent.CLICK, onClickStartChar );
    		}
    		public function onClickStartChar( event:MouseEvent ):void {
    			gotoAndStop(2);
    			char_mc = new Char();
    			char_mc.y = 325;
    			char_mc.x = 250;
    			addChild(char_mc);
    
    			addEventListener(Event.ENTER_FRAME, moveObjects);
    			setNextObject();
    
    
    
    
    			//Sets the variables for the character's movements.
    			var leftKeyDown:Boolean = false;
    			var upKeyDown:Boolean = false;
    			var rightKeyDown:Boolean = false;
    			var downKeyDown:Boolean = false;
    			var mainSpeed:Number = 15;
    
    			char_mc.keydown = false;
    			char_mc.stance= 1;
    			char_mc.crouch=false;
    
    			var left:Boolean = false;
    			var right:Boolean = false;
    			var walking:Boolean =false;
    
    
    			char_mc.addEventListener(Event.ENTER_FRAME, moveChar);
    			function moveChar(event:Event):void {
    
    
    				if (leftKeyDown) {
    					char_mc.x -= mainSpeed;
    					char_mc.scaleX = -1;
    					if (char_mc.currentFrame != 2) {
    						char_mc.gotoAndStop(2);
    					}
    				}
    				if (rightKeyDown) {
    					char_mc.x += mainSpeed;
    					char_mc.scaleX = +1;
    					{
    						if (char_mc.currentFrame != 2) {
    							char_mc.gotoAndStop(2);
    						}
    					}
    				};
    				if (upKeyDown || mainJumping) {
    					mainJump();
    				}
    			}
    
    
    			//Checks if keys are down
    			stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
    			function checkKeysDown(event:KeyboardEvent):void {
    
    
    				if (event.keyCode == 37 || event.keyCode == 65) {
    					leftKeyDown = true;
    
    				}
    				if (event.keyCode == 38 || event.keyCode == 87) {
    					upKeyDown = true;
    					char_mc.gotoAndStop("jump");
    				}
    				if (event.keyCode == 39 || event.keyCode == 68) {
    					rightKeyDown = true;
    
    				}
    				if (event.keyCode == 40 || event.keyCode == 83) {
    					downKeyDown = true;
    					char_mc.gotoAndStop("duck");
    				}
    				if (left || right) {
    					if (walking ==false) {
    						char_mc.gotoAndStop(2);
    						walking = true;
    					}
    				}
    			}
    
    			//Checks if keys are up and if so puts character in it's stance position.
    			stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
    			function checkKeysUp(event:KeyboardEvent):void {
    
    				if (event.keyCode == 37 || event.keyCode == 65) {
    					leftKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    				if (event.keyCode == 38 || event.keyCode == 87) {
    					upKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    				if (event.keyCode == 39 || event.keyCode == 68) {
    					rightKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    				if (event.keyCode == 40 || event.keyCode == 83) {
    					downKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    			}
    
    			//Runs the jump code so when the up key is pressed the character jumps.
    			var mainJumping:Boolean = false;
    			var jumpSpeedLimit:int = 15;
    			var jumpSpeed:Number = jumpSpeedLimit;
    
    			function mainJump():void {
    				if (!mainJumping) {
    					mainJumping = true;
    					jumpSpeed = jumpSpeedLimit*-1;
    					char_mc.y += jumpSpeed;
    				} else {
    					if (jumpSpeed < 0) {
    						jumpSpeed *= 1 - jumpSpeedLimit/75;
    						if (jumpSpeed > -jumpSpeedLimit/5) {
    							jumpSpeed *= -1;
    						}
    					}
    					if (jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit) {
    						jumpSpeed *= 1 + jumpSpeedLimit/50;
    					}
    					char_mc.y += jumpSpeed;
    					char_mc.gotoAndStop("jump");
    
    					if (char_mc.y >= 320) {
    						mainJumping = false;
    						char_mc.y = 325;
    						char_mc.gotoAndStop("stance");
    					}
    				}
    			}
    		}
    		//Function that triggers the objects falling, when they fall and if they hit the character they dissapear.
    		public function setNextObject() {
    			nextObject = new Timer(1000+Math.random()*1000,1);
    			nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
    			nextObject.start();
    		}
    
    		public function newObject(e:Event) {
    			var coins:Array = ["Coin1","Coin2"];
    			var creatures:Array = ["Creature1"];
    			var presents:Array = ["Presents1","Presents2","Presents3","Presents4","Presents5","Presents6","Presents7","Presents8"];
    
    			if (Math.random() < .5) {
    				var r:int = Math.floor(Math.random()*coins.length);
    				var classRef:Class = getDefinitionByName(coins[r]) as Class;
    				var newObject:MovieClip = new classRef();
    				newObject.typestr = "coin";
    			} else if (Math.random() < .5) {
    				r = Math.floor(Math.random()*creatures.length);
    				classRef = getDefinitionByName(creatures[r]) as Class;
    				newObject = new classRef();
    				newObject.typestr = "creatures";
    			} else {
    				r = Math.floor(Math.random()*presents.length);
    				classRef = getDefinitionByName(presents[r]) as Class;
    				newObject = new classRef();
    				newObject.typestr = "presents";
    
    			}
    
    			newObject.x = Math.random()*500;
    			addChild(newObject);
    			objects.push(newObject);
    			setNextObject();
    		}
    		public function moveObjects(e:Event) {
    			for (var i:int=objects.length-1; i>=0; i--) {
    				objects[i].y += speed;
    				if (objects[i].y > 400) {
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    				if (objects[i].hitTestObject(char_mc)) {
    					if (objects[i].typestr == "coin") {
    						coin += 1;
    					} else if (objects[i].typestr == "presents") {
    						present += 1;
    					} else {
    						present -= 3;
    						changeHealth(-50);
    					}
    					if (coin < 0) {
    						coin = 0;
    					}
    					if (present < 0) {
    						present = 0;
    					}
    					coinDisplay.text = ""+coin;
    					presentDisplay.text = ""+present;
    					removeChild(objects[i]);
    					objects.splice(i,1);
    
    					//This function changes the health
    					function changeHealth(healthDiff:Number):void {
    						healthDiff /= 100;
    						if (mcHealth.scaleX + healthDiff >= 1) {
    							mcHealth.scaleX = 1;
    						} else if (mcHealth.scaleX + healthDiff <= 0) {
    							
    THIS IS WHERE I WANT IT TO GO TO NEXT FRAME AND STOP THE CODE FROM RUNNING.
    
    						} else {
    							mcHealth.scaleX += healthDiff;
    						}
    					}
    				}
    			}
    		}
    	}
    }
    Hope someone can help, thank you

  15. #15
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    When player's health is zero, then set a flag to true. Then in your newObject function, check the flag before calling setNextObject function again.

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  16. #16
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Quote Originally Posted by Pazil View Post
    When player's health is zero, then set a flag to true. Then in your newObject function, check the flag before calling setNextObject function again.

    P.
    What do you mean by flag?

  17. #17
    Pencil Farmer cadin's Avatar
    Join Date
    Jul 2006
    Location
    Vancouver BC
    Posts
    323
    If you're just asking how to stop the enterFrame event you'd put
    PHP Code:
    removeEventListener(Event.ENTER_FRAMEmoveObjects
    in that little block where you check for health reaching zero.

    Sorry if that's not what you're asking.

  18. #18
    Please, Call Me Bob trogdor458's Avatar
    Join Date
    Aug 2006
    Location
    Pensacola, FL
    Posts
    915
    Quote Originally Posted by FullMetalEdward
    What do you mean by flag?
    A flag is just a variable set to true or false
    Just put the entire block of code in an if statement
    Make it so the if statement only executes when your guy's alive n well

  19. #19
    Senior Member Pazil's Avatar
    Join Date
    Sep 2006
    Location
    Ontario, Canada
    Posts
    913
    Well, instead of nesting the whole thing in an if statement, only have:
    PHP Code:
    if (!alive)
         
    setNextObject(); 
    in your newObject function. You might get a case where one more enemy spawns...

    What I'd recommend doing is:
    Code:
    nextObject.removeEventListener( TimerEvent.TIMER_COMPLETE, newObject);
    Put this in the if statement when health is zero. (Place this where you wrote:
    THIS IS WHERE I WANT IT TO GO TO NEXT FRAME AND STOP THE CODE FROM RUNNING.)

    P.
    WIP-ZOMBIES

    I love vegetarians! More meat for the rest of us!

  20. #20
    Junior Member
    Join Date
    Mar 2009
    Posts
    21
    Quote Originally Posted by Pazil View Post
    Well, instead of nesting the whole thing in an if statement, only have:
    PHP Code:
    if (!alive)
         
    setNextObject(); 
    in your newObject function. You might get a case where one more enemy spawns...

    What I'd recommend doing is:
    Code:
    nextObject.removeEventListener( TimerEvent.TIMER_COMPLETE, newObject);
    Put this in the if statement when health is zero. (Place this where you wrote:
    THIS IS WHERE I WANT IT TO GO TO NEXT FRAME AND STOP THE CODE FROM RUNNING.)

    P.
    ok So I added those like this:

    Code:
    package {
    
    
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.utils.Timer;
    	import flash.utils.getDefinitionByName;
    	import flash.media.Sound;
    
    	//Creates a new character and places it on stage.
    	public class Game extends MovieClip {
    		var char_mc:Char;
    		var gameOver:GameOver;
    		var nextObject:Timer;
    		var objects:Array = new Array();
    		var coin:int = 0;
    		var present:int = 0;
    		var food:int = 0;
    		var chary:int = 325;
    		var charx:int = 250;
    		const speed:Number = 7.0;
    		var alive:Boolean = false;
    		
    		
    
    
    		public function Game() {
    
    			startButtonChar.addEventListener( MouseEvent.CLICK, onClickStartChar );
    		}
    		public function onClickStartChar( event:MouseEvent ):void {
    			gotoAndStop(2);
    			char_mc = new Char();
    			char_mc.y = chary;
    			char_mc.x = charx;
    			addChild(char_mc);
    
    			addEventListener(Event.ENTER_FRAME, moveObjects);
    //ADDED
    			if (!alive) {
    			setNextObject();}
    
    
    
    
    			//Sets the variables for the character's movements.
    			var leftKeyDown:Boolean = false;
    			var upKeyDown:Boolean = false;
    			var rightKeyDown:Boolean = false;
    			var downKeyDown:Boolean = false;
    			var mainSpeed:Number = 15;
    
    			char_mc.keydown = false;
    			char_mc.stance= 1;
    			char_mc.crouch=false;
    
    			var left:Boolean = false;
    			var right:Boolean = false;
    			var walking:Boolean =false;
    
    
    			char_mc.addEventListener(Event.ENTER_FRAME, moveChar);
    			function moveChar(event:Event):void {
    
    
    				if (leftKeyDown) {
    					char_mc.x -= mainSpeed;
    					char_mc.scaleX = -1;
    					if (char_mc.currentFrame != 2) {
    						char_mc.gotoAndStop(2);
    					}
    				}
    				if (rightKeyDown) {
    					char_mc.x += mainSpeed;
    					char_mc.scaleX = +1;
    					{
    						if (char_mc.currentFrame != 2) {
    							char_mc.gotoAndStop(2);
    						}
    					}
    				};
    				if (upKeyDown || mainJumping) {
    					mainJump();
    				}
    			}
    
    
    			//Checks if keys are down
    			stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
    			function checkKeysDown(event:KeyboardEvent):void {
    
    
    				if (event.keyCode == 37 || event.keyCode == 65) {
    					leftKeyDown = true;
    
    				}
    				if (event.keyCode == 38 || event.keyCode == 87) {
    					upKeyDown = true;
    					char_mc.gotoAndStop("jump");
    				}
    				if (event.keyCode == 39 || event.keyCode == 68) {
    					rightKeyDown = true;
    
    				}
    				if (event.keyCode == 40 || event.keyCode == 83) {
    					downKeyDown = true;
    					char_mc.gotoAndStop("duck");
    				}
    				if (left || right) {
    					if (walking ==false) {
    						char_mc.gotoAndStop(2);
    						walking = true;
    					}
    				}
    			}
    
    			//Checks if keys are up and if so puts character in it's stance position.
    			stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
    			function checkKeysUp(event:KeyboardEvent):void {
    
    				if (event.keyCode == 37 || event.keyCode == 65) {
    					leftKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    				if (event.keyCode == 38 || event.keyCode == 87) {
    					upKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    				if (event.keyCode == 39 || event.keyCode == 68) {
    					rightKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    				if (event.keyCode == 40 || event.keyCode == 83) {
    					downKeyDown = false;
    					char_mc.gotoAndStop("stance");
    				}
    			}
    
    			//Runs the jump code so when the up key is pressed the character jumps.
    			var mainJumping:Boolean = false;
    			var jumpSpeedLimit:int = 15;
    			var jumpSpeed:Number = jumpSpeedLimit;
    
    			function mainJump():void {
    				if (!mainJumping) {
    					mainJumping = true;
    					jumpSpeed = jumpSpeedLimit*-1;
    					char_mc.y += jumpSpeed;
    				} else {
    					if (jumpSpeed < 0) {
    						jumpSpeed *= 1 - jumpSpeedLimit/75;
    						if (jumpSpeed > -jumpSpeedLimit/5) {
    							jumpSpeed *= -1;
    						}
    					}
    					if (jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit) {
    						jumpSpeed *= 1 + jumpSpeedLimit/50;
    					}
    					char_mc.y += jumpSpeed;
    					char_mc.gotoAndStop("jump");
    
    					if (char_mc.y >= 325) {
    						mainJumping = false;
    						char_mc.y = chary;
    						char_mc.gotoAndStop("stance");
    					}
    				}
    			}
    		}
    		//Function that triggers the objects falling, when they fall and if they hit the character they dissapear.
    		public function setNextObject() {
    			nextObject = new Timer(1000+Math.random()*1000,1);
    			nextObject.addEventListener(TimerEvent.TIMER_COMPLETE,newObject);
    			nextObject.start();
    		}
    
    		public function newObject(e:Event) {
    			var coins:Array = ["Coin1","Coin2"];
    			var item:Array = ["Object1","Object2","Object3"];
    			var presents:Array = ["Presents1","Presents2","Presents3","Presents4","Presents5","Presents6","Presents7","Presents8"];
    			var foods:Array = ["Food1","Food2"];
    			
    			if (Math.random() < .5) {
    				var r:int = Math.floor(Math.random()*coins.length);
    				var classRef:Class = getDefinitionByName(coins[r]) as Class;
    				var newObject:MovieClip = new classRef();
    				newObject.typestr = "coin";
    			} else if (Math.random() < .5) {
    				r = Math.floor(Math.random()*item.length);
    				classRef = getDefinitionByName(item[r]) as Class;
    				newObject = new classRef();
    				newObject.typestr = "item";
    			} else if (Math.random() < .5) {
    				r = Math.floor(Math.random()*foods.length);
    				classRef = getDefinitionByName(foods[r]) as Class;
    				newObject = new classRef();
    				newObject.typestr = "food";
    			} else {
    				r = Math.floor(Math.random()*presents.length);
    				classRef = getDefinitionByName(presents[r]) as Class;
    				newObject = new classRef();
    				newObject.typestr = "presents";
    
    			}
    
    			newObject.x = Math.random()*500;
    			addChild(newObject);
    			objects.push(newObject);
    			setNextObject();
    		}
    		public function moveObjects(e:Event) {
    			for (var i:int=objects.length-1; i>=0; i--) {
    				objects[i].y += speed;
    				if (objects[i].y > 400) {
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    				if (objects[i].hitTestObject(char_mc)) {
    					if (objects[i].typestr == "coin") {
    						jammin.play();
    						coin += 1;
    					} else if (objects[i].typestr == "presents") {
    						tubeshot.play();
    						present += 1;
    					} else if (objects[i].typestr == "food") {
    						bite.play();
    						food += 1;
    					char_mc.scaleX += 0.1;
    					char_mc.scaleY += 0.1;
    					char_mc.y -= 2;
    					chary -=2;
    					
    					} else {
    						changeHealth(-50);
    						youch2.play();
    					}
    					if (coin < 0) {
    						coin = 0;
    					}
    					if (present < 0) {
    						present = 0;
    					}
    					coinDisplay.text = ""+coin;
    					presentDisplay.text = ""+present;
    					foodDisplay.text = ""+food;
    					removeChild(objects[i]);
    					objects.splice(i,1);
    
    					//This function changes the health
    					function changeHealth(healthDiff:Number):void {
    						healthDiff /= 100;
    						if (mcHealth.scaleX + healthDiff >= 1) {
    							mcHealth.scaleX = 1;
    						} else if (mcHealth.scaleX + healthDiff <= 0) {
    //ADDED
    							alive = true;
    							nextObject.removeEventListener( TimerEvent.TIMER_COMPLETE, newObject);
    							gotoAndStop(3);
    							
    						} else {
    							mcHealth.scaleX += healthDiff;
    						}
    					}
    				}
    			}
    		}
    	}
    }
    Now it keeps comming up with.

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at Game/moveObjects()

    Any Suggestions for this

Tags for this Thread

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