Play full Mc in a single key press - actionscript-3

First, sorry to ask this silly question (I am new in AS3). I wasted more then 2 weeks on this problem, and am now posting it here.
I am making a hero move with the keyboard. I have three animations.
Standby mode
walk front
walk behind
It's working well so far, but the problem I'm facing in jumping the player is having to hold down the key to jump. I don't want the player to be required to hold the key to perform jumping.
So I want to play the full MovieClip with one key press, and honestly I don't know which function I have to use or how to do it.
Here is the file, and here is my code
import flash.display.Stage;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
kim.gotoAndStop("kim Stand");
var grav:int = 0;
var floor = 450;
var dPressed:Boolean = false;
var aPressed:Boolean = false;
var jumping: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;
}
else if (Devent.keyCode == Keyboard.A)
{
aPressed = true;
}
else if (Devent.keyCode == Keyboard.W && !jumping)
{
jumping = true;
}
}
function KeyUpHandaler (Uevent:KeyboardEvent):void
{
if (Uevent.keyCode == Keyboard.D)
{
dPressed = false;
kim.gotoAndStop("kim Stand");
}
else if(Uevent.keyCode == Keyboard.A)
{
aPressed = false;
kim.gotoAndStop("kim Stand");
}
else if(Uevent.keyCode == Keyboard.W)
{
jumping = false;
kim.gotoAndStop("kim Stand");
}
}
function gameLoop(Levent:Event):void
{
if (dPressed)
{
kim.x += 5;
kim.gotoAndStop("kim Move Right");
}
else if(aPressed)
{
kim.x -= 5;
kim.gotoAndStop("kim Move Left");
}
else if(jumping)
{
kim.gotoAndStop("kim Jump");
kim.y -= 10;
}
gravity();
}
function gravity ():void
{
kim.y += grav;
if (kim.y+kim.height/2 <floor){
grav++;
}
else {
grav = 0;
kim.y = floor - kim.height/2 ;
}
}

You may want to try state based logic. The keyboard events set it, and the actions for updating the character are handled separately in your gameLoop(). The last piece of the puzzle would be to update your state when you recognize you've landed (something that happens not from keyboard interaction, but rather from your gravity function).
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 state:String = "stand";
stage.addEventListener(KeyboardEvent.KEY_DOWN , keyHandler);
stage.addEventListener(KeyboardEvent.KEY_UP , keyHandler);
stage.addEventListener(Event.ENTER_FRAME , gameLoop);
function keyHandler(e:KeyboardEvent):void {
switch (e.keyCode) {
case Keyboard.D:
state = (e.type == "keyDown") ? "right" : "stand";
break;
case Keyboard.A:
state = (e.type == "keyDown") ? "left" : "stand";
break;
case Keyboard.W:
state = (e.type == "keyDown") ? "jumping" : "falling";
break;
}
}
function gameLoop(Levent:Event):void {
switch (state) {
case "stand":
kim.gotoAndStop("kim Stand");
break;
case "right":
kim.x += 5;
kim.gotoAndStop("kim Move Right");
break;
case "left":
kim.x -= 5;
kim.gotoAndStop("kim Move Left");
break;
case "jumping":
kim.y -= 10;
kim.gotoAndStop("kim Jump");
break;
case "falling":
kim.gotoAndStop("kim Jump");
break;
}
gravity();
}
function gravity ():void {
kim.y += grav;
if (kim.y + kim.height/2 < floor) {
grav++;
} else {
grav = 0;
kim.y = floor - kim.height/2;
if (state == "falling") {
state = "stand"
}
}
}

Related

how to move an object a fixed distance with 1 press

This is a very simple question but after trying several time in google i am still not able to fix this.
i have a hero which walk left and right if user hold down the button and change position 5 per frame.
Now what i want is if i press a key single time (not holding) then it will go/walk normally to a fix distance( lets say x +=50 ) and stop there.
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 dPressed:Boolean = false;
var aPressed: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;
}
else if (Devent.keyCode == Keyboard.A)
{
aPressed = true;
}
}
function KeyUpHandaler (Uevent:KeyboardEvent):void
{
if (Uevent.keyCode == Keyboard.D)
{
dPressed = false;
hero.gotoAndStop("hero Stand");
}
else if(Uevent.keyCode == Keyboard.A)
{
aPressed = false;
hero.gotoAndStop("hero Stand");
}
}
function gameLoop(Levent:Event):void
{
if (dPressed)
{
hero.x += 5;
hero.gotoAndStop("hero Move Right");
}
else if(aPressed)
{
hero.x -= 5;
hero.gotoAndStop("heroMove Left");
}
}
set up another property:
var counter: uint = 0;
Put a counter in your gameLoop function like this:
if (dPressed == true) {
counter++;
hero.x += 5;
hero.goToAndStop("hero Move Right");
if (counter >= 10){
dPressed = false;
counter = 0;
}
}

