as3 - how to stop animation when both keys pressed? - actionscript-3

My player stops moving when two keys are pressed at the same time. But the animation still moves. For example, if I press up and down at the same time or right and left at the same time.
On key down event listener:
if (event.keyCode == Keyboard.D)
{
isRight = true
}
if (event.keyCode == Keyboard.A)
{
isLeft = true
}
if (event.keyCode == Keyboard.W)
{
isUp = true
}
if (event.keyCode == Keyboard.S)
{
isDown = true
}
On key up event listener:
if (event.keyCode == Keyboard.D)
{
isRight = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.A)
{
isLeft = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.W)
{
isUp = false
gotoAndStop(1);
}
if (event.keyCode == Keyboard.S)
{
isDown = false
gotoAndStop(1);
}
On enterframe:
if (isRight == true)
{
x += 5;
play();
}
if (isLeft == true )
{
x -= 5;
play();
}
if (isUp == true)
{
y -= 5;
play();
}
if (isDown == true)
{
y += 5;
play();
}

If players goes x -= 1 and x += 1 it basically moves x += 0 overall. We can easily check that and stop animation in needed:
var iP:Point = new Point(x,y);//try to avoid creating new objects on frame interval
if (isRight) x += 5;
if (isLeft) x -= 5;
if (isUp) y -= 5;
if (isDown) y += 5;
if(!Point.distance(iP,new Point(x,y)) goToAndStop(1);
else play();

I don't see any checks to see if more than 1 key is being pressed?
surely you should introduce something like a keycount into the enterframe:
var count:uint = 0;
if (isRight == true){
count++
x += 5;
}
if (isLeft == true ){
count++;
x -= 5;
}
if (isUp == true){
count++;
y -= 5;
}
if (isDown == true){
count++
y += 5;
}
if (count > 1) {
isRight = isLeft = isUp = isDown = false;
gotoAndStop(1);
} else {
play();
}

Related

First question! NPC walk-cycle randomizer is working as intended (boundaries do need tweaking), but not playing the nested walking animations? (AS3)

Pretty much, the walking animations won't work with the switch statement I think? That's what it looks like, but the randomizer itself works fine. What I wanted to do was rewrite the working code for the mc, but replace the key inputs with the cases (random walking directions chosen) from the switch statement and plug that rewritten code in separately into the NPC's brain (roshi).
The way I have the code set up is the NPC "roshi" is a movieclip on the stage. Inside are individual movieclips each with frames for each walking animation but they won't play (ex. roshiRightStand, roshiRightWalk, etc). It always gets stuck on the chosen frame, and never actually enters the movieclip no matter how I mess with the code, brackets or booleans. Maybe I'm not declaring or nesting something right or at all? The animations are frames within a movieclip nested within another movieclip and the names seem to match up in the properties? Not quite sure how to declare it, but at some point I believe I may have had the whole thing working and messed it up. Left my old leftover code if it could help? Any input would be much appreciated! Learning fairly quick. :)
*The Code w/ mistakes (slashed out): https://textuploader.com/108de
*The Code cleaned up and tidy: https://textuploader.com/108lw
*Game Upload (reflects the code as is; playable on your browser w/ plugin): https://www.newgrounds.com/dump/item/e06224a5f9fd5645ce5a4604173f8bbd?fbclid=IwAR3HJdXMXEqxUN5TH2xaDvV82QBDmI0ewnVej1EQJFkZLb3RYuEK0dvMz74
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;
mc.gotoAndStop("standingRight");
toggle_btn.stop();
var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();
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 mcSpeed:Number = 10;
var roshiTimer:Number = 0; //random roshi-cycle duration between 0-25
var roshiDuration:Number = Math.random() * 25;
var roshiFacing:Number = Math.floor(Math.random() * 4); //random # bwt 0-3 (ex. 4 is rounded down to 3)
var roshiSpeed:Number = 3; //roshi's walk speed
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function togglePlay(event:MouseEvent):void
{
(!played)? played = true : played = false;
(played)? myChannel=mySound.play():SoundMixer.stopAll();
(played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
played = false;
toggle_btn.gotoAndStop(1);
}
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.DOWN)
{
downPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
mc.gotoAndStop("standingRight");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
mc.gotoAndStop("standingLeft");
}
else if (keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
mc.gotoAndStop("standingDown");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
mc.gotoAndStop("standingUp");
}
}
function gameLoop(loopEvent:Event):void
{
//MC's Movement Controls
if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingRight");
}
if(rightPressed && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed;
}
else if (backenvironment.x > -650) //right world borderwall
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingLeft");
}
if(leftPressed && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed;
}
else if (backenvironment.x < -130) //left world borderwall
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingUp");
}
if(upPressed && mc.currentLabel == "walkingUp")
{
if(mc.y > 200) //og 200
{
mc.y -= mcSpeed;10
}
else if (backenvironment.y < -10) //top world borderwall
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingDown");
}
if (downPressed && mc.currentLabel == "walkingDown")
{
if(mc.y < 485) //og 568 LESS MOVES MC UP CAMERA
{
mc.y += mcSpeed;
}
else if (backenvironment.y > -577) //bottom world borderwall og-577
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
if(roshiTimer < roshiDuration)
{
switch(roshiFacing) //x=horozontal y=vertical +=right/up -=left/down
{
case 0 :
roshi.gotoAndStop("roshiUpWalk");
//roshi.addEventListener(Event.ENTER_FRAME)
roshi.y -= roshiSpeed;
break;
case 1 :
roshi.gotoAndStop("roshiDownWalk");
roshi.y += roshiSpeed;
break;
case 2 :
roshi.gotoAndStop("roshiLeftWalk");
roshi.x -= roshiSpeed;
break;
case 3 :
roshi.gotoAndStop("roshiRightWalk");
roshi.x += roshiSpeed;
break;
}
roshiTimer ++;
}
if(roshiTimer > roshiDuration)
{
roshiDuration = Math.random() * 25; //25
roshiFacing = Math.floor(Math.random() * 4); //4
roshiTimer = 0; //greater than 0
}
}
~Solved by user Organis.
import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;
import flash.display.Stage;
import flash.events.MouseEvent;
mc.gotoAndStop("standingRight");
toggle_btn.stop();
var played:Boolean=false;
var mySound:Sound = new MySound();
var myChannel:SoundChannel = new SoundChannel();
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 mcSpeed:Number = 10;
var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3
addEventListener(Event.ENTER_FRAME, onRoshi);
toggle_btn.addEventListener(MouseEvent.CLICK, togglePlay);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, gameLoop);
function togglePlay(event:MouseEvent):void
{
(!played)? played = true : played = false;
(played)? myChannel=mySound.play():SoundMixer.stopAll();
(played)? toggle_btn.gotoAndStop(2):toggle_btn.gotoAndStop(1);
myChannel.addEventListener(Event.SOUND_COMPLETE,soundCompleted);
}
function soundCompleted(event:Event):void
{
played = false;
toggle_btn.gotoAndStop(1);
}
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.DOWN)
{
downPressed = true;
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = true;
}
}
function keyUpHandler(keyEvent:KeyboardEvent):void
{
if(keyEvent.keyCode == Keyboard.RIGHT)
{
rightPressed = false;
mc.gotoAndStop("standingRight");
}
else if(keyEvent.keyCode == Keyboard.LEFT)
{
leftPressed = false;
mc.gotoAndStop("standingLeft");
}
else if (keyEvent.keyCode == Keyboard.DOWN)
{
downPressed = false;
mc.gotoAndStop("standingDown");
}
else if(keyEvent.keyCode == Keyboard.UP)
{
upPressed = false;
mc.gotoAndStop("standingUp");
}
}
function gameLoop(loopEvent:Event):void
{
if(rightPressed && mc.currentLabel !="walkingRight" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingRight");
}
if(rightPressed && mc.currentLabel =="walkingRight")
{
if(mc.x < 800)
{
mc.x += mcSpeed;
}
else if (backenvironment.x > -650)
{
backenvironment.x -= mcSpeed;
frontenvironment.x -= mcSpeed;
}
}
if(leftPressed && mc.currentLabel !="walkingLeft" && upPressed == false && downPressed == false)
{
mc.gotoAndStop("walkingLeft");
}
if(leftPressed && mc.currentLabel =="walkingLeft")
{
if(mc.x > 200)
{
mc.x -= mcSpeed;
}
else if (backenvironment.x < -130)
{
backenvironment.x += mcSpeed;
frontenvironment.x += mcSpeed;
}
}
if(upPressed && mc.currentLabel != "walkingUp" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingUp");
}
if(upPressed && mc.currentLabel == "walkingUp")
{
if(mc.y > 200)
{
mc.y -= mcSpeed;10
}
else if (backenvironment.y < -10)
{
backenvironment.y += mcSpeed;
frontenvironment.y += mcSpeed;
}
}
if(downPressed && mc.currentLabel != "walkingDown" && rightPressed == false && leftPressed == false)
{
mc.gotoAndStop("walkingDown");
}
if (downPressed && mc.currentLabel == "walkingDown")
{
if(mc.y < 485)
{
mc.y += mcSpeed;
}
else if (backenvironment.y > -577)
{
backenvironment.y -= mcSpeed;
frontenvironment.y -= mcSpeed;
}
}
}
var RF:Array =
[
"roshiUpWalk", "roshiDownWalk",
"roshiLeftWalk", "roshiRightWalk",
];
var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];
function onRoshi(e:Event):void
{
roshiTimer ++;
if (roshiTimer > roshiDuration)
{
roshiDuration = 10 + Math.random() * 25;
roshiTimer = 0;
while (Roshi.currentLabel == RF[roshiFacing])
{
roshiFacing = Math.random() * 4;
}
Roshi.gotoAndStop(RF[roshiFacing]);
}
Roshi.x += RX[roshiFacing] * roshiSpeed;
Roshi.y += RY[roshiFacing] * roshiSpeed;
}
Well, let's try anyway. Put this into separate project along with Roshi graphics. The main difference between your code and this that I don't trigger Roshi.gotoAndStop every frame, just once upon Roshi changing directions.
// Integers, because you don't have to round them.
var roshiTimer:int = 0;
var roshiDuration:int = 0;
var roshiFacing:int = 0;
var roshiSpeed:Number = 3;
addEventListener(Event.ENTER_FRAME, onRoshi);
// Let's do this instead of switch.
var RF:Array = [
"roshiUpWalk", "roshiDownWalk",
"roshiLeftWalk", "roshiRightWalk",
];
var RX:Array = [0, 0, -1, 1];
var RY:Array = [-1, 1, 0, 0];
function onRoshi(e:Event):void
{
roshiTimer++;
if (roshiTimer > roshiDuration)
{
roshiDuration = 10 + Math.random() * 25;
roshiTimer = 0;
// Let's make Roshi ALWAYS change direction
// without occasionally proceeding to the same one.
while (Roshi.currentLabel == RF[roshiFacing])
{
roshiFacing = Math.random() * 4;
}
Roshi.gotoAndStop(RF[roshiFacing]);
}
Roshi.x += RX[roshiFacing] * roshiSpeed;
Roshi.y += RY[roshiFacing] * roshiSpeed;
}

