how to add timer in as3 - actionscript-3

I am doing a game that will count number of clicks then add scores. What i want is to add a timer, for example: the timer is 10 secs and the click done is 25 and your score is 35 points if the timer stopped the button(the one used to count the num of clicks) cannot be clicked and it will pause for a while, plan to make a little animation before it moves to another frame.
Want to make this simple as possible since design is more important than the codes, because it is a design base class.
Please no hitTestObject or classes, and i want to avoid arrays too :( last time a used them is a disaster...
Sorry for being noob
And thank you for advance
Here is the cODE:
var power:Number = 0;
var myTimer : Timer = new Timer(10 * 1000, 0);
myTimer.addEventListener(TimerEvent.TIMER_COMPLETE, function( e:TimerEvent ):void
{
myTimer.start();
trace("time up");
bgBack.gotoAndPlay("hit");
if (power == 5)
{
bgBack.gotoAndPlay("mini");
}
else if (power == 15)
{
bgBack.gotoAndPlay("mini");
}
else if (power == 25){
bgBack.gotoAndPlay("belowAve");
}
else if (power == 35){
bgBack.gotoAndPlay("ave");
}
else if (power == 50){
bgBack.gotoAndPlay("ave");
}
else if (power == 65){
bgBack.gotoAndPlay("highAve");
}
else if (power == 80){
bgBack.gotoAndPlay("magni");
}
});
pressBtn.addEventListener( MouseEvent.CLICK, function( e:MouseEvent ):void
{
power++;
if (power == 5)
{
gauge.gotoAndPlay("one");
}
else if (power == 15)
{
gauge.gotoAndPlay("two");
}
else if (power == 25){
gauge.gotoAndPlay("three");
}
else if (power == 35){
gauge.gotoAndPlay("four");
}
else if (power == 50){
gauge.gotoAndPlay("five");
}
else if (power == 65){
gauge.gotoAndPlay("six");
}
else if (power == 80){
gauge.gotoAndPlay("seven");
}

var myTimer : Timer = new Timer( 10 * 1000, 0 );
myTimer.addEventListener( TimerEvent.TIMER_COMPLETE, function( e:TimerEvent ):void
{
//here the timer ends and you can do stuff with the clicks.
//the ,0 means it will repeat this over and over and over. 10 seconds * 1000 milliseconds because its kept in milliseconds.
});
myTimer.start();
and also
var myClicks : Number = 0;
stage.addEventListener( MouseEvent.CLICK, function( e:MouseEvent ):void
{
myClicks++;
});

Related

Character jumping but not returning to ground platform game AS3

I am making a platform game where the main character moves right and left and jumps however my character jumps and does not return to the ground but stays on top of the stage.My characters movie-clip symbol is called 'naruto' and my ground symbol is called 'ground'.
Here is my code:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
naruto.gotoAndStop("stance");
var rightPressed:Boolean = new Boolean(false);
var leftPressed:Boolean = new Boolean(false);
var upPressed:Boolean = new Boolean(false);
var downPressed:Boolean = new Boolean(false);
var narutoSpeed:Number = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME,gameLoop);
function keyDownHandler(keyEvent:KeyboardEvent):void
{
if (keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = true;
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = true;
}
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if (keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
naruto.gotoAndStop("standright")
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
naruto.gotoAndStop("standleft")
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
naruto.gotoAndStop("stance")
}else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
naruto.gotoAndStop("stance")
}
}
function gameLoop(loopEvent: Event): void {
//If the right key is pressed, and the left key is NOT pressed
if (rightPressed && !leftPressed) {
naruto.x += narutoSpeed;
naruto.gotoAndStop("right");
}
if(leftPressed && !rightPressed) {
naruto.x -= narutoSpeed;
naruto.gotoAndStop("left");
}
var jumpHeight =0;
var defaultJumpSpeed = 20;
var jumpSpeed = 20;
if(upPressed && naruto.hitTestObject(ground))
{
trace("HELLO!");
naruto.y -= jumpSpeed;
jumpSpeed-= 4;
}
if(upPressed)
{
trace("HELLO!");
jumpHeight++;
naruto.y -= jumpSpeed;
if(jumpHeight>10)
jumpSpeed -= 4;
}
if(naruto.hitTestObject(ground))
{
trace("HELLO!");
jumpHeight =0;
jumpSpeed = defaultJumpSpeed;
}
}
Here is the link for my work: https://www.mediafire.com/?8d5opy49fuqmup5
Here is the problem:
The main issue, is in your current code, the player can only possible be in 3 positions:
Whatever the ground position is
16 (up is pressed and the character was not touching the ground
12 (up is pressed and the character was touching the ground)
Please see the code comments next to your original code to explain what is happening:
//you have this var inside the game loop, so every loop it resets to 20
var jumpSpeed = 20;
//so here, if up is pressed and the character is touching the ground
//you initiate a jump
if(upPressed && naruto.hitTestObject(ground))
{
trace("HELLO!");
naruto.y -= jumpSpeed;
jumpSpeed-= 4; //since you reset jumpSpeed to 20 every loop, this value will always just be 16
}
if(upPressed)
{
trace("HELLO!");
jumpHeight++;
//if naruto is touching the ground
//you are subtracting jumpSpeed above and right here again.
//so now the y position is 12 if touching the ground (or 16 if not touching the ground)
//since you reset jumpSpeed to 20 every loop, it always be one of those two values
naruto.y -= jumpSpeed;
if(jumpHeight>10)
jumpSpeed -= 4; //this serves no purpose, as your not using the value again and it gets reset to 20 next time the game loop runs
}
//this hit test will succeed in addition to the first one above when your jump starts.
if(naruto.hitTestObject(ground))
{
trace("HELLO!");
jumpHeight =0;
jumpSpeed = defaultJumpSpeed;
}
To remedy your jumping, you'll need to do something along these lines:
//initialize these vars outside of the game-loop for efficient and proper scoping (so their values don't reset every frame)
var isJumping:Boolean = false; //a flag to know if a jump is in progress
var jumpSpeed:Number = 0; //the current velocity of the jump
var defaultJumpSpeed:Number = 20; //the initial force (speed) of a jump
var jumpGravity:Number = 2; //subtract this from the speed every frame to slow the jump over time and fall
var onGround:Boolean = false; //a helper var for efficiency
function gameLoop(loopEvent: Event): void {
//If the right key is pressed, and the left key is NOT pressed
if (rightPressed && !leftPressed) {
naruto.x += narutoSpeed;
naruto.gotoAndStop("right");
}
if (leftPressed && !rightPressed) {
naruto.x -= narutoSpeed;
naruto.gotoAndStop("left");
}
//only do the hit test once per frame for efficiency (store the result)
onGround = naruto.hitTestObject(ground);
if (upPressed && onGround) {
trace("START JUMP");
isJumping = true;
jumpSpeed = defaultJumpSpeed; //set the initial jump velocity
}
if(isJumping){ //if jumping
naruto.y -= jumpSpeed; //move the player based off the current jump velocity
jumpSpeed -= jumpGravity; //slow the jump every frame (eventually causing it to be negative making the player fall)
}
//touching the ground and the up key is not pressed
//it's very important to put !upPressed so this doesn't run at the time as the initial jump above
if (!upPressed && onGround) {
//stop any jump motion
jumpSpeed = 0;
isJumping = false;
//you probably also want to snap the player to the ground
naruto.y = ground.y - naruto.height + 2; //the plus 2 ensures that the ground and the player touch so the hit test succeeds
}
}

how do i change my main player in flash?

im new about scripting and i want to make a little free shop, my game is about a running car that have to avoid objects, so in the shop i want to that when the user press select http://prntscr.com/270ws5 the main car that is this one http://prntscr.com/270y45 , become the car that they selected, thanks
//These variables will note which keys are down
//We don't need the up or down key just yet
//but we will later
var leftKeyDown:Boolean = false;
var upKeyPressed:Boolean = false;
var rightKeyDown:Boolean = false;
var downKeyDown:Boolean = false;
//the main character's speed
var mainSpeed:Number = 7;
//whether or not the main guy is jumping
var mainJumping:Boolean = false;
//how quickly should the jump start off
var jumpSpeedLimit:int = 20;
//the current speed of the jump;
var jumpSpeed:Number = 0;
//adding a listener to mcMain which will make it move
//based on the key strokes that are down
player.addEventListener(Event.ENTER_FRAME, moveChar);
function moveChar(event:Event):void{
//if certain keys are down then move the character
if(leftKeyDown){
player.x -= mainSpeed;
}
if(rightKeyDown){
player.x += mainSpeed;
}
if(upPressed || mainJumping){
mainJump();
gotoAndStop(2)
gotoAndStop(3)
gotoAndStop(4)
gotoAndStop(5)
}
}
//listening for the keystrokes
//this listener will listen for down keystrokes
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeysDown);
function checkKeysDown(event:KeyboardEvent):void{
//making the booleans true based on the keycode
//WASD Keys or arrow keys
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = true;
}
if(event.keyCode == 38 || event.keyCode == 87){
upPressed = true;
gotoAndStop(2)
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
}
}
//this listener will listen for keys being released
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeysUp);
function checkKeysUp(event:KeyboardEvent):void{
//making the booleans false based on the keycode
if(event.keyCode == 37 || event.keyCode == 65){
leftKeyDown = false;
}
if(event.keyCode == 38 || event.keyCode == 87){
upPressed = false;
gotoAndStop(2)
gotoAndStop(3)
gotoAndStop(4)
gotoAndStop(5)
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = false;
}
if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = false;
}
}
//jumping function
function mainJump():void{
//if main isn't already jumping
if(!mainJumping){
//then start jumping
mainJumping = true;
jumpSpeed = jumpSpeedLimit*-1;
player.y += jumpSpeed;
} else {
//then continue jumping if already in the air
//crazy math that I won't explain
if(jumpSpeed < 0){
jumpSpeed *= 1 - jumpSpeedLimit/75;
if(jumpSpeed > -jumpSpeedLimit/5){
jumpSpeed *= -1;
}
}
if(jumpSpeed > 0 && jumpSpeed <= jumpSpeedLimit){
jumpSpeed *= 1 + jumpSpeedLimit/50;
}
player.y += jumpSpeed;
//if main hits the floor, then stop jumping
//of course, we'll change this once we create the level
if(player.y >= stage.stageHeight - player.height){
mainJumping = false;
player.y = stage.stageHeight - player.height;
}
}
}
that is my car script, thanks a lot if someone can help me, to improve my script knowledge
You give a property to player's class of type MovieClip as it's movie clips you seem to have in your game to represent cars, which is then assigned an instance of a selected car. Then, whenever you want your player to gotoAndStop(), you address that to attached instance instead. Thus the instance will represent changeable graphics and the wrapper object (player) takes care of handling, collisions and other non-graphical stuff.
class Player extends Sprite {
private var _car:MovieClip; // the instance
public function Player() {
_car=new MovieClip();
addChild(_car);
}
public function get car():MovieClip { return _car; } // getter function
public function set car(value:MovieClip):void {
// setter function. We need to clean up our player from old car
if (!value) return; // null car - no wai
if (_car) removeChild(_car);
_car=value;
_car.x=0;
_car.y=0; // place car at zero relative to player
// this way you move the player, not the car,
// but MC playing is the car, not the player
addChild(_car);
}
...
}
Note that you can also relocate jump processing into Player class, so that you can just call player.jump() and don't care about the actual values, the player will then have to store the jumping constants, speeds, state (in midair or touching ground), etc etc.

