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);//(FORodge!)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);//(FORodge!)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 (); } } } }
This is really good. I'm not to good with AS3 myself but instead of
var pStates:Array = [0,0] //0=Poison, 1=Stunned,
You might want to have a separate boolean for each status. If you use an array you might not be able to give one person to status effects such as one person being both stunned and poisoned.
You might want to have a separate boolean for each status. If you use an array you might not be able to give one person to status effects such as one person being both stunned and poisoned.
Hmm, I'll think about that. Thanks. I might also consider making it an object. or something.
Expect many people to steal this code.
No worries, technically It's just the battle framework and some operations. Most of the funcs use the trace command.
As for helping you improve the code, sorry, I only know the basics of AS2 :P
What I'd suggest looking at next is either events or custom classes, ephasis on custom classes. This way you won't have to represent the player with an array, but you can have a spiffy Player class which can handle that internally.
Especially since you'll want to extend this code, I can guarantee it's going to cost some time to remember which array index it was you needed. If you've got a Player class, you can simply call a function you made for the life, ie. player.getLife().
I just might post some of my own code as well... not everything though, since 3249 lines (excluding library packages) are a bit much to read through :P
What I'd suggest looking at next is either events or custom classes, emphasis on custom classes. This way you won't have to represent the player with an array, but you can have a spiffy Player class which can handle that internally.
That is a planned feature. The reason I made arrays in this class is to immediately test if "battle works"
I just might post some of my own code as well... not everything though, since 3249 lines (excluding library packages) are a bit much to read through :P
Share a link to my messenger!
The only problem I see is where it says package { at the very beginning, it should be package then the { under the p of package.
What PixelSmash said.
Thanks for Reviewing...
Anyways, I'm quite busy now. With the Flash Version of Ultimate Zombie Teamwork Forum Game pressing up, I might just stop trying to learn AS3. (I'm the artist of the team, and I just tried making my own code. And we're making some changes.)
Well, bye, Let's all support other AS3 programmers out there!