why doesn't this code detect diagonal motion?

"player" is an object that moves up , down , left and right when I press on arrow keys.
"player" doesn't move diagonally when I press up and left keys together or any other couple of keys.
I'm using Adobe flash CS5 and action script 3 ( As3 ), Would you tell me what the solution is ?
stage.addEventListener(KeyboardEvent.KEY_DOWN, detectText);
function detectText(myevent:KeyboardEvent):void {
var up:Boolean = false;
var down:Boolean = false;
var left:Boolean = false;
var right:Boolean = false;
var speedOfplayer:Number = 5 ;
if (myevent.keyCode==39){
right = true ;
}
if (myevent.keyCode==37){
left = true ;
}
if (myevent.keyCode==38){
up = true ;
}
if (myevent.keyCode==40){
down = true ;
}
// if(right is true and left is not true)
if( right && !left ) {
player.x = player.x + speedOfplayer;
}
// if(up is true and down is not true)
if( up && !down ) {
player.y = player.y - speedOfplayer;
}
// if(down is true and up is not true)
if( down && !up ) {
player.y = player.y + speedOfplayer;
}
// if(down is true and up is not true)
if( left && !right ) {
player.x = player.x - speedOfplayer;
}
// Move diagonally
if( left && up && !right && !down ) {
player.y = player.y - speedOfplayer;
player.x = player.x - speedOfplayer;
}
if( right && up && !left && !down ) {
player.x = player.x + speedOfplayer;
player.y = player.y - speedOfplayer;
}
if( left && down && !right && !up ) {
player.x = player.x - speedOfplayer;
player.y = player.y - speedOfplayer;
}
if( right && down && !left && !up ) {
player.x = player.x + speedOfplayer;
player.y = player.y + speedOfplayer;
}
You may be overcomplicating things. This can be done in the following way. What is happening to the KEY_DOWN listener is that it will report the last key pressed. So you need a ENTER_FRAME listener to move the object. And because ENTER_FRAME will keep calling the function provided you need to capture if a key is released to reset the speed of the player to 0.
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
stage.addEventListener(Event.ENTER_FRAME, onTick);
var speedX:uint = 0;
var speedY:uint = 0;
function onKeyDown(event:KeyboardEvent):void {
var key:uint= event.keyCode;
if (key == Keyboard.UP) {
speedY = -5;
}
if (key == Keyboard.DOWN) {
speedY = 5;
}
if (key == Keyboard.LEFT) {
speedX = -5;
}
if (key == Keyboard.RIGHT) {
speedX = 5;
}
}
function onKeyUp(event:KeyboardEvent):void {
var key:uint= event.keyCode;
if (key == Keyboard.UP || key == Keyboard.DOWN) {
speedY = 0;
}
if (key == Keyboard.LEFT || key == Keyboard.RIGHT) {
speedX = 0;
}
}
function onTick(event:Event):void {
player.x += speedX;
player.y += speedY;
}

as3 multiple key_down moving object

My object "araba" is not moving when KEY_DOWN happening waiting for your comments thx !
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.display.Bitmap;
stage.addEventListener(KeyboardEvent.KEY_DOWN, basili);
stage.addEventListener(KeyboardEvent.KEY_UP, degil);
stage.addEventListener(Event.ENTER_FRAME, giris);
var yukari:Boolean = false;
var asagi:Boolean = false;
var sola:Boolean = false;
var saga:Boolean = false;
var hiz:Number = 5;
var araba:MovieClip=new araba();
var masa:MovieClip =new masa();
function giris(event:Event):void
{
if ( sola && !saga ) {
araba.x -= hiz;
araba.rotation = 270;
}
if( saga && !sola ) {
araba.x +=hiz;
araba.rotation = 90;
}
if( yukari && !asagi ) {
araba.y -= hiz;
araba.rotation = 0;
}
if( asagi && !yukari ) {
araba.y += hiz;
araba.rotation = 180;
}
if( sola && yukari && !saga && !asagi ) {
araba.rotation = 315;
}
if( saga && yukari && !sola && !asagi ) {
araba.rotation = 45;
}
if( sola && asagi && !saga && !yukari ) {
araba.rotation = 225;
}
if( saga && asagi && !sola && !yukari) {
araba.rotation = 135;
}
if( araba.y < masa.y ){
araba.y = masa.height;
}
if( araba.y > masa.height ){
araba.y = masa.y;
}
if( araba.x < masa.x ){
araba.x = masa.width;
}
if( araba.x > masa.width ){
araba.x = masa.x;
}
}
function basili (event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
yukari = true;
break;
case Keyboard.DOWN:
asagi = true;
break;
case Keyboard.LEFT:
sola = true;
break;
case Keyboard.RIGHT:
saga = true;
break;
}
}
function degil(event:KeyboardEvent):void
{
switch( event.keyCode )
{
case Keyboard.UP:
yukari = false;
break;
case Keyboard.DOWN:
asagi = false;
break;
case Keyboard.LEFT:
sola = false;
break;
case Keyboard.RIGHT:
saga = false;
break;
}
}
<code>
I've looked at your code and it looks fine to me, but why don't you try tracing and see what happens?
private var leftKeyIsDown:Boolean;
private var rightKeyIsdown:Boolean
private var downKeyIsDown:Boolean;
private var upKeyIsDown:Boolean;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDown); //when key is down, go to this function
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); //when key is up, go to this function
gameloop();
private function gameLoop(e:Event):void //this function runs 30 times every second, due to 30fps
{
playerControl();
}
private function keyDown(e:KeyboardEvent):void
{
//trace Event and KeyCode, when we trace this we get I.D number from key
//trace(e.keyCode);
if (e.keyCode == 37)
{
//left key is pressed
leftKeyIsDown = true;
}
if (e.keyCode == 38)
{
//up key is pressed
upKeyIsDown = true;
}
if (e.keyCode == 39)
{
//right key is pressed
rightKeyIsdown = true;
}
if (e.keyCode == 40)
{
//down key is pressed
downKeyIsDown = true;
}
}
private function keyUp(e:KeyboardEvent):void
{
//if our left key is released
if (e.keyCode == 37)
{
//left key is released
leftKeyIsDown = false;
}
if (e.keyCode == 38)
{
//up key is released
upKeyIsDown = false;
}
if (e.keyCode == 39)
{
//right key is released
rightKeyIsdown = false;
}
if (e.keyCode == 40)
{
//down key is released
downKeyIsDown = false;
}
}
private function playerControl():void
{
//if left is currently down
if (leftKeyIsDown)
{
//move ship to the left
ship.x -= 5; //subtract 5 from ships position evey loop,
}
//if right is currently down
if (rightKeyIsdown)
{
//move ship to the right
ship.x += 5; //subtract 5 from ships position evey loop,
}
//if up is currently down
if (upKeyIsDown)
{
//move ship up
ship.y -= 5; //subtract 5 from ships position evey loop,
}
//if left is currently down
if (downKeyIsDown)
{
//move ship down
ship.y += 5; //subtract 5 from ships position evey loop,
}
}
replace ship with your object.
This is a template, i've commented too.
Please tell me if this is useful, it doesn't fix your code, I know.
But hopefully you can adapt this to it.

