1084: Syntax error: expecting right paren before leftbrace - actionscript-3

I'm fairly new to action script and I was wondering if any of you guys would be able to solve my problem.
function keyDown (keyEvent:KeyboardEvent):void
{
if (keyEvent.keyCode == Keyboard.D
{
rightPressed = true;
}
else if (keyEvent.keyCode == Keyboard.A
{
leftPressed = true;
}
else if (keyEvent.keyCode == Keyboard.S
{
downPressed = true;
}
else if (keyEvent.keyCode == Keyboard.W
{
upPressed = true;
}
}
I thought the code was fine, however whenever I run it I always get the "1084: Syntax error: expecting right parent before leftbrace" error.

You forgot to close your parenthesis. Like this
if (keyEvent.keyCode == Keyboard.D)
You have it like this
if (keyEvent.keyCode == Keyboard.D

Related

Character jumping while moving right and left AS3

I am making a platform game where the main character moves around and jumps.
I want the character to jump left and right separately. Maybe using two keys at the same time And land on top of the floor. My characters movie clip symbol is Naruto and my floor movie clip symbol is floor.
My project file can be found here: Naruto Game
In order to do this I have a main movie-clip with all the other movie-clips inside such as "jump right" and "jump left".
What I'm having a problem with, is when THE USER IS MOVING RIGHT I WANT THE CHARACTER TO FACE RIGHT WHEN JUMPING (and the same with the left).
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.MovieClip;
import flash.events.Event;
import flash.display.Stage;
naruto.gotoAndStop("stance");
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 narutoSpeed: Number = 10;
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;
naruto.gotoAndStop("standright")
} else if (keyEvent.keyCode == Keyboard.LEFT) {
leftPressed = false;
naruto.gotoAndStop("standleft")
} else if (keyEvent.keyCode == Keyboard.UP) {
upPressed = false;
naruto.gotoAndStop("stance")
} else if (keyEvent.keyCode == Keyboard.DOWN) {
downPressed = false;
naruto.gotoAndStop("stance")
}
}
function gameLoop(loopEvent: Event): void {
if (rightPressed) {
naruto.x += narutoSpeed;
naruto.gotoAndStop("right");
} else if (leftPressed) {
naruto.x -= narutoSpeed;
naruto.gotoAndStop("left");
} else if (upPressed) {
naruto.gotoAndStop("jumpright");
}
}
I owe so much to the person that can solve this I have been trying to solve this for a week! Thank you very much!
To solve this, you need change your else statements into additionalif statements (so instead of being one or the other, you can potentially get both actions - jump & right).
Here is a code example:
function gameLoop(loopEvent: Event): void {
//If the right key is pressed, and the left key is NOT pressed.
if (rightPressed && !leftPressed) {
naruto.x += narutoSpeed;
naruto.gotoAndStop("right");
}
if(leftPressed && !rightPressed) {
naruto.x -= narutoSpeed;
naruto.gotoAndStop("left");
}
if (upPressed) {
//if up is pressed, and right is pressed
if(rightPressed && !leftPressed) naruto.gotoAndStop("jumpright");
if(leftPressed && !rightPressed) naruto.gotoAndStop("jumpleft");
}
if(leftPressed && rightPressed && !upPressed){
naruto.gotoAndStop("stance"); //if left & right are pressed, they negate each other and you play stance
}
}
With this code, if the right key is pressed, naruto will move right. If up is also pressed, then in addition to moving right, it will switch from the right state to the jumpright state.

ActionScript 3 use multiple onKeyDown/onKeyUP

Im trying to get a two plyer version of Snake running but i am having trouble getting the second snake to work, player 1 plays with w,a,s and d while player 2 uses the arrow keys. Player 1 with w,a,s and d is working player 2 with the arrows does not.
The code looks like this:
function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.A){
SnakeDirection = "left";
}else if (event.keyCode == Keyboard.D) {
SnakeDirection = "right";
}else if (event.keyCode == Keyboard.W) {
SnakeDirection = "up";
}else if (event.keyCode == Keyboard.S) {
SnakeDirection = "down";
}
}
function onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.A) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.D) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.W ) {
SnakeDirection = "";
}else if(event.keyCode == Keyboard.S){
SnakeDirection = "";
}
}
/* function onKeyDown(event:KeyboardEvent):void{
if(event.keyCode == Keyboard.LEFT){
Snake2Direction = "left";
}else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
}else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
}else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event:KeyboardEvent):void {
if(event.keyCode == Keyboard.LEFT) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.UP ) {
Snake2Direction = "";
}else if(event.keyCode == Keyboard.DOWN){
Snake2Direction = "";
}
}*/
The comments are there because it breaks the game, from my understanding the errors are because i can only use one onKeyUp/Down. If that is the case, is there another way?
Thanks!
Why do you want to use 2 functions for each event? Wouldn't it be much simpler if you just did the logic for the second player in the same function that also handles the logic of the first player?
Like this:
function onKeyDown(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A) {
SnakeDirection = "left";
} else if (event.keyCode == Keyboard.D) {
SnakeDirection = "right";
} else if (event.keyCode == Keyboard.W) {
SnakeDirection = "up";
} else if (event.keyCode == Keyboard.S) {
SnakeDirection = "down";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT) {
Snake2Direction = "left";
} else if (event.keyCode == Keyboard.RIGHT) {
Snake2Direction = "right";
} else if (event.keyCode == Keyboard.UP) {
Snake2Direction = "up";
} else if (event.keyCode == Keyboard.DOWN) {
Snake2Direction = "down";
}
}
function onKeyUp(event : KeyboardEvent) : void {
//handle player 1
if (event.keyCode == Keyboard.A || event.keyCode == Keyboard.D || event.keyCode == Keyboard.W || event.keyCode == Keyboard.S) {
SnakeDirection = "";
}
//handle player 2
if (event.keyCode == Keyboard.LEFT ||event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.UP || event.keyCode == Keyboard.DOWN) {
Snake2Direction = "";
}
}
Keep in mind, you want to keep the if-blocks for the players separated, so that you can get multiple inputs at the same time.
Also as a side note, the else-if blocks in your onKeyUp function are unnecessary. They all do the same thing anyway.

