How to get mt character to attack - actionscript-3

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.

Related

I want to make a shooting game with animate AS3, and i stuck with with this code

I want to improve this coding of mine but i cant find a way. so everytime i press "space" the bullet came out from the object, and if i pressed it again, the bullet that already called to the stage will reset its coordination, instead of calling another one. or can u only called one child at a time? is there any way for me to calling many child instead of just 1?
import flash.events.KeyboardEvent;
import flash.events.Event;
var fl_MyInstance:bullet = new bullet();
function move(event:Event):void
{
if(fl_MyInstance.hitTestObject(wall))
{
removeChild(fl_MyInstance);
stage.removeEventListener(Event.ENTER_FRAME, move);
}
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, summon);
function summon(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.SPACE){
addChild(fl_MyInstance);
fl_MyInstance.x = hero.x
fl_MyInstance.y = hero.y
stage.addEventListener(Event.ENTER_FRAME, move);
}
}
stage.addEventListener(Event.ENTER_FRAME, gerak);
function gerak(e:Event):void
{
fl_MyInstance.y += 5;
}

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.

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

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.

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++;
}
//....

In Flash Actionscript 3.0 how do I create smooth keyboard controls for player movement?

I am a beginner in Flash Actionscript 3.0 programming. I am trying to create smooth keyboard controls for player movement in a game.
I'm currently using addEventListener(KeyboardEvent.KEY_DOWN) listening for a keyboard key press and then within the handler function moving a graphic by adding a number to its .x or .y property.
This creates a slow, sluggish jerk at the beginning. I know there's a smoother, more responsive way to do this but have no idea where to begin. Any help would be appreciated!
For smooth keys I would suggest using either a Timer or onEnterFrame to poll for keys often enough to get smooth controls. It will get the job done, but at a certain expense. If you've got the rest of the logic all fine, this should fit in ok:
var key:int = NaN;
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress,false,0,true);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyRelease,false,0,true);
this.addEventListener(Event.ENTER_FRAME,update,false,0,true);
function onKeyPress(event:KeyboardEvent):void {
key = event.keyCode;
event.stopPropagation();
}
function onKeyRelease(event:KeyboardEvent):void {
key = NaN;
event.stopPropagation();
}
function update(event:Event):void{
if(key) trace(key);
}
I make sure the event doesn't bubble by stopping it's propagation, and it's set on the stage which should be the topmost level, event wise. Also I'm using the key only the key is down, otherwise I ignore it in the enterFrame handler.
HTH,
George
Where are you placing the listener? Is it in the application, or in the sprite that is supposed to move? Does the sprite have the focus when you are pressing the key?
Also, in adding the event listener, are you using capture? That is, are you setting the 3rd argument to true, as in
addEventListener(KeyboardEvent.KEY_DOWN, yourHandler, true)
If you use capture, which is how you have to do it if the App itself is listening for the event, then you will get a certain amount of latency, and this latency will be greater the more complex the interface is. If those events have to work their way up a vast hierarchy, this could be noticeable. If there are many sprites, this can exacerbate the problem.
What you can do is have the sprite that has the focus dispatch a custom event which a controller class listens to for each sprite. The controller class will have a handler that moves the event.currentTarget however you plan to have it done.
Also read up about custom events and how to use the SystemManager to add and remove listeners dynamically: http://livedocs.adobe.com/flex/3/langref/index.html.
the most simplest example to this would be this.
here you have a controllable Ship class(Ship.as).
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.events.Event;
public class Ship extends MovieClip {
private var speedX;
private var speedY;
public function Ship() {
//constructor
stage.addEventListener(KeyboardEvent.KEY_DOWN ,keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP ,keyUp);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function keyDown(e:KeyboardEvent) {
if(e.keyCode == 37) {
speedX = -5;
}
if(e.keyCode == 38) {
speedY = -5;
}
if(e.keyCode == 39) {
speedX = 5;
}
if(e.keyCode == 40) {
speedY = 5;
}
}
public function keyUp(e:KeyboardEvent) {
if(e.keyCode == 37 || e.keyCode == 39) {
speedX = 0;
}
if(e.keyCode == 38 || e.keyCode == 40) {
speedY = 0;
}
}
public function update(e:Event) {
x += speedX;
y += speedY;
}
}