Actionscript 3.0: Cannot get key input - actionscript-3

guys. I created a little game. Nothing actually happening thought, because im not getting my keyboard input. I spend some time trying to create my own taht didnt work. Then I copy/pasted code from official actionscript 3.0 reference page, but tweaked it for my game (but I didnt touch anything related to keyboard stuff). Also the only thing my game returns in cosnole is false
import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;
stop();
var left = false;
var right = false;
var speed = 0.3;
player.addEventListener(KeyboardEvent.KEY_DOWN, keydF);
player.addEventListener(KeyboardEvent.KEY_UP, keyuF);
player.addEventListener(Event.ENTER_FRAME, updF);
function keydF(event:KeyboardEvent):void {
trace("test0");
if(event.keyCode == Keyboard.D) {
trace("test1");
left = true;
}
if(event.keyCode == Keyboard.A) {
right = true;
}
}
function keyuF(event:KeyboardEvent):void {
trace("test2");
if(event.keyCode == Keyboard.D) {
left = false;
}
if(event.keyCode == Keyboard.A) {
right = false;
}
}
function updF(e:Event):void {
if(left) {
level.x -= speed;
}
if(right) {
level.x += speed;
}
trace(left + ""); //always false :\
}

If you want to get the key input to your app, you should add the listeners to the stage :)

Related

Speed keeps stacking in actionscript 3.0

We have been making this game for our class CPT and you play as a character and try to get through these levels but the issue is that every time you die and go back to play the level again the speed stacks on top of the previous speed to a point where the game isn't even playable anymore. We have tried resetting it and setting it back to 0 but nothing seems to work we also tried to code it so that is the speed goes past 21 it resets back to 20 (our ideal speed) we also tried to remove extra event listeners that could have been stacking on top of the other. and if you do happen to die but get to the next frame the speed goes back to 20 for that frame.But if you die on that frame it doubles the speed on that frame too.Our code is:
import flash.events.Event;
import flash.display.MovieClip;
//makes the character jump
var grav:Number = 10;
var jumping:Boolean = false;
var jumpPow:Number = 0;
stage.addEventListener(KeyboardEvent.KEY_DOWN, whenKeyPressed);
stage.addEventListener(Event.ENTER_FRAME, update);
function whenKeyPressed(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.UP)
{
if(jumping != true)
{
jumpPow = -50;
jumping = true;
}
}
}
function update(event:Event):void
{
if(jumping)
{
player_mc.y += jumpPow;
jumpPow += grav;
if(player_mc.y >= stage.stageHeight)
{
jumping = false;
player_mc.y = stage.stageHeight;
}
}
//when the character makes it to the other side of the screen, the frame
changes
if (player_mc.hitTestObject(frameChanger1))
{
gotoAndStop(91);
}
//end the game when the character touches the penguin
if (player_mc.hitTestObject(penguin1))
{
gotoAndStop(89);
}
}
//makes the character move left and right
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
player_mc.addEventListener(Event.ENTER_FRAME, moveInDirectionOfKey);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, unsetKeyPressed);
function moveInDirectionOfKey(event:Event)
{
if (leftPressed)
{
player_mc.x -= 20;
}
if (rightPressed)
{
player_mc.x += 20;
}
}
function setKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = true;
break;
}
case Keyboard.RIGHT:
{
rightPressed = true;
break;
}
}
}
function unsetKeyPressed(event:KeyboardEvent):void
{
switch (event.keyCode)
{
case Keyboard.LEFT:
{
leftPressed = false;
break;
}
case Keyboard.RIGHT:
{
rightPressed = false;
break;
}
}
//when the character makes it to the other side of the screen, the frame
changes
if (player_mc.hitTestObject(frameChanger1))
{
gotoAndStop(91);
}
//end the game when the character touches the penguin
if (player_mc.hitTestObject(penguin1))
{
gotoAndStop(89);
}
}
Did You tried :
if(!stage.hasEventListener(Event.ENTER_FRAME)){
stage.addEventListener(Event.ENTER_FRAME, update);
}
if(!stage.hasEventListener(KeyboardEvent.KEY_DOWN)){
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPressed);
}
if(!stage.hasEventListener(KeyboardEvent.KEY_UP)){
stage.addEventListener(KeyboardEvent.KEY_UP, unsetKeyPressed);
}
if(!player_mc.hasEventListener(Event.ENTER_FRAME)){
player_mc.addEventListener(Event.ENTER_FRAME, moveInDirectionOfKey);
}
And so on...
Otherwise You will register the Events multiple times...
This may be the issue.
I'm not sure, I avoid to code on multiple frames.
You may also use the answer of #Philarmon here Function being called faster