Single Action with Button Click

I am currently building a small version of a fighting game in flash, but I don't want the character to continuously hit etc. by just holding down a key, but rather to have the key be pressed everytime. This is what I currently have:
function moveChar(event:Event):void{
if(rightKeyDown && !hitting && !combo){
hitting = true;
gotoAndPlay("basic_punch");
kickbag.gotoAndPlay("hit1");
countHits++;
}
if(downKeyDown && !hitting && !combo){
hitting = true;
gotoAndPlay("basic_kick");
kickbag.gotoAndPlay("hit1");
countHits++;
}
if(downKeyDown && combo){
gotoAndPlay("kick_combo1");
kickbag.gotoAndPlay("hit2");
kickbag.stop();
}
if(rightKeyDown && combo){
gotoAndPlay("punch_combo");
kickbag.gotoAndPlay("hit2");
kickbag.stop();
}
}
function checkKeysDown(event:KeyboardEvent):void{
if(countHits == 2) bar.gotoAndStop("bar2");
if(countHits == 6) bar.gotoAndStop("bar3");
if(countHits == 10) {
bar.gotoAndStop("bar4");
combo = true;
gotoAndPlay("combo_stand");
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
}
}
function checkKeysUp(event:KeyboardEvent):void{
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = false;
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = false;
}
}
But as I explained this allows for the buttons to be held down.
Create onKeyDown and onKeyUp listeners. In the onKeyDown listener kill the onKeyDown listener, perform action, and init the onKeyUpListener. In the onKeyUp listener, kill the onKeyUp listener and re-init the onKeyDown listener.
I think #Ribs is on the right track, but given your current code the answer seems simple.
Remove the Event.ENTER_FRAME from moveChar() and change the function definition line so it looks like this:
function moveChar():void //e:Event removed from parameters
Then in your checkKeysDown function, call the moveChar() for the key pressed (your moveChar() evaluates the booleans anyway):
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
moveChar();
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
moveChar();
}
Now you can alter the if statements above to make it a bit more concrete:
if ( !downKeyDown && !rightKeyDown ) {
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
moveChar();
}
else if(!downKeyDown && !rightKeyDown && (event.keyCode == 40 || event.keyCode == 83)){
downKeyDown = true;
moveChar();
}
}
This is basically just handling the down or right key pressed one at a time, and ignoring everything else while those are running. It could also be assume that you might not want to do anything while down or right keys are pressed, therefore you can just do this:
function checkKeysDown(event:KeyboardEvent):void{
if ( downKeyDown || rightKeyDown ) return; //do nothing, we are pressing a key
if(countHits == 2) bar.gotoAndStop("bar2");
if(countHits == 6) bar.gotoAndStop("bar3");*/
if(countHits == 10) {
bar.gotoAndStop("bar4");
combo = true;
gotoAndPlay("combo_stand");
}
if(event.keyCode == 39 || event.keyCode == 68){
rightKeyDown = true;
moveChar();
}
else if(event.keyCode == 40 || event.keyCode == 83){
downKeyDown = true;
moveChar();
}
}
This will return in the checkKeyDown function if we are currently pressing the down or right key. I'm assuming this is probably the desired effect you are going for so your counters do not increment each KEY_DOWN registration. As you can see you have some options and there are multiple ways of doing this. Hope this provides some guidance.
I too have been working on a platformer flash game, and here's how I accomplished a clean attack code: Declare an "attack" Boolean to help decide whether or not the character can or cannot attack. You can prevent the character from continuously attacking by creating an enter frame event listener that checks what frame the character is in during his attack. If the character play-head is at the last frame of the animation than simply set the attack Boolean to false.

