How to play full movie clip once with a single key press - actionscript-3

I have a movie clip (lets say 10 frame with 10 different image) and I am playing it with a keyboard. so player has to hold down the key to play that movie clip if player release the button it stops doing the movie clip and go to normal.
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
var dPressed:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , gameLoop);
function keyDownHandaler(Devent:KeyboardEvent):void
{
if (Devent.keyCode == Keyboard.D)
{
dPressed = true;
}
}
function KeyUpHandaler (Uevent:KeyboardEvent):void
{
if (Uevent.keyCode == Keyboard.D)
{
dPressed = false;
char.gotoAndStop("Standby Mode");
}
function gameLoop(Levent:Event):void
{
if (dPressed)
{
char.x += 5;
char.gotoAndStop("Char Move Right");
}
That's working fine but what I need is not hold the key, player will press the key 1 time and it will finish the whole movie clip once (lets say 5 frame with 5 different image) and go back to standby mode instead of looping.
I have tried char.gotoAndPlay("Frame name") // which content the 5 frame movie clip
but it doesn't work, when I click it goes to the first frame and stays like that.

Related

how to make full jump with single press

This is a very simple question but even after googling i am not getting the right idea.
i have a hero who i want to make jump. i have written the code in my logic but the problem is i have to hold the key to perform the whole jump. lets say it will go y-= 20but if i hold my button its going the full distance. But if i click once its not going/ passing the full distance.
i want to make my hero jump the whole way with a single press, i mean just 1 press not holding down the button.
here is my code
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
var grav:int = 0;
var floor = 450;
var jumping:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , update);
function gravity ():void
{
hero.y += grav;
if (hero.y+hero.height/2 <floor){
grav++;
}
else {
grav = 0;
hero.y = floor - hero.height/2 ;
}
}
function keyDownHandaler(Devent:KeyboardEvent):void
{
if (Devent.keyCode == Keyboard.W)
{
jumping = true;
}
}
function KeyUpHandaler (Uevent:KeyboardEvent):void
{
if (Uevent.keyCode == Keyboard.W)
{
jumping = false;
}
}
function update(Levent:Event):void
{
if (jumping)
{
hero.y -= 20;
}
gravity();
}
I think the trick you need to work out is keeping the "jumping" flag set until the entire jump sequence is finished. Currently you have it resetting as soon as the button lifts up, then setting again when it's re-pressed.
There is actually no reason to turn off the jumping flag just because the user lifted the button.
Right here:
hero.y += grav;
if (hero.y+hero.height/2 <floor){
grav++;
}
You have a check to see if the character has hit the floor. It should be there that the jump sequence finishes.
All the stuff you do to your character should not be tracked on the KEY being DOWN. It should be tracked on the jumping=true. The key being pressed just starts the sequence. And you do need to keep that flag so as not to start the sequence again if it's already running.
Added because the first answer is already pretty long. This is an answer with the requested re-write of the OP's code: (Read notes at the end)
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
var grav:double = 0.025; //Change this to make gravity stronger or weaker.
var gravInc:double = .2; //Changed to "double" because int is too large an increment. If double doesn't work, try float or decimal.
var floor = 450;
var jumping:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyDownHandaler);
stage.addEventListener(KeyboardEvent.KEY_UP , KeyUpHandaler);
stage.addEventListener(Event.ENTER_FRAME , update);
function gravity ():void
{
if (hero.y+hero.height/2 < floor){
//grav++; //not needed
gravInc = gravInc - grav; //GravInc starts strong, and gradually decreases. It will eventully be less than zero, and start negetive.
hero.y -= gravInc; //The effect of which is the chracter will move upward until gravity takes over, and make hero move downward - increaseing speed until it hits the floor.
} else {
//grav = 0; Not needed
hero.y = floor - hero.height/2 ;
//We've hit the floor. Stop jump sequence
jumping = false;
}
}
function keyDownHandaler(Devent:KeyboardEvent):void
{
if (Devent.keyCode == Keyboard.W)
{
if (jumping==false) { //The character is not already jumping
//This initiates the jump sequence
//set up the jump sequence by resetting the gravInc and setting jumping to true
gravInc = .2;
jumping = true;
//Start the jump now so that the character is off the floor
hero.y -= gravInc;
}
}
}
function KeyUpHandaler (Uevent:KeyboardEvent):void
{
if (Uevent.keyCode == Keyboard.W)
{
// jumping = false;
// Not needed here
}
}
function update(Levent:Event):void
{
// This whole thing goes. It is not needed because the jumping==true will handle it.
// if (jumping)
// {
// hero.y -= 20;
// }
if ( jumping==true ){
gravity();
}
}
I have no way of testing this code. I commented out the parts of your code that were unnecessary, and I added in the things that you'd need to make it function.
ActionScript syntax is very similar to JavaScript. So I'm including this link as a sort-of demo to show how the whole gravity thing works in an actual program. Bounding balls
View the source on that page.
Hopefully this does the trick for you.