For loop not running inside of a function

I am trying to make a character (char) jump in stages in a game using a for loop to jump a part of the way each time the loop run. The loop never initializes.
Jump starting is traced to the output console but the jump No. does not get traced.
Why is this?
JumpHeight == 25
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(Event.ENTER_FRAME, loop);
function loop(event:Event):void
{
if (jumping == false && char.hitTestObject(floor) == false)
{
char.y += gravity
}
}
function keyPressed(event:KeyboardEvent):void
{
if (event.keyCode == jumpKey)
{
jump()
}
}
function jump()
{
if (char.y >= groundY)
{
trace("Jump Starting")
jumping = true
for (jCycle = 0; jCycle == jumpHeight; jCycle++)
{
char.y -= gravity
trace("Jump No. " + jCycle)
}
jumping = false
}
}
Your problem is that for condition is always false (jCycle == 0 !=jumpHeight) and body is unreachable.
Try this:
for (var jCycle:int = 0; jCycle <= jumpHeight; jCycle++)
{
//body
}
You have an error in the for loop condition: jCycle == jumpHeight should be jCycle < jumpHeight (or jCycle <= jumpHeight).

Some Problems with my AS3 code

Some troubles to make my new flash game(i used AS3,CS5.5):
I just try to make my character(hatt) walk and jump, but I can't make it at the same time, well, idk how... Furthermore, I don't know how make my character recognize the ground.
And at last this thing here:
"Scene 1, Layer 'hatt', Frame 1, Line 6 Warning: 1090: Migration issue: The onKeyDown event handler is not triggered automatically by Flash Player at run time in ActionScript 3.0. You must first register this handler for the event using addEventListener ( 'keyDown', callback_handler)."
Plz help me and give some hints plz... Thanks.
Here's the code:
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(Event.ENTER_FRAME, update);
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
function update(evt:Event):void
{
if (jumping)
{
hatt.y += jumpPow;
jumpPow += grav;
if (hatt.y >= stage.stageHeight)
{
jumping = false;
hatt.y = stage.stageHeight;
}
}
}
stage.addEventListener (KeyboardEvent.KEY_DOWN, myFunction) ;
function myFunction (event: KeyboardEvent){
if(event.keyCode == Keyboard.LEFT) {
hatt.x -= 5
}
if(event.keyCode == Keyboard.RIGHT) {
hatt.x += 5
}
}
remove this line:
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
remove function onKeyDown:
function onKeyDown(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.UP)
{
if (jumping != true)
{
jumpPow = -25;
jumping = true;
}
}
}
replace myFunction with this:
function myFunction (event: KeyboardEvent)
{
if(event.keyCode == Keyboard.LEFT)
hatt.x -= 5;
if(event.keyCode == Keyboard.RIGHT)
hatt.x += 5;
if(event.keyCode == Keyboard.UP && jumping != true)
{
jumpPow = -25;
jumping = true;
}
}