AS3 Disabling keyboard text input

I'm making a virtual keyboard, and I want to find a good way to disable the actual keyboard.
For whatever reason my overall code doesnt work unless my text fields are input based.
I tried something simple with this, but it only works when its out of scope..
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyEvent);
function onKeyEvent(e:KeyboardEvent):void
{
var character:String = String.fromCharCode(e.charCode);
if (e.keyCode == 65)
{
trace(character);
}
else if (e.keyCode == 66)
{
trace(character);
}
else if (e.keyCode == 67)
{
trace(character);
}
else if (e.keyCode == 68)
{
trace(character);
}
else if (e.keyCode == 69)
{
trace(character);
}
else if (e.keyCode == 70)
{
trace(character);
}
else if (e.keyCode == 71)
{
trace(character);
}
else if (e.keyCode == 72)
{
trace(character);
}
else if (e.keyCode == 66)
{
trace(character);
}
}
Try
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);
function blindKeyboard(e:KeyboardEvent):void{
e.preventDefault();
e.stopPropagation();
}

fluidly dealing with multiple keypresses in ActionScript3

I'm currently building an engine for a platformer game at the moment, but I've noticed that ActionScript3 is having difficulty in keeping fluid when multiple keypresses are in use. For example;
function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
if (isTouchingGround()) {
isJumping = true;
yv = -100;
}
} else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
if (xv == 0) {
player.gotoAndPlay(275);
}
} else if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
if (xv == 0) {
xv = -24;
} else if (xv != -120) {
xv-=2;
}
} else if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
if (xv == 0) {
xv = 24;
} else if (xv != 120) {
xv+=2;
}
}
}
So, as listed above, using the UP (or W, or Space) key triggers the player to jump (seperate onframe event handler handles gravity etc). Using the RIGHT (or D..) key triggers increases the player acceleration, which is again applied to the player in a seperate onframe event handler.
Everything works fine by itself - but the problem arises when multiple keystrokes are used. If a player starts to move to the right, and hits jump, he will cease accelerating. At the same time, he will not decelerate, as instructed in the Keyboard.UP method. Instead, he will maintain constant at his current rate, until the RIGHT key is hit again.
In short, it is as though Actionscript begins ignoring both the keyboard.down and keyboard.up methods for the RIGHT or LEFT movement keys, until they are no longer being pressed. This obviously causes for some very rigid gameplay - is there any solution anyone would be willing to share with me on this?
Your problem lies in the fact that your if conditionals are followed by if else conditionals. Drop the else and just have the if conditionals. Basically if the user holds down space then none of the other if conditionals are going to be tested since space is being held down and it's the first if statement. So just drop the else off of the if's. Just remove the if else conditionals that are testing keystrokes, not the conditionals inside of the if statements that deal with keystrokes.
Here is what your code should look like:
function onKeyDown(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
if (isTouchingGround()) {
isJumping = true;
yv = -100;
}
}
if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
if (xv == 0) {
player.gotoAndPlay(275);
}
}
if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
if (xv == 0) {
xv = -24;
} else if (xv != -120) {
xv-=2;
}
}
if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
if (xv == 0) {
xv = 24;
} else if (xv != 120) {
xv+=2;
}
}
}
Something else you may notice is that when the UP key and RIGHT key are both being held down, the computer seems to freeze keyboard input, however when the W key and D key are being held down you can still press other keys and the computer will register their input. The answer to that question is here.
Update:
For the fluid part, instead of triggering something when a keystroke takes place, it is better to have a boolean variable such as keyUP or UP that holds a true if the key is down or false when the key is up. Then have a function onEnterFrame(event:Event):void {} that performs an action when keyUP is true. Like so:
import flash.events.*;
public class keyEvents extends MovieClip {
private var keyRIGHT:Boolean = false;
public function keyEvents():void
{
this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
}
function onKeyDown(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT) {
this.keyRIGHT = true;
}
}
function onKeyUp(event:KeyboardEvent):void
{
if(event.keyCode == Keyboard.RIGHT) {
this.keyRIGHT = false;
}
}
function onEnterFrame(event:Event):void
{
if(this.keyRIGHT) {
// This code is executed while the RIGHT arrow key is down.
}
}
}
If the above code does not work I think that your problem lies with your keyboard, not that it's broken or anything but the way it was made might be messing things up.
Let me know if this didn't help and I'll continue trying.