Flash Actionscript Game - Character Not Moving after Adding New Keyframes

I am really new to actionscript and am having an issue with a project I am working on for school. The code that makes my character move (Right, Left, Up, Down) all worked fine but then I added a new Keyframe at the beginning of the project and created a scene to choose a character. I have set it up exactly like my teacher showed us but now that the new keyframe is there, I click the character and it goes to the gameplay but the character cannot be moved. Any help would be very much appreciated.
Here is the code from the first key frame:
stop ();
mc_fox.visible = false;
import flash.events.Event;
import flash.events.MouseEvent;
btn_fox.addEventListener(MouseEvent.CLICK,btn_foxHandler);
function btn_foxHandler(event:MouseEvent):void
{
gotoAndStop(2);
}
And The code from frame 2:
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
mc_fox.stop ();
var move:uint = 0;
stage.addEventListener (KeyboardEvent.KEY_DOWN, keydownHandler);
stage.addEventListener (KeyboardEvent.KEY_UP, keyupHandler);
function keyupHandler (event:KeyboardEvent) :void {
if (event.keyCode == Keyboard.RIGHT){
move = 0
mc_fox.gotoAndStop (1);
}
else if (event.keyCode == Keyboard.LEFT){
move = 0
mc_fox.gotoAndStop (15);
}
else if (event.keyCode == Keyboard.UP){
move = 0
mc_fox.gotoAndStop (30);
}
else if (event.keyCode == Keyboard.DOWN){
move = 0
mc_fox.gotoAndStop (45);
}
}
function keydownHandler (event:KeyboardEvent) :void {
if(event.keyCode == Keyboard.RIGHT && mc_fox.x < 889) {
if (move ==0){
mc_fox.gotoAndPlay (1);
move = 1
}
else {
mc_fox.x = mc_fox.x + 5;
mc_fox.play ();
}
}
else if(event.keyCode == Keyboard.LEFT && mc_fox.x > 111) {
if (move ==0){
mc_fox.gotoAndPlay (15);
move = 1
}
else {mc_fox.x = mc_fox.x - 5;
mc_fox.play ();
}
}
else if (event.keyCode == Keyboard.UP && mc_fox.y > 270) {
if (move ==0){
mc_fox.gotoAndPlay (30);
move = 1
}
else{mc_fox.y = mc_fox.y - 5;
mc_fox.width = mc_fox.width - .9;
mc_fox.height = mc_fox.height - .9;
mc_fox.play();
}
}
else if (event.keyCode == Keyboard.DOWN) {
if (move ==0){
mc_fox.gotoAndPlay (45);
move = 1
}
else{mc_fox.y = mc_fox.y + 5;
mc_fox.width = mc_fox.width + .9;
mc_fox.height = mc_fox.height + .9;
mc_fox.play();
}
}
}

Linking multiple objects to one coding name

I am trying to make an rpg style game in Flash AS3. I am trying to get my character to stop when he hits objects such as trees and buildings. Is there a way I can link multiple objects together and make the code say something like 'take object1, object2, and object3 and name it multipleobjects'? I have the code set up so that the character stops at one tree but I'm not positive how/if you can combine objects so that he won't go through multiple at one time. Thank you very very much in advanced!
Objects:
manmc (my character), treer1_MC (the first tree) treer2_MC (second tree) and so on
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
manmc.gotoAndStop ("Stand Front Frame");
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 manSpeed:Number = 3;
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;
manmc.gotoAndStop("Stand Right Frame");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
manmc.gotoAndStop("Stand Left Frame");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
manmc.gotoAndStop("Stand Back Frame");
}
else if(keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
manmc.gotoAndStop("Stand Front Frame");
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed)
{
if(manmc.x < 1050)
{
manmc.x += manSpeed;
}
manmc.gotoAndStop("Walk Right Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("leftHit");
manmc.x -= 3;
}
}
else if(leftPressed)
{
if (manmc.x > 145)
{
manmc.x -= manSpeed;
}
manmc.gotoAndStop("Walk Left Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("rightHit");
manmc.x += 3;
}
}
else if(downPressed)
{
if(manmc.y < 780)
{
manmc.y += manSpeed;
}
manmc.gotoAndStop("Walk Front Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("downHit");
manmc.y -= 3;
}
}
else if(upPressed)
{
if(manmc.y > 145)
{
manmc.y -= manSpeed;
}
manmc.gotoAndStop("Walk Back Frame");
if (manmc.hitTestObject(treer1_MC))
{
trace("upHit");
manmc.y += 3;
}
}
}
Use an array to keep track of multiple objects.
Instead of manipulating the single object, you then use a loop to iterate over all the elements in the array which are the single objects.

