hey im fairly new to AS3 and flashpunk, but im trying to make a simple space shooter with a player whos ability updates based on score, aka speed increase, more bullets, etc, when the score reaches a certain point. but so far i cant get it to work, is there anyone who has done this/may be able to help m e do this?
How to make updates based on score?
I understood that you have a variable wich is the score and you want to make improvements depending of it, right?
Also it’s important for the design if it can be reseted or it always increses (or stays the same), if you want the upgrades be always the same or you want to change them every time, etc. This little things are the key to make it in one way or another.
The most basic way I can think is to store the score on a static class and make a multiplier, something like this:
//initialize the power up as 1
int powerUp = 1;
...
//to avoid useless IF statement, try to put this code after by any means the score is increased, or if it's impossible use on the update
if(GameObject.score > 500){
powerUp = 1.1; //10%
}
if(GameObject.score > 750){
powerUp = 1.2; //20%
}
...
//and a example of using would be something like this
//HSpeed as Horizontal Speed
x += HSpeed * powerUp;
I’ve thought about this way. It’s quite easy to make and allows the usual things you want to include.
public var score:Number = 0; //or uint or int depending of the game
private var _powerLvl:uint = 0;
//in the update function
public function update():void
{
//[...]
//The power ups
if (_powerLvl <= 0 && score >= 100)
{
//apply first power up (can be multiple changes)
_powerLvl++;
}
if (_powerLvl <= 1 && score >= 200)
{
//apply second power up
_powerLvl++;
}
//[...] //add more powerups
//the power downs. If the score is always increasing, do not use this. Also notice that the order is inverted.
//[...] //add more power downs
if (_powerLvl >= 1 && score < 200)
{
//revert second power up
_powerLvl--;
}
if (_powerLvl >= 0 && socre < 100)
{
//revert the first power up
_powerLvl--;
}
//[...]
}
This code allows to upgrade multiple times in the same frame. If it’s impossible for the player do so, you should use if ... else if ... else if
instead of if ... if ... if
to improves the efficiency.
right now i have
if (_score.score >= 5)
{
_MyEntity.hasDoubleShoot = true;
}
in my world.as and
.
public var hasDoubleShoot:Boolean = false;
override public function update():void
{
if (Input.check(Key.LEFT)) { x -= 5; }
if (Input.check(Key.RIGHT)) { x += 5; }
if (Input.check(Key.UP)) { y -= 5; }
if (Input.check(Key.DOWN)) { y += 5; }
if (Input.check(Key.SPACE))
{
shoot.play();
}
if (Input.released(Key.SPACE))
{
if (hasDoubleShoot)
{
world.add(new Bullet(myWorld(world).generateBulletPath(3), x - 5, y));
world.add(new Bullet(myWorld(world).generateBulletPath(3), x + 5, y));
}
else
{
world.add(new Bullet(myWorld(world).generateBulletPath(3), x, y));
}
}
}
in my entity, but i keep getting this error: 15 Error: Access of possibly undefined property score through a reference with static type gScore.
In this line
if (_score.score >= 5)
what class _score is?
I believe that _score is a var
of gScore
but that you have never made an instance of gScore
.
Try to put the word static
before the score property on the gScore
class.
Like this:
class gScore extends Object{
public static var score:int;
}
Also change
if (_score.score >= 5)
to
if (gScore.score >= 5)
extends Object
does nothing.
Plus, this error is saying that you are trying to change a non-existing parameter.
so i added the static bit and changed it to gScore.score, and now it says there are no errors, but its not generating 2 bullets like its supposed to after the score hits five…
Is hard to tell without having the entire code; but i feel that, at this point, the best thing you can do is to think how you want to structure the code (really important!) and rewrite it from scratch.
Well, please do note that you’re generating your double shot in the same frame, on the same place. So, you will not see 2 bullets, but there are 2 bullets on the same place.
But as Copying said, it’s hard to tell anything without having the code that increases the score.