AS3 keyboard animated character - Compiler errors all over the place despite following tutorial to the letter

Trying to create a 2D character that responds to keyboard inputs in Flash Professional CC. Every YouTube tutorial my son and I have tried produces Compiler errors and NOTHING works.
import flash.events.KeyboardEvent;
import flash.events.Event;
var rightKeyIsDown:Boolean = false;
var leftKeyIsDown:Boolean = false;
var upKeyIsDown:Boolean = false;
var downKeyIsDown:Boolean = false;
var playerSpeed:int = 7;
stage.addEventListener(KeyboardEvent.KEY_DOWN, pressAKey);
stage.addEventListener(KeyboardEvent.KEY_UP, releaseAKey);
function pressAKey(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = true;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = true;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = true;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = true;
}
}
function releaseAKey(event.KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
rightKeyIsDown = false;
}
if(event.keyCode == Keyboard.LEFT)
{
leftKeyIsDown = false;
}
if(event.keyCode == Keyboard.UP)
{
upKeyIsDown = false;
}
if(event.keyCode == Keyboard.DOWN)
{
downKeyIsDown = false;
}
}
player_mc.addEventListener(Event.ENTER_FRAME, moveThePlayer);
function moveThePlayer(event:Event):void
{
if(rightKeyIsDown == true)
{
player_mc.x += playerSpeed;
}
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
}
What is wrong with the above code? We have followed this tutorial, which seems very straightforward however the minute we type in a curly bracket it highlights in red, and then when we try to test we receive Compiler Errors.
Scene 1, Layer 'Player', Frame 1, Line 35, Column 27 1084: Syntax error: expecting rightparen before dot.
We are using Flash Professional CC and AS3 - is there a better resource for tutorials for this type of thing as we're being driven mad having tried at least 5 of these now without success.
Would really appreciate some advice on why the above is wrong and also where we might find tutorials that actually work!
Thank you.
NJ & Son! :)
I threw your code into Flash and did not see any issues surrounding mismatching parenthesis; however, I did notice the following errors
First, In the following if control structure:
if(leftKeyIsDown == true)
{
player_mc.x -+ playerSpeed;
}
You are attempting to use an operator of -+ but no such operator exists in Flash ActionScript. I'm thinking you meant to do the following instead:
if(leftKeyIsDown == true)
{
player_mc.x -= playerSpeed;
}
Second, the issue is right here:
function releaseAKey(event.KeyboardEvent):void
{
// your code
}
You need a : to specify the datatype. Can't believe I missed that. So it should be:
function releaseAKey(event:KeyboardEvent):void
{
// your code
}
I was able to plug this code into Flash CC and get it to compile with no errors. Cheers!

Function is undefined for no apparent reason