Unable to stop spawning an enemy

Okay so here's the dealio.
The code below is what I use to spawn a single enemy periodically in my horror game. He spawns at the window every 10 seconds, then every 6 seconds later, he enters the room and you're essentially dead already, because 5 seconds after that you get a lovely jumpscare. The moment the enemy shifts from the window to the bedroom is when I want the window enemy to stop spawning.
EnemyShipTimer is the timer that spawns the enemy.
enemyTimer is the timer that counts how long the enemy has been at
the window, and then in the room.
In the main file, I've attempted to get rid of the enemy a number of ways, to no effect.
So function gtfo is my latest attempt at removing the enemy, by trying to stop the spawning from the class file itself. It still hasn't worked. Maybe I need to somehow remove the class file from my game, but I have no idea how. Please help! ;A;
PS, countClick counts how many clicks you've done to make the enemy go away, although when I make him go away, room enemy still spawns :c
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.display.Sprite;
import flash.utils.Timer;
import flash.utils.getTimer;
import flash.events.TimerEvent;
import flash.text.TextField;
public class EnemyShip extends MovieClip
{
private var clickCount:int = 0;
private var enemyShipTimer:Timer;
private var enemyTimer:Timer;
public function EnemyShip()
{
this.x = 900;
this.y = 214;
addEventListener(MouseEvent.CLICK, addClick);
addEventListener(MouseEvent.CLICK, countclick);
addEventListener(TimerEvent.TIMER, gtfo);
}
function gtfo(e:TimerEvent):void{
if (enemyTimer.currentCount==5)
enemyShipTimer.stop();
stage.removeChild(this);
}
function addClick(event:MouseEvent):void
{
clickCount = clickCount + 1;
trace("Clickage : " + clickCount);
}
function countclick(event:MouseEvent):void
{
if (clickCount ==4)
{
stage.removeChild(this);
}
}
}
}
The following is the relevant main timeline code that may be clashing with the code (I have updated the package class as per #Karma 's helpful instructions c: though the enemy still spawns; I think what the problem is now is that the timer functions clash with timer functions I've put in my main timeline, and I attempted to sort of move them into the package class but I don't know how to add the necessary movieclips so that I won't get access of undefined property errors so I separated the two once more
enemy is the window spawning enemy.
mosnta is the room spawning enemy.
sod is the screen of death (game over screen) which does not play at all.
var enemyShipTimer:Timer;
function sendEnemy(e:Event)
{
var enemy = new EnemyShip();
stage.addChild(enemy);
var enemytimer = new Timer (1000, count);
enemytimer.start();
enemyShipTimer = new Timer(10000);
enemyShipTimer.addEventListener("timer", sendEnemy);
enemyShipTimer.start();
enemytimer.addEventListener(TimerEvent.TIMER, countdowndesu);
function countdowndesu(e:Event):void{
if (enemytimer.currentCount>5){
trace("ur ded");
enemytimer.removeEventListener(TimerEvent.TIMER, countdowndesu);
mosnta.visible=true;
enemy.visible=false;
if (enemytimer.currentCount==8){
sod.visible=true;
sod.gotoAndPlay(2);
}
}
}
}
Not really understanding the dynamics of how your game works, but here's an altered version of your code with working Timers. Maybe this will help you figure out how to fix your issues
package
{
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.utils.Timer;
import flash.events.TimerEvent;
public class EnemyShip extends MovieClip
{
private var clickCount:int = 0;
private var enemyShipTimer:Timer;
private var enemyTimer:Timer;
public function EnemyShip()
{
this.x = 900;
this.y = 214;
//only need one click handler
addEventListener(MouseEvent.CLICK, addClick);
//Instantiate Timers
enemyShipTimer = new Timer(10000); //10 seconds
enemyTimer = new Timer(6000); //6 seconds
//add TimerEvent listeners onto Timer Objects
enemyShipTimer.addEventListener(TimerEvent.TIMER, gtfo);
enemyTimer.addEventListener(TimerEvent.TIMER, enemyTimerHandler);
//Start the timers
enemyShipTimer.start();
enemyTimer.start();
}
//This gets called every 10 seconds
private function gtfo(e:TimerEvent):void
{
trace("enemy spawns at window");
if (enemyTimer.currentCount == 5) {
enemyShipTimer.stop();
stage.removeChild(this);
}
}
//This gets called every 6 seconds
private function enemyTimerHandler(e:TimerEvent):void
{
trace("enemy moves to room");
}
private function addClick(event:MouseEvent):void
{
clickCount ++;
trace("Clickage : " + clickCount);
if (clickCount == 4)
{
stage.removeChild(this);
}
}
}
}

How can I stopped all layers in layers movie clip when click pause button using action script-3?

I have use action script 3 but problem is, when I click stop button then stop just object & graphic not a pause movie clip, so how can I pause full screen ?
If you want to stop all MovieClips,try this
import flash.display.MovieClip;
import flash.display.DisplayObject;
import flash.display.DisplayObjectContainer;
stopAll(stage);
private function stopAll(obj:DisplayObject):void {
var mc:MovieClip = obj as MovieClip;
var container:DisplayObjectContainer = obj as DisplayObjectContainer;
if (mc) {
mc.stop();
}
if (container) {
for each (var child:DisplayObject in container) {
stopAll(obj);
}
}
}

How to get mt character to attack

I'm trying to create a beat em up game and right now i got my character attacking. I have 3 attack animation so far and works just about. This is what i want, if the player presses the attack button then the character will attack and if the player keeps pressing the attack button it will continue with its attack sequence (Note: I do no want if the player holds down the key, has to be a key press).
The problem is if you keep mashing the attack button it attacks but the problem is it goes to the next attack animation as soon as the attack button is down and I don't want that. How can i make it go to the next attack animation once the current attack animation has completely finished instead of jumping to the next frame midway of it's current attack animation. So i want the character to finish its attack and if the player still keys in the attack key it will go to the next attack frame otherwise it will stop. This is what i have done so far
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
//Attack variables
var Attacking:Boolean = false;
var Punches:int = 0;
var Punching:Boolean = false;
public function Player()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
addEventListener(Event.ENTER_FRAME,Update);
}
function KeyPressed(event:KeyboardEvent):void
{
//Note: I need these listeners here
stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
//If A key is down
if (event.keyCode == 65)
{
Attacking = true;
Punching = true;
Punches++;
}
}
function Update(event:Event)
{
//If player is not attacking
if (Attacking == false)
{
Punching = false;
Punches = 0;
}
else if (Attacking == true)
{
if (Punching == true)
{
gotoAndStop('Jab' + Punches);
}
}
}
function KeyUp(event:KeyboardEvent)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
}
}
}
Also within the last frame of every attack animation i have put down
import flash.display.MovieClip;
import flash.events.Event;
stop();
MovieClip(parent).Attacking = false;
MovieClip(parent).Punches = 0;
Not sure if this method is the best way around it or should i create a extended class/arrays if so how will i do this
Change
if (Punching == true)
{
gotoAndStop('Jab' + Punches);
}
To
if (Punching && !Attacking)
{
gotoAndStop('Jab' + Punches);
}
So basically, it will make sure that an animation isn't already playing before it (re)plays the animation.
Edit: Even though you are calling gotoAndStop while still on that frame, it will replay the animations of any children MovieClips.

