How to Make My score Board works - actionscript-3

i have been trying to make a score board, which will start counting from when the helicopter start running till when it falls cause of crashing with a enemie, but the score is not running or doing anything would you tell me whats the problem of my code?
http://prntscr.com/2aospf
that is the game, which is in frame 3, my button of play on frame 2 and the pre loader on frame 1
var score:"0";
//scoreCounter is the instance name of the dynamic text box
function updateScore():void{
score += 1;
scoreCounter.text = score.toString();
}
what im doing wrong? it doesnt start counting when the helicopter runs, thanks.

Are you calling your function whenever the score has to update? If the objective is to purely not hit an enemy and the score therefore based on time, you should call the function at the start of the frame and put a while loop around the counting and updating of the scoreCounter within the function to make the score go up through time.

Related

Creating a score variable and update it when an event occurs

I have been making a random dice roll game in Flash CS5.
When a user rolls the dice, a random face is thrown and that value is added to score. The concept of the game is, the player must get 20 points in 10 tries. I didn't use external classes, instead I used the code in timeline.
The problem I have is the score is not added up and the triesLeft variable is not decreasing by one. After solving this problem, I need if/else statements to end the game. I would be happy if you give the code for this too.
This is the SWF file of my project. If you need the source file I will upload it.
It seems to me like you are assigning the value of the dice roll to the score and not adding it to the score. Also it seems like you are just assigning the rolls left to 9 and not decrementing it.

How to update as3 code faster?

Is it any way possible to update my code (as3) more than 1 time per frame? it doesnt have to do anything visual. I used before event.ENTER_FRAME. If there's a way to do so, could someone explain or give a link how to do that. Thanks.
You can use a timer to execute code at whatever rate you want :
// first parameter for Timer is duration in milliseconds 1000ms = 1 second.
var timer:Timer = new Timer(10,0);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();
function timerHandler(e:TimerEvent):void
{
// do something;
}
However, I'd question why you want to do this and determine if it's really needed.
If you app is running at 30fps you can set it to run at 60fps, then ENTER_FRAME will be called twice as much, or if you use a timer to update you can reduce its delay by half.

How to prevent cheating on a flash game by slowing down cpu?

I developed a flash game in AS3, similar to Music Challenge on Facebook, where you need to guess from a selection of songs, wich one is the one playing, score is based on how many did you guess in 30 seconds, this timeout, is handled by an instance of Timer.
Everything works good, but someone pointed out that by reducing cpu performance, timer goes slower, allowing users to play longer, guess more songs and ultimately get a higher score, then I did some research, and read that to prevent this, I need to use systems time, the problem is, I don't know how to do this.
Any help is very much appreciated.
You could have a repeating timer with a short interval, 100ms or so, and check new Date().getTime() on each tick. By default, a new Date has the current system time, and getTime() gives you the time in milliseconds for a simpler comparison. Compare that with a start time and end the game once at least 30 seconds have passed. Something like:
import flash.utils.Timer;
import flash.events.TimerEvent;
var ticker:Timer = new Timer(100, 0);
ticker.addEventListener(TimerEvent.TIMER, checkElapsedTime);
var startTime:Number = new Date().getTime();
ticker.start();
function checkElapsedTime(e:TimerEvent):void {
var now:Number = new Date().getTime();
if (now - startTime >= 30 * 1000) {
// End the game and stop the ticker.
ticker.stop();
ticker.removeEventListener(TimerEvent.TIMER, checkElapsedTime);
}
}
There is obviously still some slight lag if the timer is very slow, so just in case I would run the same check when an answer is submitted and ignore it if it was after the deadline.
Finally, to check if a user has changed their system clock you could store the previous now and make sure the new now is always larger.

AS3: Combining counter (score) variable from multiple objects in different classes

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
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();

Time Slow/Speed Up Power Up

