Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > General > Gaming and Game Development

Reply
 
Thread Tools Rate Thread Display Modes
Old 05-23-2012, 08:28 PM   #1
LadyBug88
Registered User
 
Join Date: May 2012
Posts: 4
Default [AS3] trying to have 2 different "enemy's" fly from the right of the screen.

I am a total beginner. This is also my first forum post ever as well. I am currently in a flash course and I am stuck. I have been working with a tutorial on Micheal James Williams site.

The assignment is to re-create this game with our own ideas. I am trying to have 2 different "enemy's" fly from the right of the screen. I want one enemy to actually be a good enemy and when the avatar touches it it vanishes and you gain points. I want the bad enemy to end the game once the avatar touches it.

I have the bad enemy already working but when I try to add the good enemy the script throws errors depending on where I try to add the code. I haven't really changed much from the original code that is in the tutorial. The code I added is highlighted in bold.

Thank you to anyone that answers. I am at my wits end.

This is the code I am using in the AvoiderGame.as
Code:
package 
{
	import flash.display.MovieClip;
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.ui.Mouse;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.events.Event;
	import flash.media.SoundChannel;
	
	public class AvoiderGame extends MovieClip 
	{
		public var gameClock:Clock;
		public var gameScore:Score;
		public var backgroundContainer:BackgroundContainer;
		public var army:Array;
		public var enemy:Enemy;
                public var good:Good;
		public var avatar:Avatar;
		public var gameTimer:Timer;
		public var useMouseControl:Boolean;
		public var downKeyIsBeingPressed:Boolean;
		public var upKeyIsBeingPressed:Boolean;
		public var leftKeyIsBeingPressed:Boolean;
		public var rightKeyIsBeingPressed:Boolean;
		public var backgroundMusic:BackgroundMusic;
		public var bgmSoundChannel:SoundChannel;	//bgm for BackGround Music
		public var enemyAppearSound:EnemyAppearSound;
		public var sfxSoundChannel:SoundChannel;	//sfx for Sound FX
		public var currentLevelData:LevelData;
		
		public function AvoiderGame() 
		{
			currentLevelData = new LevelData( 1 );
			setBackgroundImage();
			
			backgroundMusic = new BackgroundMusic();
			bgmSoundChannel = backgroundMusic.play();
			bgmSoundChannel.addEventListener( Event.SOUND_COMPLETE, onBackgroundMusicFinished, false, 0, true );
			enemyAppearSound = new EnemyAppearSound();
			
			downKeyIsBeingPressed = false;
			upKeyIsBeingPressed = false;
			leftKeyIsBeingPressed = false;
			rightKeyIsBeingPressed = false;
			
			useMouseControl = false;
			Mouse.hide();
			army = new Array();
			
			avatar = new Avatar();
			addChild( avatar );
			if ( useMouseControl )
			{
				avatar.x = mouseX;
				avatar.y = mouseY;
			}
			else
			{
				avatar.x = 200;
				avatar.y = 250;
			}
			
			gameTimer = new Timer( 25 );
			gameTimer.addEventListener( TimerEvent.TIMER, onTick, false, 0, true );
			gameTimer.start();
			
			addEventListener( Event.ADDED_TO_STAGE, onAddToStage, false, 0, true );
		}
		
		public function onBackgroundMusicFinished( event:Event ):void
		{
			bgmSoundChannel = backgroundMusic.play();
			bgmSoundChannel.addEventListener( Event.SOUND_COMPLETE, onBackgroundMusicFinished, false, 0, true );
		}
		
		public function onAddToStage( event:Event ):void
		{
			stage.addEventListener( KeyboardEvent.KEY_DOWN, onKeyPress, false, 0, true );
			stage.addEventListener( KeyboardEvent.KEY_UP, onKeyRelease, false, 0, true );
		}
		
		public function onKeyPress( keyboardEvent:KeyboardEvent ):void
		{
			if ( keyboardEvent.keyCode == Keyboard.DOWN )
			{
				downKeyIsBeingPressed = true;
			}
			else if ( keyboardEvent.keyCode == Keyboard.UP )
			{
				upKeyIsBeingPressed = true;
			}
			else if ( keyboardEvent.keyCode == Keyboard.LEFT )
			{
				leftKeyIsBeingPressed = true;
			}
			else if ( keyboardEvent.keyCode == Keyboard.RIGHT )
			{
				rightKeyIsBeingPressed = true;
			}
		}
		
		public function onKeyRelease( keyboardEvent:KeyboardEvent ):void
		{
			if ( keyboardEvent.keyCode == Keyboard.DOWN )
			{
				downKeyIsBeingPressed = false;
			}
			else if ( keyboardEvent.keyCode == Keyboard.UP )
			{
				upKeyIsBeingPressed = false;
			}
			else if ( keyboardEvent.keyCode == Keyboard.LEFT )
			{
				leftKeyIsBeingPressed = false;
			}
			else if ( keyboardEvent.keyCode == Keyboard.RIGHT )
			{
				rightKeyIsBeingPressed = false;
			}
		}
		
		public function onTick( timerEvent:TimerEvent ):void 
		{
			gameClock.addToValue( 25 );
			if ( Math.random() < 0.003 )
			{
				var randomX:Number = Math.random() * 500;
				var newEnemy:Enemy = new Enemy( randomX, 500 );
				army.push( newEnemy );
				addChild( newEnemy );
				/*gameScore.addToValue( 10 );
				sfxSoundChannel = enemyAppearSound.play();*/
			};
			
			if ( Math.random() < 0.003 )
			{
				var randomX:Number = Math.random() * 500;
				var newGood:Good = new Good( randomX, 500 );
				army.push( newGood );
				addChild( newGood );
				/*gameScore.addToValue( 10 );
				sfxSoundChannel = enemyAppearSound.play();*/
			};
			if ( useMouseControl )
			{
				avatar.x = mouseX;
				avatar.y = mouseY;
			}
			else
			{
				if ( downKeyIsBeingPressed )
				{
					avatar.moveABit( 0, 1 );
				}
				else if ( upKeyIsBeingPressed )
				{
					avatar.moveABit( 0, -1 );
				}
				else if ( leftKeyIsBeingPressed )
				{
					avatar.moveABit( -1, 0 );
				}
				else if ( rightKeyIsBeingPressed )
				{
					avatar.moveABit( 1, 0 );
				}
			}
			
			if ( avatar.x < ( avatar.width / 2 ) )
			{
				avatar.x = avatar.width / 2;
			}
			if ( avatar.x > 400 - ( avatar.width / 2 ) )
			{
				avatar.x = 400 - ( avatar.width / 2 );
			}
			if ( avatar.y < ( avatar.height / 2 ) )
			{
				avatar.y = avatar.height / 2;
			}
			if ( avatar.y > 300 - ( avatar.height / 2 ) )
			{
				avatar.y = 300 - ( avatar.height / 2 );
			}
			
			var avatarHasBeenHit:Boolean = false;
			var i:int = army.length - 1;
			var enemy:Enemy;
				while ( i > -1 )
				{
					enemy = army[i];
					enemy.moveABit();
					if ( PixelPerfectCollisionDetection.isColliding( avatar, enemy, this, true ) ) 
					{
						gameTimer.stop();
						avatarHasBeenHit = true;
					}
					if ( enemy.y > 350 )
					{
						removeChild( enemy );
						army.splice( i, 1 );
					}
					i = i - 1;
				}
				
			if ( avatarHasBeenHit )
			{
				bgmSoundChannel.stop();
				dispatchEvent( new AvatarEvent( AvatarEvent.DEAD ) );
			}
			
			if ( gameScore.currentValue >= currentLevelData.pointsToReachNextLevel )
			{
				currentLevelData = new LevelData( currentLevelData.levelNum + 1 );
				setBackgroundImage();
			}
		}
		
		public function setBackgroundImage():void
		{
			if ( currentLevelData.backgroundImage == "blue" )
			{
				backgroundContainer.addChild( new BlueBackground() );
			}
			else if ( currentLevelData.backgroundImage == "red" )
			{
				backgroundContainer.addChild( new RedBackground() );
			}
		}
		
		public function getFinalScore():Number
		{
			return gameScore.currentValue;
		}
		
		public function getFinalClockTime():Number
		{
			return gameClock.currentValue;
		}
	}
}
}
LadyBug88 is offline   Reply With Quote
Old 05-23-2012, 10:02 PM   #2
5TonsOfFlax
.as-hole
 
