Flash Actionscript Game - Character Not Moving after Adding New Keyframes - actionscript-3

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

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;
}
}

Play full Mc in a single key press

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"
}
}
}

AS3 error 1084 syntax error expecting rightparen before dot

i am new to this whole as3 thing and i am struggling immensely. I have been sat for the last two days trying to do something i can imaging to be simple to everyone else reading this. I am trying to create a game where i have a skate boarder controlled by the keyboard keys. However when i type this code in i am getting a 1084 error please help before i throw my laptop out the window. Thanks!!
package {
import flash.display.*;
import flash.events.*;enter code here
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
the_skater.addChild:(skater);
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
function keyPressedDown(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = true;
} else if (event.keyCode == 39) {
moveRight = true;
} else if (event.keyCode == 65) {
moveUp = true;
} else if (event.keyCode == 90) {
moveDown = true;
}
}
function keyPressedUp(event:KeyboardEvent) {
if (event.keyCode == 37) {
moveLeft = false;
} else if (event.keyCode == 39) {
moveRight = false;
} else if (event.keyCode == 65) {
moveUp = false;
} else if (event.keyCode == 90) {
moveDown = false;
}
}
function moveskater(event:Event) {
var speed:uint = 20;
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0){
skater.x = 800;
}
}
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800){
skater.x = 0;
}
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0){
skater.y = 0;
}
}
if (moveDown) {
skater.y += speed;
if (skater.y > 0){
skater.y = 0;
}
}
I tried your code but didn't get your error. Your sample throws other errors & problems.
So my advice is these two things..
The correct to construct your code...
package
{
//IMPORTS go here
//Declare your Class
public class skatefate extends MovieClip
{
//VARS go here
//*******************************************************************
//note: later you may also add other VARS inside functions as needed
//(but were not originally put (declared) in this section)
//*******************************************************************
//Declare main function of your Class (must have same name as Class (.as)
public function skatefate()
{
//Constructor code here
//************************************************************************
// Your main program code and related functions (K/board etc) go here and
// will reference your VARS declared above in public Class construction)
//************************************************************************
} //End of (public) Function
} //End of (public) Class
} //End of Package
Just incase you still struggle, this edit of your code shown should compile. From there you can study & learn. Hopefully one more laptop will survive in this cruel world.
package
{
import flash.display.*;
import flash.events.*; //enter code here
//Declare your Class
public class skatefate extends MovieClip {
var the_skater:Sprite = new Sprite();
var skater:Sprite = new Sprite(); //hide line if skater exists already (i.e in Library)
var speed:uint = 20;
//Declare main function of your Class
public function skatefate ()
{
var moveLeft:Boolean = false;
var moveRight:Boolean = false;
var moveUp:Boolean = false;
var moveDown:Boolean = false;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressedDown);
stage.addEventListener(KeyboardEvent.KEY_UP, keyPressedUp);
stage.addEventListener(Event.ENTER_FRAME, moveskater);
the_skater.addChild(skater);
addChild(the_skater); //adds to stage
function keyPressedDown (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = true; }
else if (event.keyCode == 39) { moveRight = true; }
else if (event.keyCode == 65) { moveUp = true; }
else if (event.keyCode == 90) { moveDown = true; }
}
function keyPressedUp (event:KeyboardEvent)
{
if (event.keyCode == 37) { moveLeft = false; }
else if (event.keyCode == 39) { moveRight = false; }
else if (event.keyCode == 65) { moveUp = false; }
else if (event.keyCode == 90) { moveDown = false; }
}
function moveskater(event:Event)
{
//var speed:uint = 20; //already declared at top
//speed = 20; // later change 'speed' this way by updating number
if (moveLeft) {
skater.x -= speed;
if (skater.x < 0)
{ skater.x = 800; }
}
if (moveRight) {
skater.x += speed;
if (skater.x > 800)
{ skater.x = 0; }
}
if (moveUp) {
skater.y -= speed;
if (skater.y > 0)
{ skater.y = 0; }
}
if (moveDown) { skater.y += speed;
if (skater.y > 0)
{ skater.y = 0; }
}
} //close 'moveskater' function
} //End of your (public) Function
} //End of your (public) Class
} //End of Package
Hope it helps. Ask for advice in the comments and don't forget to tick as "correct answer" if it works for you. That's how we say "Thanks" on Stack Overflow.

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;
}
}

Character Jump with Animation

Im trying to create a platform game but im struggling with the jump part of the game. I have got the jump working but when I try to add the animation it goes all wrong. When I hit the jump button it jumps but when i lands it gets stuck on the end of the jump animation until i move. Secondly when i jump and press the right or left button it jumps but plays the run animation while i try to move in mid air. How can i resolve these problems
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 Yvelocity:Number = 0;
var CanJump: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
Yvelocity += Gravity;
//if player is more than 300 on the y-axis
if (this.y > 300)
{
//Player stays on the ground and can jump
Yvelocity = 0;
CanJump = true;
}
if ((RightKeyPress == true))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress == true))
{
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
if ((UpKeyPress == true && CanJump))
{
Yvelocity = -15;
CanJump = false;
gotoAndStop('Jump');
}
this.y += Yvelocity;
}
function KeyReleased(event:KeyboardEvent)
{
if (event.keyCode == 39)
{
event.keyCode = 0;
RightKeyPress = false;
gotoAndStop('Idle');
}
if (event.keyCode == 37)
{
event.keyCode = 0;
LeftKeyPress = false;
gotoAndStop('Idle');
}
if (event.keyCode == 38)
{
event.keyCode = 0;
UpKeyPress = false;
}
}
}
}
this.y > 300, that is your jump end logic. Add a gotoAndStop('Idle'); in that if's block.
Just Check if canJump is true on your left and right keypresses before executing those blocks. If canJump is true allow the running stuff