Question

I am developing a fairly basic objective class space-shooter style flash game in AS3. The game originally contained 1 enemy target which had random variable parameters, such as speed and health, but the combined score counter remained predefined, one.

I have since expanded my game to where I have several enemy targets each having their own class, thus, they have their own variable parameter values. I also took the liberty of increasing the combined score, to reflect the difficulty of each enemy target.

The problem that I am running in to, is that my original line of code for combining the score counter is still set to "score++;", which just adds a value of 1 each time that "if statement" occurs.

My question, is how can I get my score counter to increase the score based on the value assigned for each respective class?

Here are a few bits of relevant code, with notes to help explain my view:

"If statement" (housed in my Level class) for when an enemy target is killed:

// if the bullet is touching the ship
if (MyMaths.hitTest(sh, bullets[bcount])) {
sh.health--; // lose 1 heath point
score++;
removeChild(bullets[bcount]); // remove the bullet from the screen
bullets.splice(bcount, 1); // remove the bullet from the list
}

Ship1 (housed in my Ship1 class) score value

public function Ship1() {
score=1; // set point value
}       

Ship2 (housed in my Ship2 class) score value

public function Ship1() {
score=2; // set point value
}   

Ship3 (housed in my Ship3 class) score value

public function Ship1() {
score=3; // set point value
}   

I am just not sure if I would need to make a new function for combining the score value, and have my "score++;" pull from that, and how would I go about that?

I feel that this is a question that many fellow Objective-Oriented programmers would deem relevant, as combining a variable from multiple classes to a single counter is not as easy as it may seem. Also, this could be used in a wide range of other developer niches, apart from game developing -- creating a database of multiple models of computers in a network, and calculating a "total completed" field, after refreshing the workstations with a new OS or required software.

I really would appreciate anybody who could offer their expertise, opinion and/or words of encouragement.

Thank you very much, Alex

Was it helpful?

Solution

Why not have a Singleton that each of the ships communicate with instead of doing it that way.

Whenever you increment the score:

ScoreManager.getInstance().incrementScore(ShipX.getScoreDelta())

Where getScoreDelta() would be reset to 0 after each collection.

Then when the game is done:

ScoreManager.getInstance().getCurrentScore();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top