PDA

View Full Version : Function "player" in a game


Aereena
12-28-2003, 12:47 PM
Hi all!

Having a problem I just cannot solve, but it's probably really simple, but I suck at ActionScript. I'm in a group that has an assignment to create a game using Flash and ActionScript. This game is a boardgame for two players, and I'm the one who's supposed to create the function for "player".
This function should be able to recieve each of the players names from a textfield and then save the name in a variable as well as put it on the gameboard.
The function is also supposed to keep track of how many treasures (out of 5) that has been found by a player as well as of how many dangers (also out of 5) that the player have stumbled upon.
Then the function should also keep track of how many objects (out of four) that has been picked up by the player.

So, if anyone has got an idea as to how to solve this, I would be really greatfull, cause my mind is stuck.

Ciao!
/Aereena

Cota
12-29-2003, 05:02 AM
Have you considered using arrays to hold that info for each player. There are a few ways to approach that idea. Or if you dont need to track it in too much detail you could always just use a plan ole variable.

Aereena
12-29-2003, 09:20 AM
I think it's enough using variables what I have so far looks something like this:

function Player() {
}

function register(name) {
this.name = name;
}

register.prototype.setName = function() {
_level2.inputText.text = _level0.inputText.text;
}


var points = 0;
var traps = 0;
var objects = 0;

if(trap == true){
traps++;
}

if(traps == 4) {
.text = "GAME OVER";
}

if(treasure == true){
points = points + 1000;
}

if(object == true){
objects++;
}


if(objects == 4) {
.text ="You may proceed to collect the supertreasure";
}


So I know this is not complete, and probably has some errors, anyone with any suggestions?

Cota
12-29-2003, 01:18 PM
Will this script be running all the time, or just at specific points? Does all this occur on the main time line or within MovieClips?

dzy2566
12-30-2003, 09:09 AM
I would create an object to hold all that information, similar to what Cota suggested.
//create a general game function, this will save you time if you
//decide to add or change some of the values that players,
//enemies, or whatever, share

game = new Object();
game.maxTreasures = 5;
game.maxDangers = 5;
game.maxObjs = 4;
game.players = new Array(); //for looping through all players

//I think it's cleaner if you make the variables (you used var points
//var traps, and var objects) properties of the player object

function Player(){
this.pName;
this.treasures = 0;
this.dangers = 0;
this.objs = 0;
game.players.unshift(this); // add the player to the game.players array
}

Player.prototype.updateTreasure = function(){
if(this.treasures < 5){
this.treasures++;
}else {
this.treasures = 5;
//set supertreasure text here
}
}

//you can use similar update Methods for the Player class

//when your game starts, create two instances of your player class
//one for each player. (you may even want to change the constructor
//function by adding an argument and pass the player name
//to the new instance of Player when it is being instantiated, because
//the name is the only property that will vary upon instantiation

player1 = new Player();
player2 = new Player();
No, these scripts do not have to be running all of the time. You just need to know when to check them and what you want to do with given values. For instance, you might just want to check the properties of each player at the beginning of each turn, and do something accordingly.//begin turn
//check players' treasure count
for(i=0;i<game.players.length;i++){
if(game.players[i].treasures == 5){
//do something
}
}
When working with games, work slowly and in small chunks. If you are very careful about tracking information, you'll be fine. Just keep in mind what you need to know about something, when you need to know it, and what you want to happen when you find x,y, or z. Baby steps. Hope this helps. Shout if you got more questions.

Aereena
12-30-2003, 03:50 PM
Thanks, that was a great help. Never worked with ActionScript before in my life, so feeling a bit lost...

Hope you have a happy new year!

dzy2566
12-30-2003, 04:07 PM
Well thank you. Happy New Year to you too! If you have questions, post or feel free to email me. Take care :)