Null object error; cannot access it - AS3

I'm making a platform game. Im trying just to get the player to move around on the stage, and to be able to jump, with some type of gravity added to it. However, when I run it, I get this error: Error #1009: Cannot access a property or method of a null object reference.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code1 extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
var leftKey:Boolean;
var rightKey:Boolean;
var upKey:Boolean;
private var platform:Platform;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
stage.addEventListener(Event.ENTER_FRAME, loop);
stage.addEventListener(Event.ENTER_FRAME, update);
}
public function Code() {
}
public function update(evt:Event){
moveChar();
}
public function moveChar(){
if (leftKey == true){
charSpeed -= 10;
}
if (rightKey == true){
charSpeed += 10;
}
if (upKey == true){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = true;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = true;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = true;
}
}
function checkKeyUp(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.UP){
upKey = false;
}
else if (evt.keyCode == Keyboard.RIGHT){
rightKey = false;
}
else if (evt.keyCode == Keyboard.LEFT){
leftKey = false;
}
}
function loop(evt:Event){
player.x = charSpeed;
if (player.x < 0){
player.x = 0;
}
if (player.x > 550){
player.x = 550;
}
velocity += gravity;
if (!platform.hitTestPoint(player.x, player.y, true)){
player.y += velocity;
}
for (var i = 0; i < 10; i++){
if (platform.hitTestPoint(player.x, player.y, true)){
player.y--;
velocity = 0;
Jump = false;
}
}
}
}
}
My platform linkage is "Platform", but i set up a variable for it (or tried to). I debugged the code, and it came up with this line: player.x = charSpeed;
I have no idea what to do, if someone could help, that would be great.
You never declare or instantiate (ie player = new Player()) your player.
Alternatively, if your player is on the stage of your .fla timeline, it will need the instance name 'player'. That can be set in the object properties.
You player object is null.
I don't see the istantiation line as:
var player:Player = new Player();
Add it before use a property of your player

How to make my character dash - Double Key Press