AS3 Attack Animation Issues

I'm trying to create a beat em up game and right now i got my character attacking. I have 3 attack animation so far and works just about. If you keep mashing the attack button it will attack but the problem is it goes to the next attack animation as soon as the attack button is down and I don't want that. How can i make it go to the next attack animation once the current attack animation has finished instead of jumping to the next frame midway of it's current animation. So i want the character to finish its attack and if the player still keys in the attack key it will go to the next attack frame.
package
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
public class Player extends MovieClip
{
//Attack variables
var Attacking:Boolean = false;
var Punches:int = 0;
var Punching:Boolean = false;
var Kicks:int = 0;
var Kicking:Boolean = false;
public function Player()
{
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
addEventListener(Event.ENTER_FRAME,Update);
}
function KeyPressed(event:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, KeyUp);
//If A key is down
if (event.keyCode == 65)
{
Attacking = true;
Punching = true;
Punches++;
}
}
function Update(event:Event)
{
//If player is not attacking
if (Attacking == false)
{
Punching = false;
Punches = 0;
Kicking = false;
Kicks = 0;
}
else if (Attacking == true)
{
if (Punching == true)
{
gotoAndStop('Jab' + Punches);
}
}
}
function KeyUp(event:KeyboardEvent)
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);
}
}
}
Also within the last frame of every attack animation i have put down
import flash.display.MovieClip;
import flash.events.Event;
stop();
MovieClip(parent).Attacking = false;
MovieClip(parent).Punches = 0;
First of all you should remove the event listener adding and removing from both KeyPressed and KeyUp methods (they are not needed and they will just cause problemes) and register the key up event in the costructor just like the other two.
Secondly, to accomplish this you will need to check if the key is down instead of listening for the key press. To do this you will need a new field called, for instance, holdingAttackKey.
var holdingAttackKey:Boolean = false;
function KeyPressed(event:KeyboardEvent):void {
if (event.keyCode == 65)
{
holdingAttackKey = true;
Attacking = true;
Punching = true;
//...
function KeyUp(event:KeyboardEvent)
{
holdingAttackKey = false;
}
function Update(event:Event)
{
if(holdingAttackKey && Attacking==false) {
Attacking = true;
Punching = true;
Punches++;
}
//....