Join Date: Jan 2010
Posts: 781
Default

Read the errors flash is giving you. They will actually tell you what is wrong. Or at least what Flash thinks is wrong. If you want help, you'll have to tell us what those errors are too. Are they compile-time errors or run-time errors? What line(s) cause them?

You are probably getting an error about a duplicate definition for randomX. That's because it's already declared once in the scope where you tried to add it again. Remove the "var" on the second time. Or use a different variable.
Are you sure you want to put your Goods in the same army as your Enemies? It's not necessarily wrong, but it's something to think about.
You don't need a semicolon following that if statement (or the one before it), but it should be harmless.
5TonsOfFlax is offline   Reply With Quote
Old 05-23-2012, 10:07 PM   #3
LadyBug88
Registered User
 
Join Date: May 2012
Posts: 4
Default

Thank you for a quick reply. I put the good one inside the same as the enemy because i thought it was supposed to go there to be honest. Beginner error I guess. I am going to try what to suggested and see what happens. And the error I am getting is this:

TypeError: Error #1034: Type Coercion failed: cannot convert Good@28416da9 to Enemy.
at AvoiderGame/onTick()
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()
LadyBug88 is offline   Reply With Quote
Old 05-23-2012, 10:15 PM   #4
5TonsOfFlax
.as-hole
 
