PDA

View Full Version : [AS3] RPG code - things to improve?


Secretmapper
04-28-2011, 02:09 PM
It's not finished yet, and I know that I might have done something really wrong. Being a flash artist, I liked the idea of writing code in AS3 too. So, I guess I just sat down and write (some sort of) RPG Code.

I did it down and dirty, so expect some serious bull**** in it XD

Tell me things that can make me improve it. Like, I might be missing some thing important that would have made this piece of code better.

Warning:This is my first AS3 code, and I didn't follow any tutorials too, so don't go too hard on me, please :)

package {
import flash.display.SimpleButton;
import flash.events.MouseEvent;
import flash.display.MovieClip;
import flash.display.DisplayObject

public class BattleScreen extends MovieClip {

public static var player:Array = [16,1,2,3,4,5,6]; //Player 0=Life, 1=Mana, 2=Defence, 3=Min Damage, 4=Max Damage, 5=Poison Damage, 6=Dexterity
var enemy:Array = [5,4,3,6]; //Enemy 0=Life, 1=Mana, 2=Defence, 3=Min Damage, 4=Max Damage 5=Poison Damage
var eSpells:Array = [0,1,2,3]; //Enemy Spell cast chance 0=Poison, 1=Stun
var pStates:Array = [0,0] //0=Poison, 1=Stunned,
var eStates:Array = [0,0] //0=Poison, 1=Stunned,
var acheck:Array = [0,0] //0=Poison check 1=Stun check
var randDodge:int = 1 //Player Dodge from enemy
var randETurn:int = 1 //Pick Enemy Turn
var turn = 1 //1=Player, 2=Enemy
//runs turn check
public function BattleScreen () {
turnCheck ();
}
//checks who's turn it is
public function turnCheck() {
trace ("Player Life:"+player[0])
trace ("Enemy Life:"+enemy[0])
if (turn==1) {
pStateCheck ();
}
else if (turn==2) {
eStateCheck ();
}
else {
trace ("turn is neither 1 nor 2")
}
}
//Player!
//checks state of player
public function pStateCheck() {
if (player[0]>0){ //checks if player is still alive
if (pStates[0]>0 && acheck[0]==0) { //If player is poisoned AND Poison check is not done yet.
pPoison ();
}
else if (pStates[1]>0 && acheck[1]==0){//If player is stunned AND stun check is not done yet.
pStun();
}
else {
pTurn();
}
}
}
//Player State Action!
//if player is poisoned. Decrease Life with Enemy Poison Damage, decrease Poison State number, AND Poison check is done.
public function pPoison () {
player[0]=player[0]-enemy[5]
pStates[0]=player[0]-1
acheck[0]=1
pStateCheck();
}
//if player is stunned. Decrease Stun State number, AND STUN check is done. TODO:Stun, end turn
public function pStun () {
pStates[1]=pStates[1]-1
acheck[1]=1
pStateCheck();
}
//Player Turn!
public function pTurn () {
if (turn==1){
attackButton.addEventListener( MouseEvent.CLICK, onClickAttack );
}
else if (turn==2){
trace ("wait for your turn")
}
}

public function onClickAttack(mouseEvent:MouseEvent ):void {
var randETurn = Math.floor(Math.random() * 101);//(FOR:Dodge!)get a random number between 0 and 100.
if (randETurn<=player[6]) {
trace ("It Dodged it!")
//TODO:I freaking dodged it!
}
else {
enemy[0]=-player[3]
trace ("Attack!")
}
endTurn ();
}

//Enemy!
//checks state of enemy
public function eStateCheck() {
if (enemy[0]>0){ //checks if enemy is still alive
if (eStates[0]>0 && acheck[0]==0) { //If enemy is poisoned AND Poison check is not done yet.
ePoison ();
}
else if (eStates[1]>0 && acheck[1]==0){//If enemy is stunned AND stun check is not done yet.
eStun();
}
else {
eTurn();
}
}
else {
trace ("You killed it")
}
}
//Enemy State Action!
//if enemy is poisoned. Decrease Life with Player Poison Damage, decrease Poison State number, AND Poison check is done.
public function ePoison () {
enemy[0]-player[5]
eStates[0]=-1
acheck[0]=1
eStateCheck();
}
//if enemy is stunned. Decrease Stun State number, AND STUN check is done. TODO:Stun, end turn
public function eStun () {
eStates[1]=-1
acheck[1]=1
eStateCheck();
}
//Enemy Turn!
//check if player dodges.
public function eTurn () {
var randETurn = Math.floor(Math.random() * 101);//(FOR:Dodge!)get a random number between 0 and 100.
if (randETurn<=player[6]) {
trace ("I dodged it!")
//TODO:I freaking dodged it!
}
else {
ePickTurn ();
}
}
public function ePickTurn () {
var randDodge=Math.floor(Math.random() * 101)
if (randDodge<eSpells[0]) { //execute Poison!
trace ("Poison Executed");
endTurn ();
}
else if (randDodge<eSpells[1]) { //execute Stun!
trace ("Stun Executed");
endTurn ();
}
else {
trace ("Else");
player[0]=player[0]-enemy[3]
endTurn ();
}
}
public function endTurn () {
if (turn==1){
turn = 2
turnCheck ();
}
else if (turn==2){
turn = 1
turnCheck ();
}
}
}
}

jooniya
04-28-2011, 08:41 PM
My advice would be to read a book, website, magazine, or anything at all on object-oriented programming. Also, a GREAT book for learning AS3 is Learning ActionScript 3.0 by Rich Shupe and Zevan Rosser. Another good one is Essential Actionscript 3 from O'Reilly publishing. You'll realize how you can refine your code you posted after reading a bit. GOOD LUCK!

rrh
04-28-2011, 10:36 PM
Maybe consider objects instead of arrays in some cases. For example:

public static var player:Array = [16,1,2,3,4,5,6]; //Player 0=Life, 1=Mana, 2=Defence, 3=Min Damage, 4=Max Damage, 5=Poison Damage, 6=Dexterity

could be:
public static var player:Object = {life: 16, mana : 1, defence : 2, ... etc };

and then you can reference them with
player.life instead of the harder to read player[0]


Or go full-out object oriented, define a Player class, and you can even give the Player class its own functions.

Secretmapper
04-29-2011, 04:21 AM
Game Framework:
Document Class pulls in Options Screen
Options Screen pulls in Nav Screen
Nav Screen pulls in Game Screen
Game Screen pulls in Win/Lose Screen
Goes back to Nav Screen

I've been thinking of ways to make the transitions smoother, because it's kinda wierd when a "screen" just appears out of nowhere. :p