AS3: Single Key Press Only

I'm trying to create a platform game and I have the run, jump and double jump working. Now my problem is I want the player to press the jump button and the hero to jump only once instead of continuously jumping. If I hold the up button it will keep jumping which i do not want, i just want it to perform the jump once even if the key is still held down. I want the same for the double jump, how can i do this.
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 DoubleJump: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;
DoubleJump = false;
}
//If player is on floor and right key is pressed then run right
if ((RightKeyPress && CanJump))
{
x += RunSpeed;
gotoAndStop('Run');
scaleX = 1;
}
else if ((LeftKeyPress && CanJump))
{
//Otherwise if on floor and left key is pressed run left
x -= RunSpeed;
gotoAndStop('Run');
scaleX = -1;
}
else if ((UpKeyPress && CanJump))
{
//Otherwise if on floor and up key is pressed then jump
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If on floor and right and up key are pressed then jump
if ((RightKeyPress && UpKeyPress && CanJump))
{
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If on floor and left and up key are pressed then jump
if ((LeftKeyPress && UpKeyPress && CanJump))
{
JumpPower = -15;
CanJump = false;
gotoAndStop('Jump');
DoubleJump = true;
}
//If jumped and right key is pressed then move right
if ((RightKeyPress && CanJump == false))
{
x += RunSpeed;
scaleX = 1;
}
else if ((LeftKeyPress && CanJump == false))
{
//Otherwise if jumped and left key is pressed then move left
x -= RunSpeed;
scaleX = -1;
}
//If in air and able to doublejump and pressed up key, then double jump
if (UpKeyPress && DoubleJump && JumpPower > -2)
{
JumpPower = -13;
DoubleJump = false;
gotoAndStop('DoubleJump');
}
//If on floor and no key is presses stay idle
if ((!RightKeyPress && !LeftKeyPress && CanJump))
{
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;
}
}
}
}
I suspect that what is happening is that as soon as your player's y value meets the condition if (this.y > 300) you are enabling him to jump again, so as long as the key is held down he jumps because UpKeyPress && CanJump are both true.
My guess is that doing something like the following might get you closer yo your answer...
In your Update function:
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;
// Do not allow another jump until the UpKey is pressed
//CanJump = true;
//DoubleJump = false;
}
...
Then in your KeyPressed function:
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
// Do not allow another jump until the UpKey is pressed
if (this.y > 300)
{
CanJump = true;
DoubleJump = false;
}
}
}
I also second shaunhusain's recommendation to set breakpoints and get comfortable with debugging code.