I'm creating a collecting game, and I want to create a time slow/speed up powerup.
Any ideas on how to do that in Flash/AS3?
One way I thought of was simply changing the frame rate. I can slow down the frame rate. But when I try to increase the frame rate beyond 60, Flash caps it at 60.
Thank you in advance for your help.
I like to do time-based movement instead of frame-based movement for better consistency. The general concept is to check the amount of time passed between frames and base movement on that instead of frames which can alternate (e.g. you can have 60FPS for a bit and then it slows down to 30FPS). You can do a simple calculation based on time passed for movement, for instance player.x += player.speed * timeDiff but that can result in odd situations if the time passed between frames happens to be really large (for instance, the player can end up missing lots of collisions since you are moving him in one large movement). Instead, I like to use a game loop to move the player X times based on the amount of time that has passed between frames, ensuring that collisions and any other game loop events will be properly checked.
This also has the advantage that it is easy to adjust the speed.
Here is the basic concept:
private var speedMultiplier:int = 100;//100 normal speed, 0 paused
private var currRealTime:int = getTimer();
private var currGameTime:int = currRealTime;
private var globalLastTime:int = currRealTime;
private var totalTimeDiffRemainder:int = 0;
private var loopTime:int = 20;//every 20 ms run our actions
public function getGameTimer():int
{
return currGameTime;
}
private function updateGameTime():void
{
var realTime:int = getTimer();
currGameTime = currGameTime + speedMultiplier/100*(realTime - currRealTime);
currRealTime = realTime;
}
private function runEvents(event:Event):void
{//ENTER_FRAME event
var totalTimeDiff:int = getGameTimer() - globalLastTime + totalTimeDiffRemainder;
globalLastTime = getGameTimer();
while (totalTimeDiff > loopTime)
{//every 20 ms run all our actions
totalTimeDiff -= loopTime;
//run all your game loop events here, such as collision checks
}
totalTimeDiffRemainder = totalTimeDiff;
updateGameTime();
}
So every time an ENTER_FRAME event fires, we will check the time passed since the last ENTER_FRAME event and then run our actions once for each 20ms that has elapsed and pass the remainder over to the next ENTER_FRAME event. For instance, if it's been 47 ms since the last ENTER_FRAME, we will run our actions twice and pass over 7 remaining ms to the next ENTER_FRAME event.
In order to pause, slow down, or speed up the game, all you have to do is modify speedMultiplier. Changing speedMultiplier to 0 will pause the game, 50 is half speed 100 is normal speed, 200 double speed, etc.
I believe the general way to do this would be to use an MVC like setup where your model holds all the data for the game elements (character position/orientation, enemies, dynamic map elements) then the controller is modifying the model. With the controller modifying the model this way you could add a multiplier to the model, and use the multiplier when having the controller update the model for "physics" or other modifying dynamic elements in the game.
Roughly:
Model
public var speedMultiplier:Number=1;
public var playerXSpeed:Number;
public var playerYSpeed:Number;
Controller (I'm assuming you make a controller class and pass the view to the constructor and are listening for events from the view in the controller).
private function enterFrame_handler(event:Event):void
{
var playerSprite:Sprite = mainView.playerSprite;
playerSprite.x += playerXSpeed*speedMultiplier; //only problem I can see here is your player skipping past certain elements, to avoid this you could use a loop to make each motion and do checks but it's more CPU intensive
//var enemySprites:Vector<EnemySprite>;
//other game physics here, reduce speed due to drag, fix any invalid values etc.
}
Edit
Actually in thinking this through some more, although I do generally like using an MVC setup myself since it allows one to have a single sprite that does all the drawing; you could also use the same concept of a speedMultiplier shown here without necessarily changing around any software patterns. If you end up needing to do it with a loop because you need it to do checks for every spot it would hit as an object moves along, you may need to have your default speedMultiplier be something like 10 so you could set it down to 1 to get 1/10th speed with all the same checks as it would get at 10 being normal speed (again only issue here being it has to do whatever calculations 10 times for every update, in this case you may want to use a timer instead of the frame rate to control the overall calculation speed).