Copy link to clipboard
Copied
Hi guys...
I have been thinking for a while already, but I cant figure out why this doesn´t work...
I have a script written to be "killing" enemies and each time an enemy gets killed, you get 10 points.
Now I tried to set it up to be levelling up each time you reach 200 points (at 200: lvl 2; at 400: lvl3; at 600: lvl4 and so on) but I was unable to do so.
Can you show me where I am making the mistake?
Here is what I have:
var level:int = 1;
var score:int = 0;
var lvlup:int = 200;
var updlvl:int = (lvlup * level); <<<< With this I am trying to use the var updlvl to have my level up each time I have added 200 points (when var level is 2, var
updlvl will be 200 * 2 = 400
UpdateFields();
function removeEnemy(){
updateScore(10);
UpdateFields();
if (enemy.parent) {enemy.parent.removeChild(enemy)};
recycleEnemy();
}
function updateScore(amount:int):void {
score += amount;
if(score < 0) score = 0;
if (updlvl = score) updateLevel(1);
}
function updateLevel(amount:int):void {
level += amount;
}
function UpdateFields():void {
score_txt.text = String(score);
level_txt.text = String(level);
lives_txt.text = String(lives);
}
Thanks a lot!!!
Copy link to clipboard
Copied
I don't see anywhere in your code where you update the updlvl value. You only assign it once at the beginning. I can't be sure but I think you want to have that recalculated inside the updateLevel function...
function updateLevel(amount:int):void {
level += amount;
updlvl = (lvlup * level);
}
Copy link to clipboard
Copied
I thought with the
var updlvl:int = (lvlup * level);
the var updlvl gets updated everytime the level goes up...
However, I forgot to say, in the code written above, what I get is:
Each enemy I kill, my score gets updated by 10 but also my level gets updated by 1 (when it suppose to wait until it is at 200 to do so)
What is wrong?
Also, if I put it the other way around when I kill my first enemy the score goes to 200, the level goes to 2 and each new enemy the score remains at 200 but the level goes up by 1 each time
Copy link to clipboard
Copied
That one line of code by itself will not automatically update a value. It only assigns the value the one time that line of code executes. The only way to have it execute more than once for the way you show it is to move to a different frame and return to that frame, but then you would be restting the value based on resetting the level value.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now