The Armor Games website will be down for maintenance on Monday 10/7/2024
starting at 10:00 AM Pacific time. We apologize for the inconvenience.

ForumsProgramming ForumRPG Code, go look!

9 4766
Secretmapper
offline
Secretmapper
1,747 posts
Nomad

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&quot
}
}
//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&quot
}
}

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!&quot
//TODO:I freaking dodged it!
}
else {
enemy[0]=-player[3]
trace ("Attack!&quot
}
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&quot
}
}
//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!&quot
//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&quot;
endTurn ();
}
else if (randDodge<eSpells[1]) { //execute Stun!
trace ("Stun Executed&quot;
endTurn ();
}
else {
trace ("Else&quot;
player[0]=player[0]-enemy[3]
endTurn ();
}
}
public function endTurn () {
if (turn==1){
turn = 2
turnCheck ();
}
else if (turn==2){
turn = 1
turnCheck ();
}
}
}
}

  • 9 Replies
ExplosionsHurt
offline
ExplosionsHurt
248 posts
Nomad

Wow. This was your 1st AS3 code?

Nice. I can't really say anything though, since I don't know what it really means... =='

master565
offline
master565
4,104 posts
Nomad

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.
Dannydaninja
offline
Dannydaninja
948 posts
Nomad

Expect many people to steal this code.
If I was myself 3 months ago and I was learning AS3, I would have already stolen all of this.

As for helping you improve the code, sorry, I only know the basics of AS2 :P

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

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

That's okay. Thank you for reading it.
PixelSmash
offline
PixelSmash
566 posts
Nomad

Hmm, turn-based battle framework? Looks quite decent!

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

Cheers for posting!

Ghgt99
offline
Ghgt99
1,890 posts
Nomad

This is great. Way better then what I can do, even though I have been trying on and off to learn AS3 for a few months.

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.

Otherwise it is very good! =D

PixelSmash
offline
PixelSmash
566 posts
Nomad

Gh... that's only a matter of formatting, he doesn't have the { on the line after a function declaration either, so it's perfectly allright

Secretmapper
offline
Secretmapper
1,747 posts
Nomad

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!
Ghgt99
offline
Ghgt99
1,890 posts
Nomad

he doesn't have the { on the line after a function declaration either, so it's perfectly allright


Ohh yeah. My bad. Sorry, I haven't had a good look at AS3 code in a while. =P
Showing 1-9 of 9