Flash – Case 7

Another case, this time I’ve got to add some enemies to my game.

So! Just three cases to go until I have to have a game finished, I feel that I lack a lot of experience and understanding necessary for creating a final game with Flash and ActionScript 3. I’ve needed a lot of help from “the outside” in the previous cases, and this case was no different. I got a lot of help from my neighbour and from Learning ActionScript 3: The Beginner’s Guide. Mostly input on understanding why and how to use several classes, mostly for structure.

Case 7: Time to add some enemies (or friendly players) to your game. You basically have to understand the concept of using object oriented programming (OOP) and adding artificial intelligence (AI) to one objects or more in this case – to succeed with this case. You’ll be introduced to creating a new class that will communicate with your already created “main” class.

Reflections: I understand by now that I, should, devote even more time to understanding and learning the very fundamentals of programming in ActionScript 3.0. I need to devote time I feel I do not have at all, it is a struggle when I do not really aim to be a programmer – programming isn’t why I mainly chose this education, I’m more creative than technical. This mental lock I have in my head has made me quarrel and reflect a lot about programming, it’s not making it easier to understand and finish every case from week to week either. I’m also having a hard time learning everything I need to learn to complete every case (Not to speak about learning everything not considered in each case. The cases only cover the very vital and necessary functions for the final game.), I guess it’s just a matter of finding relevant information whether it’s on the internet through forums or tutorials, educational books, or fellow students.

If I have learned anything over the few past months in this course, it is that I am not really suited for programming, although I try my best I feel I can’t get further than what I’ve managed this far. I don’t want to give up just yet, but I’m not far away from doing so, I do at least want to finish my game – and to fulfill the idea and concept I have in my head. I guess the game idea I had in the start was too ambitious for me. So next time I’ll need to find out what the idea I have in my head requires from me in regards to ActionScript and Flash in itself.

In this specific case I’ve learned small fragments of how to add artificial intelligence to another object for the game. In my game, the “other objects” are enemies, specifically scripted to follow the player character wherever it moves. I’ve been introduced to Object Oriented Programming (OOP), how to make another class (another .as file) communicate (import/export variables, functions, etc.) with a main-class. I’ve also learned how to add a roughly working “camera” (moving a Movie Clip when there’s input, imitating movement) in a sidescrolling game.

Difficulties: I had a lot of difficulties or problems I did not understand before starting this case. What I first did was chopping the problem into smaller pieces so it would be easier to work with. I had three questions I needed answers to

  • What kind of code do I need to make a function randomly choose a number assigned to three different enemies? I want the game to have a different feeling to it each time a player starts the game.
  • How can I add spawnpoints – where the player character walks past and an enemy “spawns” <insert number> coordinates to the right? I want the enemies to spawn when the player passes a certain object or point in my level. One spawnpoint should spawn only one enemy each.
  • How can I add artificial intelligence to the enemies? I want them to follow the player character along the x-coordinates.

Solution: Here’s a bit of code, mainly the functions I added to solve the specific difficulties that this case introduced me to (bullet points above). I will try my best to explain parts of the code, do note that I am quite inexperienced with scripting in Flash, so you’re welcome to comment my errors if you see any.

This is the code I’ve used for randomly spawning different enemies when the player character walks through a specific x-position in the level.

var RNG=(Math.floor((Math.random() * 50)) %3) // This code is used to "randomly" run through 50 values until 0, 1, or 2 is returned. The Math.random function loops through values from 0.1, 0.2, 0.3 and so forth. Any value from 0 to 0.9 is considered "0", any value from 1 to 1.9 is considered "1", and any value from 2 to 2.9 is considered "2".

			if (RNG == 0) // If the RNG variable returns a value that is considered "0" - it uses the function called createEnemy1.
			{
				createEnemy1();
			}

			else if (RNG == 1) // If the RNG variable returns a value that is considered "1" - it uses the function called createEnemy2.
			{
				createEnemy2();
			}

			else if (RNG == 2) // If the RNG variable returns a value that is considered "2" - it uses the function called createEnemy3.
			{
				createEnemy3();
			}

This is what I have coded for the “createEnemy” function – it specifies what should happen when “createEnemy1/2/3″ is referred to.

private function createEnemy1()
		{ // start createEnemy1
		attackFrame = "rockAttack";
		walkFrame = "rockWalk";
		idleFrame = "rockIdle";
		} // end createEnemy1

		private function createEnemy2()
		{ // start createEnemy2
		attackFrame = "scissorAttack";
		walkFrame = "scissorWalk";
		idleFrame = "scissorIdle";
		} // end createEnemy2

		private function createEnemy3()
		{ // start createEnemy3
		attackFrame = "paperAttack";
		walkFrame = "paperWalk";
		idleFrame = "paperIdle";
		} // end createEnemy3

It basically tells the script to find Movie Clips which have the correct instance names for each enemy. I’ve made a sprite for the enemies, where for example the scissor enemy have three frames which consists of Movie Clips named “scissorAttack”, “scissorWalk”, and “scissorIdle” (these MCs contain the animation for said name).

This is the function I’ve used to specify where the enemies should spawn when the player passes specific “spawn points” throughout the level.

for each (var spawnObject in spawnArray)  					// Sends the functions of the loop into every spawnpoint (spawnObject) added to the spawnArray
			{ // start for each spawnObject in spawnArray
				if (spawnObject.x < handChar.x && spawnObject.spawned == false)
					{
						var newEnemy = new Enemy();						// Tells Main.as what Enemy.as is
						newEnemy.x = spawnObject.x+100;				        	// newEnemy spawn 100 x-coordinates to the right for the spawn point
						newEnemy.y = 300;								// newEnemy spawn 300 on the y-coordinates
						enemyArray.push(newEnemy);						// Pushes the enemy into the enemyArray
						addChild(newEnemy);								// Adds either of createEnemy (Enemy.as) into the stage
						spawnObject.spawned = true;						// Checks if the Enemy has been spawned on a spawnpoint, if yes, then the spawnpoint is set to true and disables the spawning of new enemies
					}
			} // end for each spawnObject in spawnArray

“spawnObject” is a Movie Clip directly on the stage in my Flash file. spawnArray is obviously an array, so I’ve pushed every spawnObject into the array in the top of my code, like this “spawnArray.push(spawn1);” where “spawn1″ is a graphical Movie Clip on my stage with the instance name called “spawn1″.

This is the function I’ve used for the artificial intelligence on my enemies.

public function Chase(handCharx, handChary)
		{ // start public function Chase
			if (handCharx < this.x-20)                    // if player (handChar) is lesser (to the left on the screen) than the enemy, enemy should turn to the left and follow the player.
                        {
                                this.x -= speed;
                                this.scaleX = -1;
                                gotoAndStop(walkFrame);
                        }
                        else if (handCharx > this.x+20)               // if player (handChar) is larger (to the right on the screen) than the enemy, enemy should turn to the right and follow the player.
			{
				this.x += speed;
				this.scaleX = 1;
				gotoAndStop(walkFrame);
			}

Result: Here’s the visual result, and here is the code behind the enemies. I haven’t added the main-class, but it involves some changes related to the newly added class.

I’ll write some more words about the solution later.


About this entry