As3 How to flip a movieclip to face movement direction?

Working on a maze game. When the leftkey is pressed the movieclip (char) should turn 90 degrees to the left.
Correct me if i'm wrong but I thought I could use this code;
char.scaleX *= -1;
However, the most important thing is that the character doesnt go through the walls of the maze.
And I think thats my problem for implementing the code above.
Because it doesnt work properly when i put in here;
if(!mazehit) {
char.y += speed;
char.scaleX *= -1;
}
My question to you is, where do I have to put the code to flip the movieclip?
var leftArrow, rightArrow, upArrow, downArrow:Boolean;
var speed:Number = 4;
var charRadius:Number = 10;
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, keyReleased);
stage.addEventListener(Event.ENTER_FRAME, everyFrame);
function keyPressed(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = true;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = true;
}
if (event.keyCode == Keyboard.UP) {
upArrow = true;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = true;
}
}
function keyReleased(event:KeyboardEvent):void {
if (event.keyCode == Keyboard.LEFT) {
leftArrow = false;
}
if (event.keyCode == Keyboard.RIGHT) {
rightArrow = false;
}
if (event.keyCode == Keyboard.UP) {
upArrow = false;
}
if (event.keyCode == Keyboard.DOWN) {
downArrow = false;
}
}
function everyFrame(event:Event):void {
var mazehit:Boolean = false;
if (leftArrow) {
for(var i:int = 0; i < speed; i++) {
if(bounds.hitTestPoint(char.x - charRadius - i, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x -= speed;
}
} else if (rightArrow) {
for(var j:int = 0; j < speed; j++) {
if(bounds.hitTestPoint(char.x + charRadius + j, char.y, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.x += speed;
}
} else if (upArrow) {
for(var k:int = 0; k < speed; k++) {
if(bounds.hitTestPoint(char.x, char.y - charRadius - k, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.y -= speed;
}
} else if (downArrow) {
for(var m:int = 0; m < speed; m++) {
if(bounds.hitTestPoint(char.x, char.y + charRadius + m, true)) {
mazehit = true;
break;
}
}
if(!mazehit) {
char.y += speed;
}
}
}
thank you for your time
I would update the direction depending on the speed:
char.scaleX = (speed > 0) ? 1 : -1;
Or, by the keys that have been pressed:
if(keyLeft && !keyRight)
{
char.scaleX = -1;
}
else if(keyRight && !keyLeft)
{
char.scaleX = 1;
}
else
{
// keep current direction
}