how do I either stop the tween at the point of my choice (first code)? or how do I stop the easing (second code)?

This is for Actionscript 3.0 on CS5.
I've been having some issues with fl.transitions.easing.None.easeNone. I tried to animate my MovieClip character without easing and stopping on a dime. I used the following code to stop the easing:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
function keyPressed(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT && var_move == false)
{
var_move = true;
hero.gotoAndStop("Walk");
hero.scaleX = 1;
var tween1:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x+75, 15, false)
tween1.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
}
if(evt.keyCode == Keyboard.LEFT && var_move == false)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = -1;
var tween2:Tween = new Tween(hero, "x", None.easeNone, hero.x, hero.x-75, 15, false)
tween2.addEventListener(TweenEvent.MOTION_FINISH, onMotionFinished);
}
}
function onMotionFinished($evt:TweenEvent):void
{
hero.gotoAndPlay("Stand");
var_move = false;
}
Which it did but if you just tap Right or Left Arrow the MovieClip character will not stop on that frame. It continues until the tween is done. If I use the following code:
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseKey);
function keyPressed(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = 1;
hero.x += 5;
}
if(evt.keyCode == Keyboard.LEFT)
{
var_move = true;
hero.gotoAndPlay("Walk");
hero.scaleX = -1;
hero.x -= 5;
}
}
function releaseKey(evt:KeyboardEvent):void
{
if(evt.keyCode == Keyboard.RIGHT || evt.keyCode == Keyboard.LEFT)
{
hero.gotoAndStop("Stand");
}
}
the MovieClip character eases into the walk animation, but stops where I want it to. how do I either stop the tween at the point of my choice (first code) or how do I stop the easing (second code)?
For tweening, I highly recommend using the library at http://greensock.com ... the free TweenMax/TweenLite will save you tons of time trying to solve problems like this... they already did the hard part :)