Top-Down RPG Shooter — Part 3 — Player Rotation

Hey everyone! Here’s a super brief tutorial. Just to hold you over for another week while I write the next big one ;)

The Code

We’ll be modifying the Player.as class so that the player rotates in the direction of the mouse. It only takes a few lines of code. (You can actually do it in one line of code, I’m just spelling everything out to make it nice and simple.)

Put all of these lines inside the bottom of the loop function in the Player.as class.

//calculate these values, which we will use to determine the angle we need to rotate to
var yDifference:Number = stageRef.mouseY - y;
var xDifference:Number = stageRef.mouseX - x;
//this constant will convert our angle from radians to degrees
var radiansToDegrees:Number = 180/Math.PI;
//this final line uses trigonometry to calculate the rotation
rotation = Math.atan2(yDifference, xDifference) * radiansToDegrees;

Continue reading

Top-Down RPG Shooter — Part 2 — Player Movement

Welcome back to the second part of my “Top-Down RPG Shooter” flash game tutorial. In the last part, we set up a new project and linked it to an external Document Class, and we added the Player to the stage. In this tutorial, we’re going to program keyboard controls to move the player.

Step 1: KeyObject.as

We are going to make use of a really great open-source class called “KeyObject.as“. This class was written by a talented developer named senocular. It provides a really simple but powerful way to check which keyboard keys are pressed.

Copy and paste this class into a new .as actionscript file, and save it as “KeyObject.as” in the same folder as your main project:

package {

	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.Proxy;
	import flash.utils.flash_proxy;

	/**
	 * The KeyObject class recreates functionality of
	 * Key.isDown of ActionScript 1 and 2
	 *
	 * Usage:
	 * var key:KeyObject = new KeyObject(stage);
	 * if (key.isDown(key.LEFT)) { ... }
	 */
	dynamic public class KeyObject extends Proxy {

		private static var stage:Stage;
		private static var keysDown:Object;

		public function KeyObject(stage:Stage) {
			construct(stage);
		}

		public function construct(stage:Stage):void {
			KeyObject.stage = stage;
			keysDown = new Object();
			stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
		}

		flash_proxy override function getProperty(name:*):* {
			return (name in Keyboard) ? Keyboard[name] : -1;
		}

		public function isDown(keyCode:uint):Boolean {
			return Boolean(keyCode in keysDown);
		}

		public function deconstruct():void {
			stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
			stage.removeEventListener(KeyboardEvent.KEY_UP, keyReleased);
			keysDown = new Object();
			KeyObject.stage = null;
		}

		private function keyPressed(evt:KeyboardEvent):void {
			keysDown[evt.keyCode] = true;
		}

		private function keyReleased(evt:KeyboardEvent):void {
			delete keysDown[evt.keyCode];
		}
	}
}

How do we use this class? Basically, we’re going to create an instance of it called “key” in our Player class (or wherever we need to access the keyboard controls). Then in that class, we can check the Boolean value of the keyObject’s isDown() function for specific keys. We can refer to keys by their unique keyCode. For example, if key.isDown(65) returns true, it means that the “A” keyboard key is currently being pressed.

Continue reading

Top-Down RPG Shooter — Part 1 — Setting Up

Hello everyone! If you’re reading this, that means you haven’t given up on AS3GameTuts, despite my year-long hiatus. Thanks for your patience!

Today I’m going to start up a brand new tutorial series that I’m really excited about. How to make a top-down RPG shooter game! This tutorial is going to be slightly faster-paced than my previous tutorials. If you haven’t programmed before, I’d recommend starting with my Pong series, and then proceeding with the Platformer before you attempt this. We will be coding using AS3 in external .as files, instead of using the timeline.

What is a Top-Down RPG Shooter?

Personally, I think this is a pretty awesome game genre. I find these types of games to be genuinely fun to play.

In case you’re unfamiliar with the genre, let’s break it down:

Top-Down

Top-down refers to the perspective of the game. The world is seen from a bird’s-eye-view, and the player can move around horizontally and vertically. Prominent examples include early titles in the Legend of Zelda and Pokemon series. These games both use tile-based maps, which our game will not, for simplicity’s sake. Instead, we will draw each map individually, giving the game a more unique, hand-drawn look. (You are, of course, welcome to modify the game to use tiles, but I won’t be doing it in this tutorial.)

Our game won’t have a scrolling map. Instead, our map will be built from a series of screens that the player can walk through. When the player walks off one edge of the screen, the map will flip to the next screen.

top down rpg shooter game pokemon map

Pokemon Ruby and Sapphire feature great tile-based top-down maps.

Continue reading