A Flash Developer Resource Site

Results 1 to 11 of 11

Thread: negative score in catching game?

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    11

    negative score in catching game?

    I'm trying to edit the actionscript of catching game in which a object fall from top to bottom and if the catcher fail to catch the object and the object hit the bottom, the score is subtracted.

    I got my code from http://flashgameu.com/Catching_Game_...12-131621.html

    Here's the code
    Code:
    package {
    	import com.greensock.*;
    	
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.utils.Timer;
    	import flash.utils.getDefinitionByName;
    	
    	public class CatchingSkittles extends MovieClip {
    		var girlmouth:GirlMouth;
    		var girlmouthfront:GirlMouthFront;
    		var nextObject:Timer;
    		var objects:Array = new Array();
    		var score:int = 0;
    		const speed:Number = 7.0;
    		
    		public function CatchingSkittles() {
    			girlmouthfront = new GirlMouthFront();
    			girlmouthfront.y = -49.00;
    			girlmouth = new GirlMouth();
    			girlmouth.y = 308.55;
    			addChild(girlmouth);
    			girlmouth.addChild(girlmouthfront);
    			setNextObject();
    			addEventListener(Event.ENTER_FRAME, moveObjects);
    		}
    		
    		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 goodObjects:Array = ["Red","Purple","Yellow","Orange","Green"];
    			
    			if (Math.random() < .5) {
    				var r:int = Math.floor(Math.random()*goodObjects.length);
    				var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
    				var newObject:MovieClip = new classRef(); 
    				newObject.typestr = "good";
    			} else {
    				r = Math.floor(Math.random()*goodObjects.length);
    				classRef = getDefinitionByName(goodObjects[r]) as Class;
    				newObject = new classRef(); 
    				newObject.typestr = "good";
    			}
    			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 > 425) {
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    				if (objects[i].hitTestObject(girlmouthfront)) {
    					if (objects[i].typestr == "good") {
    						score += 5;
    					} else {
    						score += 5;
    					}
    					if (score < 0) score = 0;
    					scoreDisplay.text = "Score: "+score;
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    			}
    			
    			girlmouth.x = mouseX;
    		}
    		
    		
    	}
    }
    Help! It's for my final project.

    In addition, how do I end the game after the catcher fail to catch at least 5 objects to go to "game over" screen with final score displayed?

  2. #2
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Did you try at all?

    Think it through. When do you need to subtract a point? Where in the code do you know that condition has happened?

    You already know how to keep track of an amount; score is doing that. You also need to keep track of the amount of missed items. How do you think you should do that?

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    I made some changes to the code after I posted it online.

    First I add "score -= 3;" to
    Code:
    public function moveObjects(e:Event) {
    for(var i:int=objects.length-1;i>=0;i--) {
    objects[i].y += speed;
    if (objects[i].y > 425) {
    --------> score -=3;
    removeChild(objects[i]);
    objects.splice(i,1);
    }
    Then I add "if (score < 0) score = 0; scoreDisplay.text = "Score: "+score;" to
    Code:
    public function moveObjects(e:Event) {
    for(var i:int=objects.length-1;i>=0;i--) {
    objects[i].y += speed;
    if (objects[i].y > 425) {
    score -=3;
    removeChild(objects[i]);
    objects.splice(i,1);
    }
    if (objects[i].hitTestObject(girlmouthfront)) {
    if (objects[i].typestr == "good") {
    score += 5;
    } else {
    score += 5;
    }
    if (score < 0) score = 0;
    scoreDisplay.text = "Score: "+score;
    removeChild(objects[i]);
    objects.splice(i,1);
    }
    -------> if (score < 0) score = 0;
    -------> scoreDisplay.text = "Score: "+score;
    
    }
    I'm in middle of figuring out how do I end the game after the catcher fail to catch at least 5 objects to go to "game over" screen with final score displayed?

    Someone suggested that I add a new variable missedItems++ and to put it in code

    Code:
    Code:
    public function moveObjects(e:Event) {
                for(var i:int=objects.length-1;i>=0;i--) {
                    objects[i].y += speed;
                    if (objects[i].y > 425) {
                        score -=3;
         ----->      missedItems++;
                        removeChild(objects[i]);
                        objects.splice(i,1);
                    }
    I did what he suggested but I don't know what else to put in....

  4. #4
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Why did you put the second code which sets score to 0 where you did? You should have put it right after score -= 3 to be like the positive case. Or take it out of the loop entirely and do it after the loop like you put in. That has slightly different meaning, since you could have -3 then +5 and end up with 2 rather than 5. If negative scores are never allowed, you'd expect to have 5.

    After you increment missedItems, you need to check whether it is greater than your limit of missed objects. If it is, you need to act on that.

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    I did what you suggested and when the catcher missed the object and the object hit the bottom, the score was negative. Thanks!!!

    As for missedItems

    Like this?
    Code:
    public function moveObjects(e:Event) {
    			for(var i:int=objects.length-1;i>=0;i--) {
    				objects[i].y += speed;
    				if (objects[i].y > 425) {
    					score -=3;
    					missedItems++ {
    						if (missedItems++ == 5) {
    							EndGame();
    						} else {
    							missedItems++
    						}
    					}
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Close, but do not use ++ inside the if statement. You've already incremented it once, if you do it again, it will go up by 2 each miss. And use >= rather than == just in case you somehow do something silly and it gets set to 6 or something. And do not increment again in the else case either. In fact, don't have an else case.

    I'd also suggest that you adopt a consistent and readable indentation style. You seem to vary wildly and not really with any particular reason.

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    Okay I did what you said and added function GameOver();

    Code:
    public function moveObjects(e:Event) {
    			for(var i:int=objects.length-1;i>=0;i--) {
    				objects[i].y += speed;
    				if (objects[i].y > 425) {
    					score -=3;
    					if (missedItems++ >= 5) {
    						EndGame();
    					} 
    				}
    I added function GameOver() after public function moveObjects().
    Code:
    public function endGame():void{
       	while(objects.numChildren){
          		objects.removeChildAt(0);
       			}
       			gotoAndPlay("GameOver");
    		}
    And what do you mean that my indentation style is wild? I try to keep my indentation style consistent in my actionscript.

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    That's actually not what I said, but it is still valid. I was saying do this:
    Code:
    missedItems++;
    if (missedItems >= 5){
      EndGame();
    }
    But, doing the increment inside the condition is also valid. You may want to use ++missedItems instead which will increment before the comparison instead of after.

    And by wild, I mean like the code you just posted for endGame. By the way, I like "endGame" more than "EndGame" as a method name because by convention only classes should start with capital letters. But you have to be consistent. If your function is "endGame", you cannot call it with "EndGame()".
    "wild" indentation like having the while line start so far over from the function declaration, and having the next line down start further left even though it is inside a more nested block. It makes no sense. I hope that this is just a copy and paste artifact and your actual code looks correct.

  9. #9
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    It's really just copy and paste artifact.

    Anyway, I tried to edit everything but it's not working when I tested the movie.

    I even created a movieclip "GameOver" for Game Over screen.

    Code:
    package {
    	import com.greensock.*;
    	
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.utils.Timer;
    	import flash.utils.getDefinitionByName;
    	
    	public class CatchingSkittles extends MovieClip {
    		var girlmouth:GirlMouth;
    		var girlmouthfront:GirlMouthFront;
    		var nextObject:Timer;
    		var objects:Array = new Array();
    		var score:int = 0;
    		var missedItems;
    		var endGame;
    		const speed:Number = 7.0;
    		
    		public function CatchingSkittles() {
    			girlmouthfront = new GirlMouthFront();
    			girlmouthfront.y = -49.00;
    			girlmouth = new GirlMouth();
    			girlmouth.y = 308.55;
    			addChild(girlmouth);
    			girlmouth.addChild(girlmouthfront);
    			setNextObject();
    			addEventListener(Event.ENTER_FRAME, moveObjects);
    		}
    		
    		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 goodObjects:Array = ["Red","Purple","Yellow","Orange","Green"];
    			
    			if (Math.random() < .5) {
    				var r:int = Math.floor(Math.random()*goodObjects.length);
    				var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
    				var newObject:MovieClip = new classRef(); 
    				newObject.typestr = "good";
    			} else {
    				r = Math.floor(Math.random()*goodObjects.length);
    				classRef = getDefinitionByName(goodObjects[r]) as Class;
    				newObject = new classRef(); 
    				newObject.typestr = "good";
    			}
    			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 > 425) {
    					score -=3;
    					missedItems++;
    					if (missedItems >= 5){
      						endGame();
    					}
    				}
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    				if (objects[i].hitTestObject(girlmouthfront)) {
    					if (objects[i].typestr == "good") {
    						score += 5;
    					} else {
    						score += 5;
    					}
    					
    					scoreDisplay.text = "Score: "+score;
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    				
    				scoreDisplay.text = "Score: "+score;
    				
    			}
    			
    			girlmouth.x = mouseX;
    		}
    		
    		public function endGame():void{
       			while(objects.numChildren){
          		objects.removeChildAt(0);
       			}
       			gotoAndPlay("GameOver");
    		}
    		
    	}
    }

  10. #10
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Define "not working". Are you getting errors? what are they?

    You say you have a GameOver movieclip, but you seem to be trying to go to a GameOver frame instead. That's not the same thing.

  11. #11
    Junior Member
    Join Date
    Nov 2011
    Posts
    11
    I got it to work from the game screen to gameover screen. However, I cannot clear off the objects from "game screen" in "gameover screen"

    Here's the new version of the code so far...
    Code:
    package {
    	import com.greensock.*;
    	
    	import flash.display.*;
    	import flash.events.*;
    	import flash.text.*;
    	import flash.utils.Timer;
    	import flash.utils.getDefinitionByName;
    	
    	public class CatchingSkittles extends MovieClip {
    		var girlmouth:GirlMouth;
    		var girlmouthfront:GirlMouthFront;
    		var nextObject:Timer;
    		var objects:Array = new Array();
    		var score:int = 0;
    		var score2:int = 0;
    		var missedItems:int = 0;
    		var speed:Number = 7.0;
    		
    		public function CatchingSkittles() {
    			girlmouthfront = new GirlMouthFront();
    			girlmouthfront.y = -49.00;
    			girlmouth = new GirlMouth();
    			girlmouth.y = 308.55;
    			addChild(girlmouth);
    			girlmouth.addChild(girlmouthfront);
    			setNextObject();
    			addEventListener(Event.ENTER_FRAME, moveObjects);
    		}
    		
    		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 goodObjects:Array = ["Red","Purple","Yellow","Orange","Green"];
    			
    			if (Math.random() < .8) {
    				var r:int = Math.floor(Math.random()*goodObjects.length);
    				var classRef:Class = getDefinitionByName(goodObjects[r]) as Class;
    				var newObject:MovieClip = new classRef(); 
    				newObject.typestr = "good";
    			} else {
    				r = Math.floor(Math.random()*goodObjects.length);
    				classRef = getDefinitionByName(goodObjects[r]) as Class;
    				newObject = new classRef(); 
    				newObject.typestr = "good";
    			}
    			newObject.x = Math.random()*510;
    			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 > 425) {
    					score -= 5;
    					removeChild(objects[i]);
    					objects.splice(i,1);
    					missedItems +=1;
    					if (missedItems == 5) {
    					gotoAndStop(2);
    					finalScoreDisplay.text = "Score: "+score;
    					speed = -10;
    					removeEventListener(Event.ENTER_FRAME, moveObjects);
    					}
    				}
    				if (objects[i].hitTestObject(girlmouthfront)) {
    					if (objects[i].typestr == "good") {
    						score += 5;
    					} else {
    						score += 5;
    					}
    					scoreDisplay.text = "Score: "+score;
    					removeChild(objects[i]);
    					objects.splice(i,1);
    				}
    				scoreDisplay.text = "Score: "+score;
    				
    				
    			}
    			
    			girlmouth.x = mouseX;
    		}
    		
    		
    	}
    }

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