I'm creating a beat em up game and so far i have the character running and jumping. I now want to make my character to dash left or right. So if the player presses the key right, right very quickly then the character will dash. How can i make this happen. 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
{
//Player run speed setting
var RunSpeed:Number = 8;
//Player key presses
var RightKeyPress:Boolean = false;
var LeftKeyPress:Boolean = false;
var UpKeyPress:Boolean = false;
//Jump variables
var Gravity:Number = 1.5;
var JumpPower:Number = 0;
var CanJump:Boolean = false;
var Jumped:Boolean = false;
public function Player()
{
// constructor code
stage.addEventListener(KeyboardEvent.KEY_DOWN,KeyPressed);
addEventListener(Event.ENTER_FRAME,Update);
stage.addEventListener(KeyboardEvent.KEY_UP,KeyReleased);
}
function KeyPressed(event:KeyboardEvent)
{
//When Key is Down
if (event.keyCode == 39)
{
RightKeyPress = true;
}
if (event.keyCode == 37)
{
LeftKeyPress = true;
}
if (event.keyCode == 38)
{
UpKeyPress = true;
}
}
function Update(event:Event)
{
//Adding gravity to the game world
JumpPower += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
JumpPower = 0;
CanJump = true;
}
//If on floor
if (CanJump)
{
//If right key is pressed run right
if ((RightKeyPress))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress))
{
//otherwise if left key is pressed run left
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
if ((UpKeyPress))
{
//If up key is pressed then jump
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
Jumped = true;
}
//If no key is pressed stay idle
if ((!RightKeyPress && !LeftKeyPress && CanJump))
{
gotoAndStop('Idle');
}
}
else if (CanJump == false)
{
//Other if in air and right key is pressed move right
if ((RightKeyPress))
{
x += RunSpeed;
scaleX = 1;
}
else if ((LeftKeyPress))
{
//Otherwise if left key is pressed then move left
x -= RunSpeed;
scaleX = -1;
}
}
//If already jumped and on floor
if (Jumped == true && CanJump)
{
//Cannot jump again
CanJump = false;
//If on floor and right key is pressed run right
if ((RightKeyPress))
{
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress))
{
//Otherwise if on floor and left key is pressed run left
gotoAndStop('Run');
scaleX = -1;
}
//If no key is pressed stay idle
if ((!RightKeyPress && !LeftKeyPress))
{
gotoAndStop('Idle');
}
}
this.y += JumpPower;
}
function KeyReleased(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
event.keyCode = 0;
RightKeyPress = false;
}
if (event.keyCode == 37)
{
event.keyCode = 0;
LeftKeyPress = false;
}
if (event.keyCode == 38)
{
event.keyCode = 0;
UpKeyPress = false;
Jumped = false;
}
}
}
}
So I made a little test and came up with this. It seems to be working pretty well right now. I haven't tested it in extreme lengths, but it could get you started. I'll leave it up to you to implement into your own code. Just copy/paste into a new AS3 document and watch the console
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressed);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function keyPressed(e:KeyboardEvent):void
{
if (lastKeyPressed == e.keyCode && pressed) trace("double tapped " + e.keyCode);
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false},doubleTapDelay);
}
EDITED TO WORK WITH KEY_DOWN
This seems to be working better with KEY_DOWN
import flash.events.KeyboardEvent;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function keyPressed(e:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
if (lastKeyPressed == e.keyCode && pressed) trace("double tapped " + e.keyCode);
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false},doubleTapDelay);
}
function onKeyboardUp(e:KeyboardEvent):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
EDITED AGAIN FOR THE DEMO
DEMO: http://ronnieswietek.com/_random/dash_example.swf
SOURCE: http://ronnieswietek.com/_random/dash_example.fla
import flash.events.KeyboardEvent;
import com.greensock.TweenLite;
import com.greensock.easing.*;
import com.greensock.plugins.*;
TweenPlugin.activate([BlurFilterPlugin]);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_DOWN, permaKeyDown);
var pressed :Boolean = false;
var lastKeyPressed :Number = -1;
var dashAmount :Number = 50;
var doubleTapDelay :Number = 260; //-- delay in milliseconds
function permaKeyDown(e:KeyboardEvent):void
{
switch (e.keyCode)
{
case 38: //-- up arrow
char.y = char.y - 2;
break;
case 39: //-- right arrow
char.x = char.x + 2;
break;
case 40: //-- down arrow
char.y = char.y + 2;
break;
case 37: //-- left arrow
char.x = char.x - 2;
break;
}
}
function keyPressed(e:KeyboardEvent):void
{
stage.removeEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyboardUp);
if (lastKeyPressed == e.keyCode && pressed)
{
trace("double tapped " + e.keyCode);
doDash(e.keyCode);
}
lastKeyPressed = e.keyCode;
pressed = true;
setTimeout(function(){pressed = false}, doubleTapDelay);
}
function onKeyboardUp(e:KeyboardEvent):void
{
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
}
function doDash(keyCode:Number):void
{
switch (keyCode)
{
case 38: //-- up arrow
TweenLite.to(char,0,{blurFilter:{blurY:50}});
TweenLite.to(char,0.3,{blurFilter:{blurY:0},y:char.y - dashAmount,ease:Expo.easeOut});
break;
case 39: //-- right arrow
TweenLite.to(char,0,{blurFilter:{blurX:50}});
TweenLite.to(char,0.3,{blurFilter:{blurX:0},x:char.x + dashAmount,ease:Expo.easeOut});
break;
case 40: //-- down arrow
TweenLite.to(char,0,{blurFilter:{blurY:50}});
TweenLite.to(char,0.3,{blurFilter:{blurY:0},y:char.y + dashAmount,ease:Expo.easeOut});
break;
case 37: //-- left arrow
TweenLite.to(char,0,{blurFilter:{blurX:50}});
TweenLite.to(char,0.3,{blurFilter:{blurX:0},x:char.x - dashAmount,ease:Expo.easeOut});
break;
}
}