Join Date: Jan 2010
Posts: 781
Default

As I said, it's not necessarily wrong to put the Goods and the Enemies in the same array. It depends on what you want to do with them. If you want to process them all the same way, putting them in the same container is a good idea. If you want to do something different with each, then you might as well separate them. Like garbage and recycling. If your neighborhood collects them separately, you probably have separate bins that you throw things into so you don't have to sort through them later.

The error you got is because you tried to set a variable of type Enemy to a value of type Good. And you did this because your army contains both Enemies and Goods. It's this line:
Code:
enemy = army[i];
That pulls whatever's in army[i] (which could be either a Good or an Enemy) and puts it in a variable called enemy, which is type Enemy. Since a Good isn't an Enemy, you have an error. Since that loop is to determine if the player has hit an Enemy, you have two basic choices:
1) unify Good and Enemy into some class and handle them identically in this code. Say you make a superclass or interface PlayerHittable which defines a method playerHit. Then you can implement playerHit differently in Good and Enemy and keep all your stuff in one army.
2) separate Good and Enemy into different collections like goodArmy and badArmy. Iterate through them separately and handle the collisions of each differently.
5TonsOfFlax is offline   Reply With Quote
Old 05-23-2012, 10:24 PM   #5
LadyBug88
Registered User
 
Join Date: May 2012
Posts: 4
Default

Thank you so much for pointing that out! I feel some what stupid right now that I didn't see it before. I am going to try separating them. I believe that will make my life easier. I am sure I will be back with another question soon though.
LadyBug88 is offline   Reply With Quote
Old 05-23-2012, 11:23 PM   #6
LadyBug88
Registered User
 
Join Date: May 2012
Posts: 4
Default

You are awesome! It finally works! Well sort of. I have go get the good to gather points and not end the game and I have a scrolling background I want to add. I just wanted to make sure you know knew how thankful I am for your help! I was about to pull all of my hair out. If I could high five you I would.
LadyBug88 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 04:36 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.