I have two event listeners calling two functions, the first checkKeyDown and then the second checkKeyUp.
package {
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.ui.Keyboard;
public class Code extends MovieClip {
var charSpeed:int = 0;
var velocity:int = 0;
var gravity:Number = 1;
var Jump:Boolean = false;
public function startGame(){
stage.addEventListener(KeyboardEvent.KEY_DOWN, checkKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, checkKeyUp);
}
public function Code() {
// constructor code
}
function checkKeyDown(evt:KeyboardEvent){
if (evt.keyCode == Keyboard.LEFT){
charSpeed -= 10;
}
if (evt.keyCode == Keyboard.RIGHT){
charSpeed += 10;
}
if (evt.keyCode == Keyboard.DOWN){
if(!Jump){
velocity -= 14;
Jump = true;
}
}
function checkKeyUp(e:KeyboardEvent){
if (e.keyCode == Keyboard.LEFT){
charSpeed = 0;
}
if (e.keyCode == Keyboard.RIGHT){
charSpeed = 0;
}
}
When I first typed the code, I did the checkKeyDown function first, and when I ran it, it worked fine (without the checkKeyUp event listener of course). After this, i added the event listener and checkKeyUp function to the code, but I got an error saying "1120: Access of undefined property checkKeyUp." I have no idea why this has come up, as the checkKeyDown function works fine. If I could get some help on why I am getting this error, that would be great.
You forget a brace (char '}') at the end of checkKeyDown function.

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

How to do you get an character to jump?

This is my fist time ever needing to use this for one of my games. I want to have the character jump. I have been trying to get this result for about an hour, but with no luck =( I am using AS3, and flash CS5.5. So far all my code does is make the character go left, and right based on keyboard input. Could someone please help?
Here is my code so far:
public class Dodgeball extends MovieClip
{
public var character:Character;
public var rightDown:Boolean = false;
public var leftDown:Boolean = false;
public var speed:Number = 3;
public var timer:Timer;
public function Dodgeball()
{
character= new Character();
addChild(character);
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, MyKeyUp);
timer = new Timer(24);
timer.addEventListener(TimerEvent.TIMER, moveClip);
timer.start();
}
public function myKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.RIGHT)
{
rightDown = true;
if(character.currentLabel != "walkingRight")
{
character.gotoAndStop ("walkingRight");
}
}
if (event.keyCode == Keyboard.LEFT)
{
leftDown = true;
if (character.currentLabel != "backingUp")
{
character.gotoAndStop("backingUp");
}
}
}
public function MyKeyUp(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT)
{
character.gotoAndStop("standing");
rightDown = false;
}
if (event.keyCode == Keyboard.LEFT)
{
character.gotoAndStop("standingLeft");
leftDown = false;
}
}
public function moveClip(event:TimerEvent):void
{
if (rightDown)
{
character.x += speed;
}
if (leftDown)
{
character.x -=speed;
}
event.updateAfterEvent();
}
}
}
One method you can try is found here: http://www.actionscript.org/forums/showthread.php3?t=256009 Like your speed variable, grav determines the vertical position of the character.
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 = -50;
jumping = true;
}
}
}
function update(evt:Event):void
{
if(jumping)
{
player_mc.y += jumpPow;
jumpPow += grav;
if(player_mc.y >= stage.stageHeight)
{
jumping = false;
player_mc.y = stage.stageHeight;
}
}
}
Edit: Jason's method is fine, but I'm not sure if it would be useful if you plan to have some kind of collision detection.
What I would do is create a motion tween of the character jumping. then call gotoAndPlay on that frame, and on the last frame of the tween put a stop, or a gotoAndStop on the "stationary" frame, or whatever frame represents a neutral position.
if (event.keyCode == Keyboard.SHIFT)
{
character.gotoAndPlay("simpleJump");
jumpDown = false;
}
This will give you the greatest animation control over the look and feel. You could also do it programmatically, but personally, I recommend against it. It will take less time to set it up, and you can tweak and refine the jump animations later. You could make several types of jump animations based on object near the target etc.
I would also change this stuff:
if(character.currentLabel != "walkingRight")
By defining a new function where you have all the rules for when and where something can be done, so that in your control logic, you just call
if(characterCan(character,"walkright")) ...
Where characterCan(String) is a method that check if this is possible. For instance, if you are jumping and shooting, you obviously cannot walk right, so in the end, you will have to start adding pieces of logic into those if statements and it's gonna become a cluttered mess.
A very simple approach is to have a vertical speed as well as a horizontal speed.
When the user presses "UP" or "JUMP", set y speed to a negative value and update it in your movieClip function. When the character gets to a certain height, reverse the speed.
Using gravity and acceleration looks better but this is a really good place to start. Look into kinematic equations to see how you would make the character accelerate.
public var originalY;
public function myKeyDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.UP && vSpeed == 0)
{
originalY = character.y;
ySpeed = -1;
}
}
public function moveClip(event:TimerEvent):void
{
if (vSpeed != 0)
{
character.y += vSpeed;
/* make the character fall down after reaching max jump height */
if(originalY - character.y > jumpHeight) {
vSpeed = vSpeed * -1;
}
/* level the character after he's hit the ground (so he doesn't go through) */
else if(character.y >= originalY) {
character.y = originalY;
vSpeed = 0;
}
}
}